Tuesday, September 25, 2007
Delegates - Talk About Name Confusion!
Working with delegates in C# can be bit confusing. Unlike most of object-oriented programming that makes a clear distinction in terminology between definitions (classes) and instances (objects), delegates have no such distinction. What is meant by the word "delegate" depends on the context in which it is being used.
Consider
To avoid confusion, I tend to use the terms "delegate definition" to describe
Consider
MyClass1 below. The word "delegate" refers to three different things: (1) the data type MyDelegateType that defines a method signature; (2) the variable MyDelegateVariable of type MyDelegateType that contains the list of methods to be called; and (3) the instance myDelegateMethodPointer that points to a specific instance and method to be called.
public class MyClass1
{
delegate void MyDelegateType();
MyDelegateType MyDelegateVariable;
public MyClass1()
{
MyDelegateType myDelegateMethodPointer =
new MyDelegateType(MyHandlerMethod);
MyDelegateVariable += myDelegatePointer;
}
private void MyHandlerMethod()
{
}
}
To avoid confusion, I tend to use the terms "delegate definition" to describe
MyDelegateType, "delegate list" to describe MyDelegateVariable, "handler" to describe myDelegateMethodPointer, and "handler method" to describe MyHandlerMethod.Subscribe to Posts [Atom]