Click here to Skip to main content
13,591,539 members
Click here to Skip to main content
Add your own
alternative version


11 bookmarked
Posted 15 Oct 2012
Licenced CPOL

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

, 15 Oct 2012
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;


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; };


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


// 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()

' When the function doesn't return (not appropriate for our delegate type)
func = Sub(str) Console.WriteLine(str.Length)
func = Sub(str)
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")


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.


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


About the Author

Software Developer
Sweden Sweden
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 4 Pin
MAD_Den23-Oct-12 9:52
memberMAD_Den23-Oct-12 9:52 
GeneralMy vote of 1 Pin
Nick Hounsome15-Oct-12 21:38
memberNick Hounsome15-Oct-12 21:38 

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

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

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