Commissioned concept author: Liang Binyu Excerpted from "C# Beginner Classroom" will be published soon
[email protected]
http://www.cnblogs.com/BeginnerClassroom
Sometimes we need to use a function as a parameter of another function, then we need to use the delegate mechanism. Delegation is a difficult concept to explain clearly. The author thought hard for several days and finally came up with an ingenious example.
Next, we design a circus performance function RunCircus(). Its first parameter is a function representing an animal. Whatever animal is passed to it, the performance of that animal will be performed. Please create a new project named "Delegate" and add the following code.
Give it a try: Defining delegates
//"C# Beginner Classroom"
//Function: dog show
static void DogAct(string name)
{
Console.WriteLine("Hello,I am " + name + "!");
Console.WriteLine(@"
.----.
_.'__ `.
.--(#)(##)---/#
.' @ /###
: , #####
`-..__.-' _.-###/
`;_: `''
.''''''''`.
/,Snoopy,
// COOL! \
`-._______.-'
___`. | .'___
(______|______)");
}
//Function: cat show
static void CatAct(string name)
{
Console.WriteLine("Hello,I am " + name + "!");
Console.WriteLine(@"
.-. __ _ .-.
| ` / |
/ '.()--
| '._/
_| O _ O |_
= '-' /=
'-._____.-'
/`/___/`
//o o/
(_| |_)
|____,____|
(____|____)");
}
//Function: lion show
static void LionAct(string name)
{
Console.WriteLine("Hello,I am " + name + "!");
Console.WriteLine(@"
,%%%%%%%%,
,%%/%%%%/%%
,%%%c "" J/%%%
%. %%%%/ o o %%%
`%%. %%%% _ |%%%
`%% `%%%%(__Y__)%%'
// ;%%%%`-/%%%'
(( / `%%%%%%%'
\ .' |
\ / | |
\/ ) | |
/_ | |__
(____________)))))))");
}
//define delegate
delegate void AnimalAct(string name);
//Function: circus performance (the first parameter is the AnimalAct type delegate)
static void RunCircus(AnimalAct animalAct, string name)
{
animalAct(name);
}
static void Main(string[] args)
{
//Convert function DogAct() to AnimalAct type delegate
AnimalAct deleDogAct = new AnimalAct(DogAct);
//Pass the delegate deleDogAct to the function RunCircus()
RunCircus(deleDogAct, "Snoopy");
//Convert the function CatAct() into an AnimalAct type delegate and pass it to the function RunCircus()
RunCircus(new AnimalAct(CatAct), " Kitty ");
}
The running results are as follows:
"C# Beginner Class"
The first parameter of the function RunCircus(AnimalAct animalAct, string name) is actually a function representing an animal. What kind of function is passed to it will use what kind of animal to perform. But in order to perform strict type checking, we cannot directly pass the name of the function to it, but should first define a delegate (Dlelegate), that is, define a "function type".
The delegate is declared with the keyword delegate. The above statement defines a delegate (function type) called AnimalAct, which clearly specifies the delegate's parameter type and return value type. When we need to use a function as a parameter, we must first convert it into a delegate instance.
This delegate instance is then passed as a parameter to the function that calls it.
It can be seen that the delegate instance deleDogAct is actually an alias of the function DogAct(). Obviously, the delegate and the delegated function should have the same parameter type and return type.
In our program, the circus always calls the function RunCircus() to perform. When defining the function RunCircus(), we do not know or care which function the delegate passed to it represents. This function is not materialized until the RunCircus() function is called and the actual parameters are passed to it. Whatever actual parameters are passed to it, the performance will be performed. If you pass it the deleDogAct delegation, the circus will perform a dog performance; if you pass it a deleCatAct delegation, the circus will perform a cat performance; if you pass it a deleLionAct delegation, the circus will perform a cat performance. The circus performed a lion show. Therefore, functions that take delegates as parameters have certain generality.