class Program
{
public delegate void mydelegate();
public event mydelegate myevent;
public delegate void mydelegate(int a);//帶參
public event mydelegate myevent;
static void Main(string[] args)
{
//多種委托
Action a = Test1;
a += Test2;
a += Test3;
Delegate[] d=a.GetInvocationList();
foreach (Delegate item in d)
{
Console.WriteLine(item.DynamicInvoke());
}
Func<string> b = Test4;
b += Test5;
b += Test6;
Delegate[] d1 = b.GetInvocationList();
foreach (var item in d1)
{
Console.WriteLine(item.DynamicInvoke());
}
// 匿名方法
Action
Action a = delegate()
{
Console.WriteLine("Hello World!");
};//匿名方法注意分號;
a();
Action<int> b = delegate(int aa)
{
Console.WriteLine("{0}",aa);
};
b(88);
Function
Func<string> f = delegate()
{
Console.WriteLine("123");
return "123";
};
f();
Func<string, string> ff = delegate(string f1)
{
Console.WriteLine("{0}",f1);
return "123";
};
ff("8888");
string a111 = "1";
string a222 = "";
string c = Show("1", out a222);zai fang
Console.WriteLine(c);
Console.WriteLine(a222);
Func<bool> f = delegate()
{
return true;
};
bool b = f();
Console.WriteLine(b);
Func <int,string,bool> f1=delegate(int a,string str){
Console.WriteLine("{0} {1}",a,str);
return false;
};
bool bb = f1(1,"88");
Console.WriteLine(bb);
// 事件
Program p = new Program();
p.myevent = Test1;
p.myevent();
Program p = new Program();
p.myevent = Test2;
p.myevent(8);
Console.ReadKey();
}
static void Test1()
{
Console.WriteLine("Test1");
}
static void Test2(int a)
{
Console.WriteLine("Test2 {0}", a);
}
static void Test3()
{
Console.WriteLine("Test3");
}
static string Test4()
{
Console.WriteLine("Test1-f");
return "1";
}
static string Test5()
{
Console.WriteLine("Test2-f");
return "2";
}
static string Test6()
{
Console.WriteLine("Test3-f");
return "3";
}
public static string Show(string a, out string b)
{
b = "123";
return a;
}
}
unity游戲開發(fā)-C#語言基礎(chǔ)篇(多種委托)
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。