using System;
namespace ConsoleApplication1
{
// Declaration of delegate
// This delegate can call only those methods which has
// integer return type and takes two integer type parameter.
delegate int MathsOperation(int a, int b);
public class DelegateDemo
{
public int Addition(int x, int y)
{
return x + y;
}
public int Subtraction(int x, int y)
{
return x - y;
}
}
class Program
{
static void Main(string[] args)
{
DelegateDemo obj = new DelegateDemo();
// Provides the function reference to the delegate.
MathsOperation delgateObj = new MathsOperation(obj.Addition);
// Delegate call
int resAdd = delgateObj(10, 20);
Console.WriteLine("Addition =: "+resAdd);
delgateObj = new MathsOperation(obj.Subtraction);
Console.WriteLine("Subtraction =: " + delgateObj(100, 50));
Console.ReadLine();
}
}
}
using System;
namespace ConsoleApplication1
{
// Declaration of delegate
// This delegate can call only those methods which has
// void return type and takes two integer type parameter.
delegate void MathsOperation(int a, int b);
public class DelegateDemo
{
public void Addition(int x, int y)
{
Console.WriteLine("Addition of {0} and {1} is =: {2}",x,y,(x + y));
}
public void Subtraction(int x, int y)
{
Console.WriteLine("Subtraction of {0} and {1} is =: {2}", x, y, (x - y));
}
}
class Program
{
static void Main(string[] args)
{
DelegateDemo obj = new DelegateDemo();
// Provides the function reference to the delegate.
MathsOperation delgateObj = new MathsOperation(obj.Addition);
// Delegate call, prints only addition
delgateObj(10, 20);
Console.WriteLine();
// Delegate call, prints addition and subtraction.
// You can use any of the following method.
// delgateObj += new MathsOperation(obj.Subtraction); or
delgateObj +=obj.Subtraction;
delgateObj(100, 50);
Console.ReadLine();
}
}
}
using System;
namespace ConsoleApplication1
{
delegate void Factorial(int a);
class Program
{
static void Main(string[] args)
{
Factorial delgateObj = delegate(int n)
{
int fact = 1;
for(int i=1; i<=n; i++)
{
fact = fact * i;
}
Console.WriteLine ("Factorial of {0} is =: {1}", n, fact);
};
Console.WriteLine ("Enter the number to find the factorial");
int no =Convert.ToInt32( Console.ReadLine());
delgateObj(no);
Console.ReadLine();
}
}
}
using System;
namespace ConsoleApplication1
{
delegate int Add(int a, int b);
class Program
{
static void Main(string[] args)
{
Add delgateObj = delegate(int x,int y)
{
return x+y;
};
Console.WriteLine("Enter two number for addition");
int a=Convert.ToInt32( Console.ReadLine());
int b = Convert.ToInt32(Console.ReadLine());
int sum = delgateObj(a, b);
Console.WriteLine("Addition of {0} and {1} is =: {2}",a,b,sum);
Console.ReadLine();
}
}
}
using System;
namespace ConsoleApplication1
{
// Declare a delegate for an event.
delegate void MyEventHandler();
class SimpleEvent
{
public event MyEventHandler MyEvent;
// This method will fire the event.
public void OnEvent()
{
if (MyEvent != null)
MyEvent();
}
}
class Program
{
static void Handler()
{
Console.WriteLine("Hello My Event");
}
static void Main(string[] args)
{
SimpleEvent eventObject = new SimpleEvent();
// Add Handler() to the event list.
eventObject.MyEvent += Handler;
// Below line will fire the event.
eventObject.OnEvent();
Console.ReadLine();
}
}
}