Created
September 2, 2016 18:46
-
-
Save codingonHP/611543e4a684d54b20ee67745396240d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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