Method as a method parameters
We all know that a method parameters which always contains value type and reference type.But it is so abstract to transmit a method to method.This paper,I will give the answer.
Delegate:
Delegate is a method pointer.It can constraint one type of method rely on its method parameters and return type.If you want to make the delegate point to a method.The method must have the same return type and the same parmaters type which ordered as the delegate parmaters.
The follow code show the delegate as a method parameters!
using System;
namespace CallBackDelegate
{
public delegate string ProcessDelegate(string strone);
class Program
{
static void Main(string[] args)
{
Test test = new Test();
string firstTest = test.Process("wulong", test.ProcessTestOne);
string secondTest = test.Process("wulong",test.ProcessTestTwo);
Console.WriteLine(firstTest);
Console.WriteLine(secondTest);
Console.Read();
}
}
public class Test
{
public string Process(string strone, ProcessDelegate process)
{
return process(strone);
}
public string ProcessTestOne(string strone)
{
return "The first Test is " + strone;
}
public string ProcessTestTwo(string strone)
{
return "The second Test is " + strone;
}
}
}