Welcome To Shadab's Blog

Programming Blog to learn C# , java, Sql Server 2005 and many more things.





About Me

My photo
Bokaro, Jharkhand, India

Friday, December 18, 2009

What is delegate in C# ?Explain with program.

using System;

class Delegate_Demo
{
public delegate void mydel(); //delegate declaration named mydel

public void method1()
{
Console.WriteLine("Method1");
}

public void method2()
{
Console.WriteLine("Method2");
}

public static void Main()

{

Delegate_Demo d=new Delegate_Demo();

mydel m1=new mydel(d.method1); //m1 contain's only one reference i.e method1

m1();

mydel m2=new mydel(d.method1);

m2+=new mydel(d.method2); //assinging multiple reference

m2(); //m2 contains two reference i.e method1 and method2

}

}

/*output will be
Method1
Method1
Method2

*/

A simple Program in C#

class First
{
           public static void Main()
           {
                  System.Console.WriteLine("Welcome to C# programming");
           }
}

// you can save this file as extenstion .cs and then compile it...
For compilng -> open commnd prompt
write -> csc filename.cs press enter

For Run-> write only filename on command prompt