Sponsored Ad

Friday, October 23, 2009

Delegates in C#

The following code snippet demonstrates the use of a delegate. It is a long - winded way of calling the ToString() scheme on an int :

private delegate string GetAString();
static void Main()
{
int x = 40;
GetAString firstStringMethod = new GetAString(x.ToString);
Console.WriteLine(“String is {0}”, firstStringMethod());

(continued)

// With firstStringMethod initialized to x.ToString(),
// the above statement is equivalent to saying
// Console.WriteLine(“String is {0}”, x.ToString());
}

In this code, you instantiate a delegate of type GetAString , & you initialize it so it refers to the ToString() process of the integer variable x . Delegates in C# always syntactically take a six – parameter constructor, the parameter being the process to which the delegate will refer. This process must match the signature with which you originally defined the delegate. So in this case, you would receive a
compilation error if you tried to initialize the variable firstStringMethod with any process that did not take any parameters & return a string. Notice that, because int.ToString() is an instance process (as opposed to a static six), you require to specify the instance ( x ) as well as the name of the process to initialize the delegate properly.

The next line actually uses the delegate to display the string. In any code, supplying the name of a delegate instance, followed by brackets containing any parameters, has exactly the same effect as calling the method wrapped by the delegate. Hence, in the preceding code snippet, the Console.WriteLine()
statement is equivalent to the commented - out line.

In fact, supplying brackets to the delegate instance is the same as invoking the Invoke() method of thedelegate class. Because firstStringMethod is a variable of a delegate type, the C# compiler replaces
firstStringMethod() with firstStringMethod.Invoke() :

firstStringMethod();
firstStringMethod.Invoke();

For less typing, at every place where a delegate instance is needed, you can pass the name of the address. This is known by the term delegate inference . This C# feature works as long as the compiler can resolve the delegate instance to a specific type. The example initialized the variable firstStringMethod of type GetAString with a new instance of the delegate GetAString :

GetAString firstStringMethod = new GetAString(x.ToString);
You can write the same just by passing the method name with the variable x to the variable
firstStringMethod :
GetAString firstStringMethod = x.ToString;

The code that is created by the C# compiler is the same. The compiler detects that a delegate type is required with firstStringMethod , so it creates an instance of the delegate type GetAString & passes the address of the technique with the object x to the constructor.

“” Be aware that you can ’ t type the ( & ) as x.ToString() & pass it to the delegate variable. This would be an invocation of the method. The invocation of x.ToString() returns a string object that can ’ t be assigned to the delegate variable. You can only assign the address of a method to the delegate variable.” ”

0 comments:

Post a Comment

Sponsored Ad

Website Update

Followers