Skip to content

Instantly share code, notes, and snippets.

@codingonHP
Created September 2, 2016 18:46
Show Gist options
  • Save codingonHP/611543e4a684d54b20ee67745396240d to your computer and use it in GitHub Desktop.
Save codingonHP/611543e4a684d54b20ee67745396240d to your computer and use it in GitHub Desktop.
public delegate int MathOperationDelegate(int x, int y);
class Program
{
static void Main(string[] args)
{
MathOperationDelegate operationDelegate = null;
//No need to create a seperate method
operationDelegate += delegate(int x, int y)
{
return x + y;
};
operationDelegate += delegate(int x, int y)
{
return x - y;
};
operationDelegate += delegate(int x, int y)
{
return x * y;
};
operationDelegate += delegate(int x, int y)
{
return x / y;
};
//Prior to .NET 2
//Methods have to be explicitely created, prior to the Invoke
operationDelegate += OnAddMathOperationDelegate;
operationDelegate += OnMulMathOperationAction;
operationDelegate += OnDivMathOperationAction;
operationDelegate += delegate(int x, int y)
{
return x / y;
};
var result = operationDelegate(12, 12);
Console.WriteLine( result );
}
private static int OnDivMathOperationAction(int x, int y)
{
return x*y;
}
private static int OnMulMathOperationAction(int x, int y)
{
return x - y;
}
private static int OnAddMathOperationDelegate(int x, int y)
{
return x + y;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment