c# 泛型委托 练习

自定义 无参无返回值 委托

using System;

namespace ConsoleApp5
{
	// 声明委托,无参数,无返回值
    delegate void MyDele();
    class Program
    {
        static void Main(string[] args)
        {
            // dele1这个变量引用着一个MyDele类型的实例,
            // 这个实例里"包裹"着M1这个方法
            MyDele dele1 = new MyDele(M1);

            /* Student student = new Student();
             dele1 += student.SayHello;*/
            // 简化
            dele1 += (new Student()).SayHello;

            //输出
            //M1 is Called
            //Hello C#
            dele1.Invoke();
        }

        static void M1()
        {
            Console.WriteLine("M1 is Called");
        }

        class Student
        {
            public void SayHello()
            {
                Console.WriteLine("Hello C#");
            }
        }
    }
}

自定义 有参有返回值 委托

using System;

namespace ConsoleApp5
{
	// 声明委托
    delegate int MyDele(int a, int b);
    class Program
    {
        static void Main(string[] args)
        {
            //MyDele myDele = new MyDele(Program.Add);
            MyDele myDele = new MyDele(Add);
            int result = myDele(100, 200);
            // 300
            Console.WriteLine(result);
        }

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

自定义 泛型 委托

using System;

namespace ConsoleApp5
{
	// 声明泛型委托
    delegate T MyDele<T>(T a, T b);
    class Program
    {
        static void Main(string[] args)
        {
            MyDele<int> deleAdd = new MyDele<int>(Add);
            int addResult = deleAdd(100, 200);
            // 300
            Console.WriteLine(addResult);

            MyDele<double> deleMul = new MyDele<double>(Mul);
            double mulResult = deleMul(100, 200);
            // 20000
            Console.WriteLine(mulResult);

            // 委托是一种特殊的类
            // True
            Console.WriteLine(deleAdd.GetType().IsClass);

        }

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

        static double Mul(double x, double y)
        {
            return x * y;
        }
    }
}

c# 类库 预先声明好的泛型委托

没有返回值类型 Action

using System;

namespace ConsoleApp5
{
    class Program
    {
        static void Main(string[] args)
        {
            // 无参
            Action action = new Action(M1);
            //输出
            // M1 is called
            action.Invoke();

            // 1个参数
            Action<string> action2 = new Action<string>(SayHello);
            //输出
            // Hello,ll!
            action2("ll");

            // 2个参数
            Action<string, int> action3 = new Action<string, int>(sayCountHello);
            //输出
            //Hello,LL
            //Hello,LL
            //Hello, LL
            action3("LL", 3);
        }

        static void M1()
        {
            Console.WriteLine("M1 is called");
        }

        static void SayHello(string name)
        {
            Console.WriteLine($"Hello,{name}!");
        }

        static void sayCountHello(string name, int count)
        {
            for (int i = 0; i < count; i++)
            {
                Console.WriteLine($"Hello,{name}");
            }
        }
    }
}

有返回值类型 Func

using System;

namespace ConsoleApp5
{
    class Program
    {
        static void Main(string[] args)
        {
            Func<int, int, int> func = new Func<int, int, int>(Add);
            int result = func(100, 200);
            // 300
            Console.WriteLine(result);

            Func<double, double, double> func2 =
                new Func<double, double, double>(Mul);
            double resultMul = func2(10, 20);
            // 200
            Console.WriteLine(resultMul);
        }

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

        static double Mul(double x, double y)
        {
            return x * y;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值