天天看點

C#委托的介紹(delegate、Action、Func、predicate)

  委托是一個類,它定義了方法的類型,使得可以将方法當作另一個方法的參數來進行傳遞。事件是一種特殊的委托。

  1.委托的聲明

  (1). delegate

        delegate我們常用到的一種聲明

    Delegate至少0個參數,至多32個參數,可以無傳回值,也可以指定傳回值類型。

    例:public delegate int MethodtDelegate(int x, int y);表示有兩個參數,并傳回int型。

  (2). Action

       Action是無傳回值的泛型委托。

   Action 表示無參,無傳回值的委托

   Action<int,string> 表示有傳入參數int,string無傳回值的委托

   Action<int,string,bool> 表示有傳入參數int,string,bool無傳回值的委托

       Action<int,int,int,int> 表示有傳入4個int型參數,無傳回值的委托

   Action至少0個參數,至多16個參數,無傳回值。

   例:

public void Test<T>(Action<T> action,T p)
        {
            action(p);
        }      

    (3). Func

   Func是有傳回值的泛型委托

   Func<int> 表示無參,傳回值為int的委托

   Func<object,string,int> 表示傳入參數為object, string 傳回值為int的委托

   Func<T1,T2,,T3,int> 表示傳入參數為T1,T2,,T3(泛型)傳回值為int的委托

   Func至少0個參數,至多16個參數,根據傳回值泛型傳回。必須有傳回值,不可void

      例:   

public int Test<T1,T2>(Func<T1,T2,int>func,T1 a,T2 b)
        {
            return func(a, b);
        }      

    (4) .predicate

   predicate 是傳回bool型的泛型委托

   predicate<int> 表示傳入參數為int 傳回bool的委托

   Predicate有且隻有一個參數,傳回值固定為bool

   例:public delegate bool Predicate<T> (T obj)

  

  2.委托的使用

  (1).Delegate的使用  

public delegate int MethodDelegate(int x, int y);
        private static MethodDelegate method;
        static void Main(string[] args)
        {
            method = new MethodDelegate(Add);
            Console.WriteLine(method(10,20));
            Console.ReadKey();
        }

        private static int Add(int x, int y)
        {
            return x + y;
        }      

  (2).Action的使用   

static void Main(string[] args)
        {
            Test<string>(Action,"Hello World!");
            Test<int>(Action, 1000);
            Test<string>(p => { Console.WriteLine("{0}", p); }, "Hello World");//使用Lambda表達式定義委托
            Console.ReadKey();
        }
        public static void Test<T>(Action<T> action, T p)
        {
            action(p);
        }
        private static void Action(string s)
        {
            Console.WriteLine(s);
        }
        private static void Action(int s)
        {
            Console.WriteLine(s);
        }      

  可以使用 Action<T1, T2, T3, T4> 委托以參數形式傳遞方法,而不用顯式聲明自定義的委托。 封裝的方法必須與此委托定義的方法簽名相對應。 也就是說,封裝的方法必須具有四個均通過值傳遞給它的參數,并且不能傳回值。 (在 C# 中,該方法必須傳回 void)通常,這種方法用于執行某個操作。

  (3).Func的使用

static void Main(string[] args)
        {
            Console.WriteLine(Test<int,int>(Fun,100,200));
            Console.ReadKey();
        }
        public static int Test<T1, T2>(Func<T1, T2, int> func, T1 a, T2 b)
        {
            return func(a, b);
        }
        private static int Fun(int a, int b)
        {
            return a + b;
        }      

  (4). predicate的使用

  泛型委托:表示定義一組條件并确定指定對象是否符合這些條件的方法。此委托由 Array 和 List 類的幾種方法使用,用于在集合中搜尋元素。

static void Main(string[] args)
        {
            Point[] points = { new Point(100, 200), 
            new Point(150, 250), new Point(250, 375), 
            new Point(275, 395), new Point(295, 450) };
            Point first = Array.Find(points, ProductGT10);
            Console.WriteLine("Found: X = {0}, Y = {1}", first.X, first.Y);
            Console.ReadKey();
        }
        private static bool ProductGT10(Point p)
        {
            if (p.X * p.Y > 100000)
            {
                return true;
            }
            else
            {
                return false;
            }
        }      

  使用帶有 Array.Find 方法的 Predicate 委托搜尋 Point 結構的數組。如果 X 和 Y 字段的乘積大于 100,000,此委托表示的方法 ProductGT10 将傳回 true。Find 方法為數組的每個元素調用此委托,在符合測試條件的第一個點處停止。

  3.委托的清空

  (1).在類中申明清空委托方法,依次循環去除委托引用。

         方法如下:

    public MethodDelegate OnDelegate;                
        public void ClearDelegate()        
        {             
            while (this.OnDelegate != null) 
            {                 
                this.OnDelegate -= this.OnDelegate;  
            }        
        }       

  (2).如果在類中沒有申明清空委托的方法,我們可以利用GetInvocationList查詢出委托引用,然後進行去除。  

  方法如下:

public MethodDelegate OnDelegate; 
     static void Main(string[] args)
        {
            Program test = new Program();

            if (test.OnDelegate != null) 
            { 
                System.Delegate[] dels = test.OnDelegate.GetInvocationList(); 
                for (int i = 0; i < dels.Length; i++) 
                {
                    test.OnDelegate -= dels[i] as MethodDelegate;
                }
            }
        }      

  4.委托的特點

  委托類似于 C++ 函數指針,但它們是類型安全的。

  委托允許将方法作為參數進行傳遞。

  委托可用于定義回調方法。

  委托可以連結在一起;例如,可以對一個事件調用多個方法。

  方法不必與委托簽名完全比對。

  5.總結:

    Delegate至少0個參數,至多32個參數,可以無傳回值,也可以指定傳回值類型

  Func可以接受0個至16個傳入參數,必須具有傳回值

  Action可以接受0個至16個傳入參數,無傳回值

  Predicate隻能接受一個傳入參數,傳回值為bool類型

  詳細參考:http://www.fengfly.com/plus/view-209140-1.html

       http://www.cnblogs.com/foolishfox/archive/2010/09/16/1827964.html

繼續閱讀