Delegation, generic delegation, Func & lt; T & gt; and Action & lt; T & gt;, funcaction

Source: Internet
Author: User

Delegation, generic delegation, Func <T> and Action <T>, funcaction

The general idea of using delegation to do some things is:

1. Define and declare a delegate that specifies the input parameter and output type.
2. Write several methods that conform to the definition of the delegate.
3. assign a list of methods to the Delegate
4. Execute Delegation

 

    internal delegate int MyDelegate();
    class Program
    {
        static void Main(string[] args)
        {
            MyDelegate d = ReturnOne;
            d += ReturnTwo;
            foreach (int i in GetAllReturnVals(d))
            {
                Console.WriteLine(i);
            }
            Console.ReadKey();
        }
        static IEnumerable<int> GetAllReturnVals(MyDelegate myDelegate)
        {
            foreach (MyDelegate del in myDelegate.GetInvocationList())
            {
                yield return del();
            }
        }
        static int ReturnOne()
        {
            return 1;
        }
        static int ReturnTwo()
        {
            return 2;
        }
    }

 

In the preceding example, the return type of the delegate is int. What if the return type is generic? You only need to make the above GetAllReturnVals method target the generic type.

 

   internal delegate T MyDelegate<T>();
    class Program
    {
        static void Main(string[] args)
        {
            MyDelegate<int> d = ReturnOne;
            d += ReturnTwo;
            foreach (int i in GetAllReturnVals(d))
            {
                Console.WriteLine(i);
            }
            MyDelegate<string> d1 = ReturnA;
            d1 += ReturnB;
            foreach (string s in GetAllReturnVals(d1))
            {
                Console.WriteLine(s);
            }
            Console.ReadKey();
        }
// Return the execution results of all delegate Methods
        static IEnumerable<T> GetAllReturnVals<T>(MyDelegate<T> myDelegate)
        {
            foreach (MyDelegate<T> del in myDelegate.GetInvocationList())
            {
                yield return del();
            }
        }
        static int ReturnOne()
        {
            return 1;
        }
        static int ReturnTwo()
        {
            return 2;
        }
        static string ReturnA()
        {
            return "A";
        }
        static string ReturnB()
        {
            return "B";
        }
    }

 

However,. NET has also prepared a more "lazy" Method for us, that is, for the generic delegate Func <T>. In this case, do not declare the delegate in advance. In the <T> <> generic list of Func, the last one is the return type, the other is the input type, and the Func <T> contains a dozen or more overload values.

 

   class Program
    {
        static void Main(string[] args)
        {
            Func<int> d = ReturnOne;
            d += ReturnTwo;
            foreach (int i in GetAllReturnVals(d))
            {
                Console.WriteLine(i);
            }
            Func<string> d1 = ReturnA;
            d1 += ReturnB;
            foreach (string s in GetAllReturnVals(d1))
            {
                Console.WriteLine(s);
            }
            Console.ReadKey();
        }
// Return the execution results of all delegate Methods
        static IEnumerable<T> GetAllReturnVals<T>(Func<T> myDelegate)
        {
            foreach (Func<T> del in myDelegate.GetInvocationList())
            {
                yield return del();
            }
        }
        static int ReturnOne()
        {
            return 1;
        }
        static int ReturnTwo()
        {
            return 2;
        }
        static string ReturnA()
        {
            return "A";
        }
        static string ReturnB()
        {
            return "B";
        }
    }

 

The above generic delegate Func <T> has both input parameters and return types. If the return type is void, you can use Action <T>, of course, Action <T> also has more than a dozen reloads.

 

   class Program
    {
        static void Main(string[] args)
        {
            Action<int> a1 = ReturnInt;
            a1(1);
            Action<string> a2 = ReturnStr;
            a2("hello");
   
            Console.ReadKey();
        }
        static void ReturnStr(string s)
        {
            Console.WriteLine(s);
        }
        static void ReturnInt(int a) 
        {
            Console.WriteLine(a);
        }
    }

 

Conclusion: Func <T> and Action <T> are generic Delegate's "syntactic sugar". If the return type is void, use Action <T>, otherwise, use Func <T>.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.