Generic Delegates

Added in .NET Framework 3.5
Two generic delegate data types are available.
These have been introduced so you do not have to define your own.
You can use delegates to pass a method as a parameter without having to declare a custom delegate.


The Func delegate is used for methods that accept one or more arguments and return a value.
An anonymous function is an inline statement or expression that can be used wherever a delegate type is expected.
A delegate with a specific signature can be declared using the Func type.
The last parameter of the Func delegate is the output parameter or result parameter.

System.Func<string, string> delegate_variable = delegate(string param1) 
{ return parameter.ToUpper(); }

instead of

delegate string Delegate_Type(string x); 

Delegate_Type delegate_variable delegate(string param1)
{ return parameter.ToUpper(); }


The Action delegate can be used for methods that accept one or more arguments and do not return a value.

System.Action<string, string> delegate_variable = delegate(string param1, string param2); 
{ //do something }

instead of

delegate void Delegate_Type(string x, string y); 

Delegate_Type delegate_variable delegate(string param1, string param2)
{ // do something }

This delegate can take zero (or more) parameters and must return void.
We have a class that contains a method that we want to call.

public class MyClass 
   public void MyMethod() { MessageBox.Show("something"); }

Before .NET 3.5 we had to declare the delegate.

public delegate void MyDelegate(); 

public class WithDeclaration
   public static void Main()
      MyClass class1 = new MyClass();
      MyDelegate delegate1 = class1.MyMethod;

With .NET 3.5 the custom declaration can now be replaced with the built-in data type new System.Action
There are several ways this could be written.

public class WithoutDeclaration 
   public static void Main()
      MyClass class2 = new MyClass();

      System.Action delegate2 = class2.MyMethod;
      System.Action delegate2 = new System.Action(class2.MyMethod);

      // other ways of writing the same thing
      System.Action delegate3 = delegate() { MessageBox.Show("something"); }
      System.Action delegate3 = new System.Action( () => MessageBox.Show("something"); );


The Predicate delegate is a special case of a Func, in that it takes only a single parameter and always returns a bool.
A predicate is a simple delegate that always returns booleans
A predicate is a functional construct that provides a convenient way of testing is something is true or false

class Person { 
    public string Name { get; set; }
    public int Age { get; set; }

Lets suppose that we have a List<Person> and we want to know if anyone has the name "Peter"
You could accomplish this by looping through the code

Person oscar = null; 
foreach (Person person in people) {
    if (person.Name == "Oscar") {
        oscar = person;

if (oscar != null) {
    // Oscar exists!

Lets suppose you also wanted to do some other tests, like is there anyone with the name "Steve"
or is there anyone with the age 35.
Using a Predicate will significantly reduce the amount of code you need to write

System.Predicate<Person> oscarFinder = (Person p) => { return p.Name == "Oscar"; }; 
System.Predicate<Person> ruthFinder = (Person p) => { return p.Name == "Ruth"; };
System.Predicate<Person> seventeenYearOldFinder = (Person p) => { return p.Age == 17; };

Person oscar = people.Find(oscarFinder);
Person ruth = people.Find(ruthFinder);
Person seventeenYearOld = people.Find(seventeenYearOldFinder);

© 2023 Better Solutions Limited. All Rights Reserved. © 2023 Better Solutions Limited TopPrevNext