Monday, October 15, 2007

Delegate (1) static method

C# Delegates Explained



using System;

namespace Delegates

{

public delegate int DelegateToMethod(int x, int y);

public class Math

{

public static int Add(int first, int second)

{

return first + second;

}

public static int Multiply(int first, int second)

{

return first * second;

}

public static int Divide(int first, int second)

{

return first / second;

}

}

public class DelegateApp

{

public static void Main()

{

DelegateToMethod aDelegate = new DelegateToMethod(Math.Add);

DelegateToMethod mDelegate = new DelegateToMethod(Math.Multiply);

DelegateToMethod dDelegate = new DelegateToMethod(Math.Divide);

Console.WriteLine("Calling the method Math.Add() through the aDelegate object");

Console.WriteLine(aDelegate(5, 5));

Console.WriteLine("Calling the method Math.Multiply() through the mDelegate object");

Console.WriteLine(mDelegate(5, 5));

Console.WriteLine("Calling the method Math.Divide() through the dDelegate object");

Console.WriteLine(dDelegate(5, 5));

Console.ReadLine();

}

}

}

blog comments powered by Disqus