Saturday, December 17, 2011

Delegtes and Events

An interesting and useful property of a delegate is that it does not know or care about the class of the object that it references.
public delegate void SimpleDelegate ()
A delegate will allow us to specify what the function we'll be calling looks like without having to specify whichfunction to call.
Delegate and Event concepts are completely tied together. Delegates are just function pointers, That is, they hold references to functions.
A Delegate is a class. When you create an instance of it, you pass in the function name (as a parameter for the delegate's constructor) to which this delegate will refer.
Every delegate has a signature. For example:
Eg:
using System;
namespace Akadia.BasicDelegate
{
// Declaration
public delegate void SimpleDelegate();
class TestDelegate
{
public static void MyFunc()
{
Console.WriteLine("I was called by delegate ...");
}
   public static void Main()
{
// Instantiation
SimpleDelegate simpleDelegate = new SimpleDelegate(MyFunc);
// Invocation
simpleDelegate();
}
}
}

List of Asked Interview Questions: C# .NET SQL MVC WCF Questions