Click here to Skip to main content
Click here to Skip to main content
Technical Blog

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

, 15 Oct 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
In this post I am going to discus about the delegate uses in C# and VB.NET.

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 the 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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

RizviHasan
Software Developer
Sweden Sweden
No Biography provided

Comments and Discussions

 
GeneralMy vote of 4 PinmemberMAD_Den23-Oct-12 10:52 
GeneralMy vote of 1 PinmemberNick Hounsome15-Oct-12 22:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 15 Oct 2012
Article Copyright 2012 by RizviHasan
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid