C# commissioned the (delegate, Action, Func, predicate)

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

  Delegation is a class, which defines the method type, the parameters can be methods as another method to transfer. The event is a special commission.

  1 entrusting statement

  (1). delegate

A statement of delegate we used to

    Delegate at least 0 parameters, up to 32 parameters, can be of no return value, you can also specify a return type.

    Example: public delegate int MethodtDelegate (int x, int y); said there are two parameters, and returns the int type.

  (2). Action

Action is without a return value of generic delegate.

   Action said no reference, entrust no return value

   Action<int,string> Said the incoming parameters int, commissioned by the string of no return

   Action<int,string,bool> Said the incoming parameters int, string, bool commissioned no return value

 Action<int,int,int,int> That has passed 4 int parameter, entrust no return value

   Action at least 0 parameters, up to 16 parameters, without a return value.

   An example:

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

    (3). Func

   Func is the return value of the generic delegate

   Func<int> No parameters, return values for the int client

   Func<object,string,int> A representation of the incoming parameters object, string returns int client

   Func<object,string,int> A representation of the incoming parameters object, string returns int client

   Func<T1,T2,,T3,int> A representation of the incoming parameters T1, T2, T3 (generic), the return value is int client

   Func at least 0 parameters, up to 16 parameters, according to the return value of generic return. Must have a return value, not void

An example:    

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

    (4) .predicate

   Predicate is the return type bool generic delegate

   predicate<int> Said the delegate returns bool for int incoming parameters

   Predicate has only one parameter, the return value is fixed at bool

   An example: public delegate bool Predicate<T> (T obj)

  

  The use of 2 commissioned

  (1)The use of.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)The use of.Action   

 static void Main(string[] args)
        {
            Test<string>(Action,"Hello World!");
            Test<int>(Action, 1000);
            Test<string>(p => { Console.WriteLine("{0}", p); }, "Hello World");//Using Lambda expressions to define the delegate
            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);
        }

  You can use the Action<T1, T2, T3, T4> commissioned passed as a parameter method, without explicitly declaring a custom delegate. The method signature package method must be defined and the corresponding principal. That is to say, the encapsulated method must have four through the values of the parameters passed to it, and cannot return a value. (in C#, this method must return void) usually, this method is used to perform an operation.

  (3)The use of.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). The use of predicate

  Generic delegate: define a set of conditions and method to determine whether the specified object to meet these conditions. This delegate is used by several methods of Array and List, to search for elements in the collection.

        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;
            }
        }

  Used with the Array.Find method of Predicate client search an array of Point structures. If the product X and Y field is greater than 100000, the Commission said the method ProductGT10 returns true. The Find method for each element of the array calls the delegate, stopping at the first point that meets the test condition.

  The 3 principal empty

  (1)Empty. That delegate methods in the class, in turn remove entrust reference.

Methods are as follows:

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

  (2)If clear method empties the entrusted not declared in the class, we can use the GetInvocationList query by reference, and then remove the.   

  Methods are as follows:

        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; } } }

  The 4 principal characteristics

  Trust is similar to the C++ function pointers, but they are type safe.

  Trust allows a method as a parameter to pass.

  Client can be used to define the callback method.

  Orders can be linked together; for example, to an event to call more than one method.

  Method need not exactly match the delegate signature.

  5.Summary:

    Delegate at least 0 parameters, up to 32 parameters, can be of no return value, you can also specify a return type

  Func can accept 0 to 16 incoming parameters, must have a return value

  Action can accept 0 or 16 arguments, no return value

  The Predicate can only accept an incoming parameters, return values of type bool


  Detailed reference:

       








Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Bruce at November 15, 2013 - 4:29 AM