Last active
August 27, 2016 07:54
-
-
Save codingonHP/c7fea26359ee832eff125b247e48902c 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
namespace DelegateLearning | |
{ | |
public delegate int MathematicalOperationDelegate(int x, int y); //declare | |
public enum MathematicalOperation | |
{ | |
Add, | |
Substract, | |
Multiply, | |
Divide | |
} | |
class DelegateLearning | |
{ | |
private readonly MathematicalOperationDelegate _mathematicalOperationDelegate; | |
public DelegateLearning(MathematicalOperation operation) | |
{ | |
switch (operation) | |
{ | |
case MathematicalOperation.Add: | |
_mathematicalOperationDelegate += new MathematicalOperationDelegate(AddOperationTarget); //Adding to invocation list | |
break; | |
case MathematicalOperation.Substract: | |
_mathematicalOperationDelegate += new MathematicalOperationDelegate(SubOperationTarget); //Adding to invocation list | |
break; | |
case MathematicalOperation.Multiply: | |
_mathematicalOperationDelegate += new MathematicalOperationDelegate(MultiplyOperationTarget); //Adding to invocation list | |
break; | |
case MathematicalOperation.Divide: | |
_mathematicalOperationDelegate += new MathematicalOperationDelegate(DivideOperationTarget); //Adding to invocation list. | |
break; | |
} | |
} | |
public int PerformMathematicalOperation(int x, int y , MathematicalOperation operation) | |
{ | |
int result = 0; | |
if (_mathematicalOperationDelegate != null) | |
{ | |
result = _mathematicalOperationDelegate(x, y); //Invoke | |
Console.WriteLine( "Result : " + result); | |
} | |
return result; | |
} | |
private int AddOperationTarget(int x, int y) | |
{ | |
return x + y; | |
} | |
private int SubOperationTarget(int x, int y) | |
{ | |
return x - y; | |
} | |
private int MultiplyOperationTarget(int x, int y) | |
{ | |
return x * y; | |
} | |
private int DivideOperationTarget(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