Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace Delegates
- {
- class Program
- {
- // delegate defines the signature (returns type and parameters);
- public delegate int Manipulate(int a);
- static void Main(string[] args)
- {
- // Create a instance of the delegate
- var normalMethodDelegate = new Manipulate(NormalMethod); // allows methods to be passed as parameter; references to another method
- Console.WriteLine(normalMethodDelegate(4));
- var result = AnotherMethod(normalMethodDelegate, 4); // Pass delegate as a parameter
- Console.WriteLine(result);
- Manipulate anonymousMethod = delegate (int a) { return a * 2; }; // Anonymous Method is a delegate () { } and returns a delegate
- var anonymousResult = anonymousMethod(4);
- //Lambda expressions are everything which have => left and right value; They can return delegate or an Expression of the delegate;
- Manipulate lambdaDelegate = (a) => { return a * 2; };
- var lambdaResult = lambdaDelegate(5);
- }
- public static int NormalMethod(int a)
- {
- return a * 2;
- }
- public static int AnotherMethod(Manipulate method, int b)
- {
- return method(b);
- }
- }
- }
Add Comment
Please, Sign In to add comment