Delegate
簡單的說就是能夠 "傳遞方法"程式碼:
using System;
namespace SimpleEvent
{
//建照標準函式 型態參數一樣才能註冊
public delegate string DelegatePrint(string Name);
class Suzanne
{
//必須和Delegate型態參數一模一樣
public static string print01(string formDelegateStr )
{
Console.WriteLine("print 01 " + formDelegateStr);
return "Done";
}
//必須和Delegate型態參數一模一樣
public static string print02(string formDelegateStr)
{
Console.WriteLine("print 02 " + formDelegateStr );
return "Done";
}
//必須和Delegate型態參數一模一樣
public static string print03(string formDelegateStr)
{
Console.WriteLine("print 03 " + formDelegateStr);
return "Done";
}
public static void Main()
{
//用print01來註冊 delegatePrint
DelegatePrint delegatePrint = new DelegatePrint( print01 );
//用print02來註冊 delegatePrint
delegatePrint += print02;
//這時候 delegatePrint => print01() , print02()
Console.WriteLine(delegatePrint("FromDelegateStr"));
//再增加一個 print03 //總共有 print01 , print02 , print03 三個函式了~
delegatePrint += print03;
//delegatePrint() => print01() , print02() , print03()
//因為剛剛註冊了三個函式
Console.WriteLine(delegatePrint("FromDelegateStr"));
Console.ReadLine();
}
}
}
namespace SimpleEvent
{
//建照標準函式 型態參數一樣才能註冊
public delegate string DelegatePrint(string Name);
class Suzanne
{
//必須和Delegate型態參數一模一樣
public static string print01(string formDelegateStr )
{
Console.WriteLine("print 01 " + formDelegateStr);
return "Done";
}
//必須和Delegate型態參數一模一樣
public static string print02(string formDelegateStr)
{
Console.WriteLine("print 02 " + formDelegateStr );
return "Done";
}
//必須和Delegate型態參數一模一樣
public static string print03(string formDelegateStr)
{
Console.WriteLine("print 03 " + formDelegateStr);
return "Done";
}
public static void Main()
{
//用print01來註冊 delegatePrint
DelegatePrint delegatePrint = new DelegatePrint( print01 );
//用print02來註冊 delegatePrint
delegatePrint += print02;
//這時候 delegatePrint => print01() , print02()
Console.WriteLine(delegatePrint("FromDelegateStr"));
//再增加一個 print03 //總共有 print01 , print02 , print03 三個函式了~
delegatePrint += print03;
//delegatePrint() => print01() , print02() , print03()
//因為剛剛註冊了三個函式
Console.WriteLine(delegatePrint("FromDelegateStr"));
Console.ReadLine();
}
}
}
沒有留言:
張貼留言