- 委托是嘛?
委托是一個類型安全的對象,它指向程序中另一個以后會被調用的方法(或多個方法)。通俗的說,委托是一個可以引用方法的對象,當創建一個委托,也就創建一個引用方法的對象,進而就可以調用那個方法,即委托可以調用它所指的方法。
- 如何使用委托?
1、定義委托類型
[訪問修飾符]delegate 返回類型 委托名(形參);
2、聲明委托對象
委托名 委托實例名;
3、創建委托對象(確定與哪些方法進行綁定)
委托實例名=new 委托名(某個類的方法)
4、使用委托調用方法
委托實例名(實參)
- 委托注意事項:
1、委托和方法必須具有相同的參數。
2、委托可以調用多個方法,即一個委托對象可以維護一個可調用方法的列表而不是單獨的一個方法,稱為多路廣播(多播)。
3、使用+=和-=運算實現方法的增加和減少
- 示例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Delegate;
namespace Delegate
{
public delegate int Call(int num1, int num2);//第一步:定義委托類型
class SimpleMath
{
// 乘法方法
public int Multiply(int num1, int num2)
{
return num1 * num2;
}
// 除法方法
public int Divide(int num1, int num2)
{
return num1 / num2;
}
}
}
class Test
{
static void Main(string[] args)
{
Call objCall;//第二步:聲明委托對象
// Math 類的對象
SimpleMath objMath = new SimpleMath();
// 第三步:創建委托對象,將方法與委托關聯起來
objCall = new Call(objMath.Multiply);
Call objCall1 = new Call(objMath.Divide);
objCall += objCall1;//向委托增加一個方法
//objCall -= objCall1;//向委托減去一個方法
// 調用委托實例,先執行objMath.Multiply,然后執行objMath.Divide
int result = objCall(5, 3);
System.Console.WriteLine("結果為 {0}", result);
Console.ReadKey();
}
}
寫法:
1、委托 委托名=new 委托(會調用的方法名); 委托名(參數);
2、委托 委托名 =會調用的方法名; 委托名(參數);
3、匿名方法
委托 委托名=delegate(參數){會調用的方法體};委托名(參數);
4、拉姆達表達式
委托 委托名=((參數1,。。參數n)=>{會調用的方法體});委托名(參數);
5、用Action<T>和Func<T>,第一個無返回值
Func<參數1, 參數2, 返回值> 委托名= ((參數1,參數2) => {帶返回值的方法體 });返回值=委托名(參數1,參數2);
貼代碼:
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
@ delegate ;
namespace
@ delegate
{
public
delegate int Call( int num1, int num2);
class
SimpleMath
{
// 乘法方法
public
static int Multiply( int num1, int num2)
{
return
num1 * num2;
}
// 除法方法
public
int Divide( int num1, int num2)
{
return
num1 / num2;
}
}
class
Test
{
static
void Main( string [] args)
{
//--------------------第一種寫法------------------------//
//Call objCall = new Call(SimpleMath.Multiply);
//Call objCall1 = new Call(new SimpleMath().Divide);
//--------------------第二種寫法------------------------//
//Call objCall = SimpleMath.Multiply;
//Call objCall1 = new SimpleMath().Divide;
//--------------------第三種寫法------------------------//
//Call objCall = delegate(int a, int b)
//{
// return a * b;
//};
//Call objCall1 = delegate(int a, int b)
//{
// return a / b;
//};
//--------------------第四種寫法------------------------//
//Call objCall =((int a,int b)=> { return a*b;});
//Call objCall1 = ((int a, int b) => { return a / b; });
//--------------------第五種寫法------------------------//
Func<
int
,
int
,
int
> objCall = ((a, b) => {
return
a * b; });
Func<
int
,
int
,
int
> objCall1 = ((a, b) => {
return
a / b; });
Action<
int
,
int
> ob = ((a, b) => { Console.WriteLine(a * b); });
ob(5, 3);
//----------------------------------------------------//
int
result = objCall(5, 3);
int
result1 = objCall1(5, 3);
System.Console.WriteLine(
"結果1為 {0},結果2為{1}"
, result,result1);
Console.ReadKey();
}
}
}
|
一、委托調用方式
1. 最原始版本:
delegate string PlusStringHandle(string x, string y); class Program { static void Main(string[] args) { PlusStringHandle pHandle = new PlusStringHandle(plusString); Console.WriteLine(pHandle("abc", "edf")); Console.Read(); } static string plusString(string x, string y) { return x + y; } }
2. 原始匿名函數版:去掉“plusString”方法,改為
PlusStringHandle pHandle = new PlusStringHandle(delegate(string x, string y) { return x + y; }); Console.WriteLine(pHandle("abc", "edf"));
3. 使用Lambda(C#3.0+),繼續去掉“plusString”方法(以下代碼均不再需要該方法)
PlusStringHandle pHandle = (string x, string y) => { return x + y; }; Console.WriteLine(pHandle("abc", "edf"));
還有更甚的寫法(省去參數類型)
PlusStringHandle pHandle = (x, y) =>
{
return x + y; }; Console.WriteLine(pHandle("abc", "edf"));
如果只有一個參數
delegate void WriteStringHandle(string str); static void Main(string[] args) { //如果只有一個參數 WriteStringHandle handle = p => Console.WriteLine(p); handle("lisi"); Console.Read(); }
二、委托聲明方式
1. 原始聲明方式見上述Demo
2. 直接使用.NET Framework定義好的泛型委托 Func 與 Action ,從而省卻每次都進行的委托聲明。
static void Main(string[] args) { WritePrint<int>(p => Console.WriteLine("{0}是一個整數", p), 10); Console.Read(); } static void WritePrint<T>(Action<T> action, T t) { Console.WriteLine("類型為:{0},值為:{1}", t.GetType(), t); action(t); }
3. 再加上個擴展方法,就能搞成所謂的“鏈式編程”啦。
class Program { static void Main(string[] args) { string str = "所有童鞋:".plusString(p => p = p + " girl: lisi、lili\r\n").plusString(p => p + "boy: wangwu") ; Console.WriteLine(str); Console.Read(); } } static class Extentions { public static string plusString<TParam>(this TParam source, Func<TParam, string> func) { Console.WriteLine("字符串相加前原值為:{0}。。。。。。", source); return func(source); } }
看這個代碼是不是和我們平時寫的"list.Where(p => p.Age > 18)"很像呢?沒錯Where等方法就是使用類似的方式來實現的。
好了,我總結完了,如有遺漏,還望補上,臣不盡感激。