Ways of Delegation in .NET (C# and VB)


Introduction Straightly speaking delegation is a process of pointing a function. So a delegate type variable is a pointer of a function. In this post I am going to discus about the delegate uses in C# and VB.NET. Declaring an using a delegate is simple with four steps. If  I give an analogy with a normal variable type like integer or string or float then things will look more simple. The types integer and string are built-in in the system so need not declare their types. But for the delegate type we will have to declare that type with delegate keyword and the function signature (the function it will point to).

Step 1: Declaring a delegate type

Blue: C# code

 
	delegate int MyDel(string input);
 

and Green: VB code

 
	Delegate Function MyDel(str As String) As Integer
 

The this declaration means that MyDel type variable can only point to the functions that returns integer and takes a string as input parameter.

Step 2: Define a variable
Now  time to define a variable for that delegate type.
In C#

 
	MyDel func;
 

In VB.NET

 
	Dim func as MyDel
 

This is same as defining a variable x for integer type.

	int x;
 

Now what happens when you create a variable of a delegate type? It simply creates a pointer type variable that can point to a function. In pure C/C++ they are called function pointer.

Step 3: Assign a function to the variable

At this step we simply assign a function to this variable. This is as like assign a value of 5 to our integer type variable x.

 x = 5;

There are 3 ways to hook up a function with a variable in C# – using delegate keyword, using lambda expression and the ordinary way. We will point a function that takes a string as input and returns the length of the string in C#.

 
 // Assign our variable a new function (created via delegate)
 func = delegate(string str) { return str.Length; };

or

 
 // Assign our variable a new function (created via lambda expressions)
 func = (str => str.Length);

or

 // If the function is declated somewhere else as FunctionFoo
 func = FunctionFoo;
 // FunctionFoo declared somewhere else as
 private int FunctionFoo(string str)
 {
     return str.Lenght;
 }

Now in VB.NET there are only two ways – lamdba expression and the ordinary way. Use the lambda expression way as bellow

' When the function returns
func = Function(str) str.Length
func = Function(str)
    Return str.Length()
End 

' When the function doesn't return (not appropriate for our delegate type)
func = Sub(str) Console.WriteLine(str.Length)
func = Sub(str)
    Console.WriteLine(str.Length)
End Sub

or the ordinary way

// If the function is declated somewhere else as FunctionFoo
f = AddressOf FunctionFoo
// FunctionFoo declared somewhere else as
Function FunctionFoo(str As String) As String
    Return str.Length
End Function

Step 4: Calling the functions using delegate variable
Now is the time to call the function. To do this do this in C#

   func("My Input string");

and in VB.NET

   func("My Input string")

or

   func.Invoke("My Input string")

conclusion: This is very basic but yet very important topic specially when you want to route some functionality among different objects. Hope this will help you in your project.

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s