Delegate is used to pass a method as a parameter to other methods. The following is an example of entrusting an application:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace DelegateApp
{
Public class Person
{
Public string Name {get; set ;}
Public int Age {get; set ;}
}
Class Program
{
// Define the FILTER delegate
Public delegate bool FilterDelegate (Person p );
Static void Main (string [] args)
{
// Create Person objects
Person p1 = new Person () {Name = "John", Age = 41 };
Person p2 = new Person () {Name = "Jane", Age = 69 };
Person p3 = new Person () {Name = "Jake", Age = 12 };
Person p4 = new Person () {Name = "Jessie", Age = 25 };
Person p5 = new Person () {Name = "Cherry", Age = 6 };
// Create a list of Person objects and fill it
List <Person> people = new List <Person> () {p1, p2, p3, p4, p5 };
DisplayPeople ("Children:", people, IsChild );
DisplayPeople ("Adult:", people, IsAdult );
DisplayPeople ("Old Man:", people, IsSenior );
// The call method remains unchanged. You only need to add the method IsOnetoThree called by the delegate.
DisplayPeople ("10 years old ~ Between the ages of 30: ", people, IsOnetoThree );
Console. Read ();
}
/// <Summary>
/// A method to filter out the people you need
/// </Summary>
/// <Param name = "people"> A list of people </param>
/// <Param name = "filter"> A filter </param>
/// <Returns> A filtered list </returns>
Static void DisplayPeople (string title, List <Person> people, FilterDelegate filter)
{
Console. WriteLine (title );
Foreach (Person p in people)
{
If (filter (p ))
{
Console. WriteLine ("{0}, {1} years old", p. Name, p. Age );
}
}
Console. Write ("\ n ");
}
// ============= FILTERS ====================================
Static bool IsChild (Person p)
{
Return p. Age <= 18;
}
Static bool IsAdult (Person p)
{
Return p. Age> = 18;
}
Static bool IsSenior (Person p)
{
Return p. Age> = 65;
}
// Extended FILTERS
Static bool IsOnetoThree (Person p)
{
Return p. Age> 10 & p. Age <30;
}
}
}
Output:
Children:
Jake, 12 years old
Cherry, 6 years old
Adult:
John, 41 years old
Jane, 69 years old
Jessie, 25 years old
Old Man:
Jane, 69 years old
10 ~ Between the ages of 30:
Jake, 12 years old
Jessie, 25 years old
It can be seen that the DisplayPeople method is easier to expand and maintain.
Multi-channel delegation:
Using System;
Delegate void MyDelegate (string s );
Class MyClass
{
Public static void Main ()
{
MyDelegate a, B, c, d;
// Delegate
A = s => {Console. WriteLine ("Hello, {0 }! ", S );};
// Delegate B
B = s => {Console. WriteLine ("Goodbye, {0 }! ", S );};
// Delegate c and merge a + B
C = a + B;
// Delegate d, call delegate c, remove
D = c-;
Console. WriteLine ("Call delegate :");
A ("");
Console. Write ("\ n ");
Console. WriteLine ("Call delegate B :");
B ("B ");
Console. Write ("\ n ");
Console. WriteLine ("Call delegate c :");
C ("C ");
Console. Write ("\ n ");
Console. WriteLine ("Call delegate d :");
D ("D ");
Console. ReadKey ();
}
}
Output:
Call delegate:
Hello,!
Call delegate B:
Goodbye, B!
Call delegate c:
Hello, C!
Goodbye, C!
Call delegate d:
Goodbye, D!
From Fly to The Moon