第22节:C# Func委托

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/haibing_zhao/article/details/81516663

代码:

/******************************************
 * 
 * Func委托
 * 
 * 特点:
 * 
 *  1.必须有一个返回值
 *  2.Func泛型委托,尖括号中,最后一个参数为返回类型。
 *  3.Func泛型委托,如果尖括号中只有一个参数,则表示返回类型。
 * 
 * ****************************************/

using System;
using System.Collections.Generic;
using System.Text;

namespace 学习系统内置委托
{
    class Demo3
    {
        Func<string> funHandle;                             //无参数
        Func<string, int> funHandlerWihtPara;               //有参数

        public Demo3()
        {
            funHandle += InvokeMethod;
            funHandlerWihtPara += InvokeMethod1;
        }

        public string InvokeMethod()
        {
            Console.WriteLine("无参方法,有string类型返回数值");
            return "你好";
        }

        public int InvokeMethod1(string num)
        {
            Console.WriteLine("有参方法,返回类型是int类型");
            return 0;
        }
        public void DisplayInfo()
        {
            //调用无参数的委托
            funHandle.Invoke();
            //调用有一个参数的委托
            funHandlerWihtPara("你好");
        }
        static void Main(string[] args)
        {
            Demo3 obj = new Demo3();
            obj.DisplayInfo();
            Console.ReadKey();
        }
    }
}
 

猜你喜欢

转载自blog.csdn.net/haibing_zhao/article/details/81516663