Approaches to Web Development for Bioinformatics

Developing Applications with C#

Previous  Contents  Next 
References

Delegates

A delegate is a C# type that is a reference to a method. Delegates are something like function pointers in C++ and variables that point to functions in scripting languages like JavaScript but more strongly typed. The main use of delegates is for callbacks. The same thing can be acheived with interfaces but delegates are more concise. Here an example that uses a delegate for logging messages. The example implementation logs to the console but a real implementation in a web environment would log to a file.


C#

// Program to illustrate use of delegates

// Delegate to log messages
public delegate void Log(string message);

// Exercise the Log delegate
class LogTest
{

// Simple imlementation logs a message to the console
// Log to a file in a web environemt
public static void LogMethod(string message)
{
System.Console.WriteLine(message);
}

static void Main() {
// Instantiate the log delegate and call it
Log log = LogMethod;
log("My log message");
}
}

In this example the code using the delegate has full knowledge of the implementation. In a real example the delegate would be passed in as a parameter leaving the calling code independent of the implementation of the callback. An example of this type of use in bioinformatics might be a long running search. The calling code may wish to be notified every time a match was found without having to make the user wait.

A delegate type can be assigned more than one method and will call all assigned methods when invoked. This is referred to as multicasting in the C# literature. To assign more than one method to a delegate use the + operator. Use the - operator to remove a delegate. The operators += and -= work in an intuitive way with delegates. Multicasting with delegates is used extensively in event handling in C#.

Anonymous methods can also be assigned to delegates. An anonymous method is a code block without a method name. Here is a program that illustrates use of an anonymous method as a delegate.


C#

// Program to demonstrate use of delegates for callbacks

// Delegate to call when a match is found
public delegate void MatchFoundCallback(string message);

// A class to do long running searches
public class LongRunningSearch
{

public void Search(string toMatch, MatchFoundCallback callback)
{
// Do the search ...

// Found a match
callback("Found a " + toMatch);
}
}

// Exercise the LongRunningSearch class
class SearchTest
{

static void Main()
{
int matchCount = 1;
// Instantiate the an anonymous MatchFoundCallback delegate
MatchFoundCallback myCallback =
delegate(string message)
{
System.Console.WriteLine("Match No. " + (matchCount++) + " " + message);
};
LongRunningSearch search = new LongRunningSearch();

// Pass the delegate to the search
search.Search("TACG", myCallback);
}
}

In this example the code that calls the delegate in the LongRunningSearch class is not aware of the impementation. It simply calls the delegate whenever a match is found. The program also illustrates the concept of a captured varable. The variable matchCount is declared outside of the anonymous method but is still accessible. This is a useful technique to use to pass context information with the callback. The output from this program is:


>SearchDelegate.exe
Match No. 1 Found a TACG
Match No. 2 Found a TACG


Previous  Contents  Next 
References

Contributed Comments and NotesAdd a comment.

There are no user comments.

Google

Please send ideas and opinions by email at alexamies@gmail.com.

© 2006-2007 Alex Amies