Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

I am Jalpesh Vadgama a Microsoft MVP for Visual C# and BrainBench Certified ASP.NET Developer having more then 6 years of experience in .NET Technology.
Browse by Tags · View All
#ASP.NET 88
#DOTNET 87
BRH 79
ASP.NET 72
#.NET 52
C#.NET 48
DOTNET 47
ASP.NET 4.0 31
ASP.NET MVC 29
VisualStudio 27

Archive · View All
December 2010 16
July 2011 13
April 2011 13
April 2012 12
January 2011 12
June 2011 11
May 2011 11
May 2012 8
February 2013 7
January 2013 7

Delegates in c#

Apr 12 2012 3:54AM by Jalpesh   

I have used delegates in my programming since C# 2.0. But I have seen there are lots of confusion going on with delegates so I have decided to blog about it. In this blog I will explain about delegate basics and use of delegates in C#.

What is delegate?

We can say a delegate is a type safe function pointer which holds methods reference in object. As per MSDN it's a type that references to a method. So you can assign more than one methods to delegates with same parameter and same return type.

Following is syntax for the delegate

public delegate int Calculate(int a, int b);

Here you can see the we have defined the delegate with two int parameter and integer parameter as return parameter. Now any method that matches this parameter can be assigned to above delegates. To understand the functionality of delegates let’s take a following simple example.

using System;
namespace Delegates
{
    class Program
    {
        public delegate int CalculateNumber(int a, int b);
        static void Main(string[] args)
        {
            int a = 5;
            int b = 5;
            CalculateNumber addNumber = new CalculateNumber(AddNumber);
            Console.WriteLine(addNumber(5, 6));
            Console.ReadLine(); 
        }
        public static int AddNumber(int a, int b)
        {
            return a + b;
        }
    }
}

Here in the above code you can see that I have created a object of CalculateNumber delegate and I have assigned the AddNumber static method to it. Where you can see in ‘AddNumber’ static method will just return a sum of two numbers. After that I am calling method with the help of the delegates and printing out put to the console application.

Now let’s run the application and following is the output as expected.

Deletegates in C#

That’s it. You can see the out put of delegates after adding a number. This delegates can be used in variety of scenarios. Like in web application we can use it to update one controls properties from another control’s action. Same you can also call a delegates whens some UI interaction done like button clicked.

Hope you liked it. Stay tuned for more. In next post I am going to explain about multicast delegates. Till then happy programming.

Shout it

Tags: C#.NET, #DOTNET, .NET, Delegate,


Jalpesh
15 · 11% · 3548
3
 
0
Lifesaver
 
 
0
Learned
 
0
Incorrect



Submit

2  Comments  

  • Hi Jalpesh,

    Good article. People often confused with delegates as they exactly don't know, when to use the delegates. Once, they knows when to use delegates, it will be easy for them. Suppose the example you showed can be achieved without using delegates also. Initially i used to confused with this point.

    One area i found delegates useful is in MVVM pattern. When the viewmodel wants to communicate with view, by using delegate, we can achieve this.

    commented on Apr 13 2012 3:11AM
    Ramireddy
    2 · 40% · 12972
  • @Ramireddy- Yes people are so much confused. So that's why I have written that blog post

    commented on Apr 13 2012 4:08AM
    Jalpesh
    15 · 11% · 3548

Your Comment


Sign Up or Login to post a comment.

"Delegates in c#" rated 5 out of 5 by 3 readers
Delegates in c# , 5.0 out of 5 based on 3 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]