Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // https://docs.microsoft.com/bg-bg/dotnet/csharp/programming-guide/concepts/covariance-contravariance/using-variance-for-func-and-action-generic-delegates
- // https://dotnettutorials.net/lesson/generic-delegates-csharp/
- /*
- Do we really need to create the Delegates?
- 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
- Func
- Action
- Predicate
- What is Func Generic Delegate in C#?
- 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.
- 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#.
- What is Action Generic Delegate in C#?
- 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
- 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#.
- What is Predicate Generic Delegate in C#?
- 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.
- Note: Whenever your delegate returns a Boolean value, by taking one input parameter, then you need to use the Predicate Generic delegate in C#.
- */
- // https://docs.microsoft.com/bg-bg/dotnet/csharp/programming-guide/concepts/covariance-contravariance/using-variance-for-func-and-action-generic-delegates
- /*
- // Simple hierarchy of classes.
- public class Person
- {
- }
- public class Employee : Person
- {
- }
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine(FindByTitle("This is the title"));
- }
- static Employee FindByTitle(String title)
- {
- // This is a stub for a method that returns
- // an employee that has the specified title.
- return new Employee();
- }
- static void Test()
- {
- // Create an instance of the delegate without using variance.
- Func<String, Employee> findEmployee = FindByTitle;
- // The delegate expects a method to return Person,
- // but you can assign it a method that returns Employee.
- Func<String, Person> findPerson = FindByTitle;
- // You can also assign a delegate
- // that returns a more derived type
- // to a delegate that returns a less derived type.
- findPerson = findEmployee;
- }
- }
- */
- public class Person
- {
- }
- public class Employee : Person
- {
- }
- class Program
- {
- static void Main(string[] args)
- {
- AddToContacts(new Person());
- }
- static void AddToContacts(Person person)
- {
- // This method adds a Person object
- // to a contact list.
- }
- static void Test()
- {
- // Create an instance of the delegate without using variance.
- Action<Person> addPersonToContacts = AddToContacts;
- // The Action delegate expects
- // a method that has an Employee parameter,
- // but you can assign it a method that has a Person parameter
- // because Employee derives from Person.
- Action<Employee> addEmployeeToContacts = AddToContacts;
- // You can also assign a delegate
- // that accepts a less derived parameter to a delegate
- // that accepts a more derived parameter.
- addEmployeeToContacts = addPersonToContacts;
- // Extra experiments
- // All the statments work
- // Action<int> noParameters = (int a) => { int b = 1; }; // lambda statment
- // Func<int> noParameters = () => 1; // lambda expression
- // var noParameters = () => 1; // lambda expression
- Func<int> noParameters = delegate () { return 1; }; // anonymous function
- }
- // this works
- // static int DoSomething() => 1;
- }
Add Comment
Please, Sign In to add comment