wingman007

FuncActionPredicateDotNet

Nov 20th, 2021 (edited)
698
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.49 KB | None | 0 0
  1. // https://docs.microsoft.com/bg-bg/dotnet/csharp/programming-guide/concepts/covariance-contravariance/using-variance-for-func-and-action-generic-delegates
  2. // https://dotnettutorials.net/lesson/generic-delegates-csharp/
  3. /*
  4. Do we really need to create the Delegates?
  5. The answer is no. C# provides some generic delegates who can do the job for us. C# provides three Generic Delegates, they are as follows
  6.  
  7. Func
  8. Action
  9. Predicate
  10. What is Func Generic Delegate in C#?
  11. The Func Generic Delegate in C# is present in the System namespace. This delegate takes one or more input parameters and returns one out parameter. The last parameter is considered as the return value. The Func Generic Delegate in C# can take up to 16 input parameters of different types. It must have one return type. The return type is mandatory but the input parameter is not.
  12.  
  13. Note: Whenever your delegate returns some value, whether by taking any input parameter or not, you need to use the Func Generic delegate in C#.
  14.  
  15. What is Action Generic Delegate in C#?
  16. The Action Generic Delegate in C# is also present in the System namespace. It takes one or more input parameters and returns nothing. This delegate can take a maximum of 16 input parameters of the different or same type
  17.  
  18. Note: Whenever your delegate does not return any value, whether by taking any input parameter or not, then you need to use the Action Generic delegate in C#.
  19.  
  20. What is Predicate Generic Delegate in C#?
  21. The Predicate Generic Delegate in C# is also present in the System namespace. This delegate is used to verify certain criteria of the method and returns the output as Boolean, either True or False. It takes one input parameter and always returns a Boolean value which is mandatory. This delegate can take a maximum of 1 input parameter and always return the value of the Boolean type.
  22.  
  23.  Note: Whenever your delegate returns a Boolean value, by taking one input parameter, then you need to use the Predicate Generic delegate in C#.
  24.  */
  25.  
  26. // https://docs.microsoft.com/bg-bg/dotnet/csharp/programming-guide/concepts/covariance-contravariance/using-variance-for-func-and-action-generic-delegates
  27. /*
  28. // Simple hierarchy of classes.  
  29. public class Person
  30. {
  31.  
  32. }
  33. public class Employee : Person
  34. {
  35.  
  36. }
  37. class Program
  38. {
  39.     static void Main(string[] args)
  40.     {
  41.         Console.WriteLine(FindByTitle("This is the title"));
  42.  
  43.     }
  44.  
  45.     static Employee FindByTitle(String title)
  46.     {
  47.         // This is a stub for a method that returns  
  48.         // an employee that has the specified title.  
  49.         return new Employee();
  50.     }
  51.  
  52.     static void Test()
  53.     {
  54.         // Create an instance of the delegate without using variance.  
  55.         Func<String, Employee> findEmployee = FindByTitle;
  56.  
  57.         // The delegate expects a method to return Person,  
  58.         // but you can assign it a method that returns Employee.  
  59.         Func<String, Person> findPerson = FindByTitle;
  60.  
  61.         // You can also assign a delegate
  62.         // that returns a more derived type
  63.         // to a delegate that returns a less derived type.  
  64.         findPerson = findEmployee;
  65.  
  66.     }
  67. }
  68. */
  69.  
  70. public class Person
  71. {
  72.  
  73. }
  74. public class Employee : Person
  75. {
  76.  
  77. }
  78. class Program
  79. {
  80.     static void Main(string[] args)
  81.     {
  82.         AddToContacts(new Person());
  83.     }
  84.  
  85.     static void AddToContacts(Person person)
  86.     {
  87.         // This method adds a Person object  
  88.         // to a contact list.  
  89.     }
  90.  
  91.     static void Test()
  92.     {
  93.         // Create an instance of the delegate without using variance.  
  94.         Action<Person> addPersonToContacts = AddToContacts;
  95.  
  96.         // The Action delegate expects
  97.         // a method that has an Employee parameter,  
  98.         // but you can assign it a method that has a Person parameter  
  99.         // because Employee derives from Person.  
  100.         Action<Employee> addEmployeeToContacts = AddToContacts;
  101.  
  102.         // You can also assign a delegate
  103.         // that accepts a less derived parameter to a delegate
  104.         // that accepts a more derived parameter.  
  105.         addEmployeeToContacts = addPersonToContacts;
  106.  
  107.         // Extra experiments
  108.         // All the statments work
  109.         // Action<int> noParameters = (int a) => { int b = 1; }; // lambda statment
  110.         // Func<int> noParameters = () => 1; // lambda expression
  111.         // var noParameters = () => 1; // lambda expression
  112.         Func<int> noParameters = delegate () { return 1; }; // anonymous function
  113.     }
  114.  
  115.     // this works
  116.     // static int DoSomething() => 1;
  117. }
Add Comment
Please, Sign In to add comment