Basic usage of C#__Action and Func delegates

// kind 

    class DelegateC
    {
        // Action,内置的委托类型,引用了一个void返回值类型的方法,T表示方法参数
        public static void AText1()
        {
            Console.WriteLine("Atext1");
        }
        public static void AText2(int x)
        {
            Console.WriteLine("Atext2" + x);
        }
        public static void AText3(int x, double y)
        {
            Console.WriteLine("Atext3" + x + y);
        }

        // Func委托,需要指定返回类型
        public static string FText1()
        {
            return "jiangdong";
        }
        public static string FText2(int x, double y)
        {
            return "jiangdong" + x + y;
        }

        public void Text()
        {
            Action method = AText1;
            method(); // Atext1

            Action<int> method2 = AText2;
            method2(100); // Atext2100

            Action<int, double> method3 = AText3;
            method3(10, 1.12); // Atext3101.12

            Func<string> f = FText1;
            Console.WriteLine(f()); // jiangdong

            Func<int, double, string> f2 = FText2;
            Console.WriteLine(f2(10,10)); // jiangdong1010

        }
    }

 // transfer

            // 委托delegateC
            // Action,内置的委托类型,引用了一个void返回值类型的方法
            // Func委托,需要指定返回类型
            DelegateC delegaC = new DelegateC();
            delegaC.Text();

Guess you like

Origin blog.csdn.net/qq_57233919/article/details/132213412