[Turn] C # delegate (Func, Action)

Original Address:
https://cloud.tencent.com/developer/article/1335104

1, Func usage (encapsulation method, pass parameters, return value)

Func<in T1, in T2, ..., out TResult> (T1, T2, ...)  

A packaging method which has (0/1/2/3 ... 16) parameters , and returns the value of the type specified by the TResult parameter.

 

public  static  void Main ()
        {
            @ Method a: Func equivalent system built delegate 
            Func < int , int , String > Method = the Calculate;

            @ Method two: Lambda implemented method call, simpler 
            Func < int , int , String > method - 1 = (X, Y) =>
            {
                int val = x + y;
                return string.Format("the calculate {0} plus {1} result is: {2}", x, y, val);
            };

            Console.WriteLine(method(3, 5));
            Console.WriteLine(method_1(10, 18));
            Console.ReadLine();
        }


        public static string Calculate(int x, int y)
        {
            int val = x + y;
            return string.Format("the calculate {0} plus {1} result is: {2}", x, y, val);
        }

 

2, Action Usage (a method of packaging, passing parameters and returns no value)

 Action<T1, T2, T3, ...>(t1, t2, t3 ...)

A packaging method which incoming (0/1/2 ...) parameters, and returns no values.

 public  static  void Main ()
        {
            Method_First("Hi, Here!");
            Method_First("Hi, There!");

            Console.ReadLine();
        }

        private static void Method_First(string y)
        {
            Action<string> method;
            method = x => { Console.WriteLine("the input message is: {0}", x); };
            method(y);
        }

        private static void Method_Sec(string y)
        {
            Action<string> method = x => { Console.WriteLine("the input message is : {0}", x); };
            method(y);
        }

 

3. Use of delegate

Told commissioned two different situations, then when to use it commissioned?

According to official documents, in the following situations, please use the commission:

  • When the event design pattern.
  • When the packaging method preferably static.
  • Other attributes of the object when the caller does not need to access the implementation of the method, a method or interface.
  • We need to facilitate the combination.
  • When a plurality of the class may need to implement the method.

4. Use the Task delegation

Task represents an asynchronous operation.

public  static  void Main ()
        {
            // starting method. 1 
            the Task T = Task.Run (() => 
            {
                Thread.Sleep(1000);
                Console.WriteLine("First task finished time is:{0}", DateTime.Now.ToString());
            });


            // 方法2
            Task t_2 = Task.Factory.StartNew(() => {
                Thread.Sleep(2000);
                Console.WriteLine("second task finished time is:{0}", DateTime.Now.ToString());
            });


            // method. 3 
            the Action Action = () =>
            {
                Thread.Sleep(3000);
                Console.WriteLine("third task finished time is:{0}", DateTime.Now.ToString());
            };
            Task.Factory.StartNew(action).ContinueWith(thirdTask =>
            {
                if (thirdTask.IsCompleted)
                {
                    Console.WriteLine("the third task has finished");
                }
                else if (thirdTask.IsFaulted)
                {
                    Console.WriteLine(thirdTask.Exception);
                }
            });

            Console.WriteLine("main thread has end:{0}",DateTime.Now.ToString() );

            Console.ReadKey();
        }

Results are as follows: 

main thread has end:2018-03-04 22:03:39

First task finished time is:2018-03-04 22:03:40

second task finished time is:2018-03-04 22:03:41

third task finished time is:2018-03-04 22:03:42

the third task has finished

 

Guess you like

Origin www.cnblogs.com/louiszh/p/12665828.html