You are on page 1of 4

Example:

class Program
{
public delegate void addnum(int a, int b);
public delegate void subnum(int a, int b);

public void add(int a, int b)


{
Console.WriteLine("{0} + {1} = {2}", a, b, a + b);
}
public void sub(int a, int b)
{
Console.WriteLine("{0} - {1} = {2}", a, b, a - b);
}
static void Main(string[] args)
{
Program obj = new Program();

addnum del1 = new addnum(obj.add);


subnum del2 = new subnum(obj.sub);

Console.Write("Enter first number: ");


int a = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter second number: ");
int b = Convert.ToInt32(Console.ReadLine());

del1(a, b);
del2(a, b);

Console.ReadKey();
}
}

Example:

namespace delegatesandeventsexample2
{
// Delegate Definition
public delegate int operation(int x, int y);

class Program
{
// Method that is passes as an Argument
// It has same signature as Delegates
static int Addition(int a, int b)
{
return a + b;
}

static void Main(string[] args)


{
// Delegate instantiation
operation obj = new operation(Program.Addition);

// output
Console.WriteLine("Addition is={0}", obj(23, 27));
Console.ReadLine();
}
}
}
Example:

class Program
{
// Delegate Definition
delegate void operation();

static void Main(string[] args)


{
// Delegate instantiation
operation obj = delegate
{
Console.WriteLine("Anonymous method");
};
obj();

Console.ReadLine();
}
}

Example:

public class Operation


{
public static void Add(int a)
{
Console.WriteLine("Addition={0}", a + 10);
}
public static void Square(int a)
{
Console.WriteLine("Multiple={0}", a * a);
}
}
class Program
{
delegate void DelOp(int x);

static void Main(string[] args)


{
// Delegate instantiation
DelOp obj = Operation.Add;
obj(2);

DelOp obj1 = new DelOp(Operation.Square);


obj1(4);

Console.ReadLine();
}
}
Example:

namespace delegatesandeventsexample7
{
public delegate void DelEventHandler();

class Program
{
public static event DelEventHandler add;

static void Main(string[] args)


{
add += new DelEventHandler(USA);
add += new DelEventHandler(India);
add += new DelEventHandler(England);
add.Invoke();

Console.ReadLine();
}
static void USA()
{
Console.WriteLine("USA");
}

static void India()


{
Console.WriteLine("India");
}

static void England()


{
Console.WriteLine("England");
}
}
}
Example:
namespace delegatesandeventsexample8
{
public delegate void DelEventHandler();

class Program
{
static int a = 35, b = 12, c;
public static event DelEventHandler calc;

static void Main(string[] args)


{

calc += new DelEventHandler(addition);


calc += new DelEventHandler(subtraction);
calc += new DelEventHandler(multiplication);
calc += new DelEventHandler(division);
calc += new DelEventHandler(modulus);
calc.Invoke();

Console.ReadLine();
}
static void addition()
{
Console.WriteLine("Addition = "+(Program.a+Program.b));
}

static void subtraction()


{
Console.WriteLine("Subtraction = " + (Program.a - Program.b));
}

static void multiplication()


{
Console.WriteLine("Multiplication = " + (Program.a * Program.b));
}

static void division()


{
Console.WriteLine("Division = " + (Program.a / Program.b));
}

static void modulus()


{
Console.WriteLine("Modulus = " + (Program.a % Program.b));
}
}
}

You might also like