Wednesday, 9 May 2012

C# Delegates

The concept of Delegates is not easy to understand. Delegates are custom defined data types that can store methods internally. It'll be easier after you practice a few times. Although you may not use it that much, it's still worth learning.

http://msdn.microsoft.com/en-us/library/ms173171%28v=vs.90%29.aspx
http://msdn.microsoft.com/en-us/library/System.Delegate_methods.aspx
-----------------------------------------------------------------------------------
using System;

delegate void MyDelegate(int X, int y); //Declaring new delegate type
public static void MethodA()
{
       MyDelegate process = new ProcessNums(AddNums); //Instantiating with AddNums method.
        process(3, 5); //The output should be 8
}
static void AddNums(int x, int y) //The method is enclosed in the delegate(MyDelegate)
{
        Console.WriteLine(x + y);
}

No comments:

Post a Comment