C# 7.1 .Net Framwork4.7 VS2017环境下,方法的引用与调用

方法的调用比较好理解,就是给方法传递实参,执行方法代码。

方法引用涉及委托,委托签名与其引用的方法必须一致。以下demo说明方法调用与引用在写程序时的区别:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 方法的引用与调用
{
    /// <summary>
    /// 定义一个委托,签名:参数两个double,返回一个double
    /// </summary>
    /// <param name="d1"></param>
    /// <param name="d2"></param>
    /// <returns></returns>
    public delegate double GetResult(double d1, double d2);
    class Program
    {
        static void Main(string[] args)
        {
            //方法的调用
            double num1 = 10;
            double num2 = 15;
            double result_CallMethod = GetSum(GetSum(num1, num2), 25);//result_CallMethod = 50

            //方法的引用
            double result_ReferenceMethod = Calculate(GetSum, num1, num2);//result_ReferenceMethod = 25
        }

        static double GetSum(double a1, double a2) => a1 + a2;

        static double Calculate(GetResult getresult,double d1, double d2) => getresult(d1, d2);
    }
}

so simple and clear,,,

上一篇:【论文阅读】Multi-level Semantic Feature Augmentation for One-shot Learning


下一篇:算法魅力之牛叉的前缀和