Delegate is a reference type that holds the reference of a class method. Any method which has the same signature as delegate can be assigned to delegate. Delegate is like function pointer in C++.
To read more delegates-c-sharp.html
Different Flavors of Delegate
Func<string, int, int> tempFuncPointer;
Func is always used when you have return object or type from method. If you have void method, you should be using Action.
2- Action is used when we do not have any return type from method. Method with void signature is being used with Action delegate.
Action<string, int> tempActionPointer;
Similar to Func delegate, the first two parameters are the method input parameters. Since we do not have return object or type, all the parameters are considered as input parameters.
3- Predicate is a function pointer for method which returns boolean value. It is used to represent a set of criteria and determine if the argument matches the criteria. Declaration for the same looks like this:
Predicate<Employee> tempPredicatePointer;
Example Code:
In short:
To read more delegates-c-sharp.html
Different Flavors of Delegate
- Func<TParameter, TOutput>
- Action<TParameter>
- Predicate<in T>
Func<string, int, int> tempFuncPointer;
Func is always used when you have return object or type from method. If you have void method, you should be using Action.
2- Action is used when we do not have any return type from method. Method with void signature is being used with Action delegate.
Action<string, int> tempActionPointer;
Similar to Func delegate, the first two parameters are the method input parameters. Since we do not have return object or type, all the parameters are considered as input parameters.
3- Predicate is a function pointer for method which returns boolean value. It is used to represent a set of criteria and determine if the argument matches the criteria. Declaration for the same looks like this:
Predicate<Employee> tempPredicatePointer;
Example Code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TestAppConsole { public class Program { public static void Main(string[] args) { Action<int> myAction = new Action<int>(DoSomething); myAction(123); // Prints out "123" // can be also called as myAction.Invoke(123); Func<int, double> myFunc = new Func<int, double>(CalculateSomething); Console.WriteLine(myFunc(5)); // Prints out "2.5" Predicate<int> myPred = new Predicate<int>(CheckVal); Console.WriteLine(myPred(1)); // Prints out "true" Console.ReadLine(); } static void DoSomething(int i) { Console.WriteLine(i); } static double CalculateSomething(int i) { return (double)i / 2; } static bool CheckVal(int i) { return true; } } }
In short:
Action is a delegate (pointer) to a method, that takes zero, one or more input parameters, but does not return anything.
Func is a delegate (pointer) to a method, that takes zero, one or more input parameters, and returns a value (or reference).
Predicate is a special kind of Func often used for comparisons.
0 comments:
Post a Comment