Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 方法或委托或函数_C#_Methods_Lambda_Delegates_Func - Fatal编程技术网

C# 方法或委托或函数

C# 方法或委托或函数,c#,methods,lambda,delegates,func,C#,Methods,Lambda,Delegates,Func,我已经读过关于这个问题的帖子,但有人能帮我把它说清楚吗? 因此,我目前正在探索代理,显然有一些用途,例如事件。 但是,对于简单的操作,例如乘以2个数字,首选什么? 过度使用代理是一种不好的做法吗? 下面是我一直在看的 namespace ConsoleApplication32 { public delegate int Function2(int x, int y); class Program { static void Main(string[] args) {

我已经读过关于这个问题的帖子,但有人能帮我把它说清楚吗? 因此,我目前正在探索代理,显然有一些用途,例如事件。 但是,对于简单的操作,例如乘以2个数字,首选什么? 过度使用代理是一种不好的做法吗? 下面是我一直在看的

namespace ConsoleApplication32
{
public delegate int Function2(int x, int y);

class Program
{
    static void Main(string[] args)
    {          
        Console.WriteLine("Method");
        Stopwatch sw2 = Stopwatch.StartNew();
        Console.WriteLine("Method: " + Function(5, 5));
        sw2.Stop();
        Console.WriteLine(sw2.Elapsed);

        Console.WriteLine("Delegate");
        Stopwatch sw4 = Stopwatch.StartNew();
        Function2 g = Function;
        Console.WriteLine("Delegate: " + g(5, 5));
        sw4.Stop();
        Console.WriteLine(sw4.Elapsed);

        Console.WriteLine("Anonymous");
        Stopwatch sw3 = Stopwatch.StartNew();
        Function2 f = delegate(int a, int b) { return a * b; };
        Console.WriteLine("Anonymous: " + f(5, 5));
        sw3.Stop();
        Console.WriteLine(sw3.Elapsed);

        Console.WriteLine("Lambda");
        Stopwatch sw5 = Stopwatch.StartNew();
        Function2 h = (x, y) => { return x * y; };
        Console.WriteLine("Lambda: " + h(5, 5));
        sw5.Stop();
        Console.WriteLine(sw5.Elapsed);

        Console.WriteLine("Func Delegate");
        Stopwatch sw = Stopwatch.StartNew();
        Func<int, int, int> function = (x, y) => x * y;
        Console.WriteLine("Func: " + function(5, 5));
        sw.Stop();
        Console.WriteLine(sw.Elapsed);
    }

    static int Function(int x, int y)
    {
        return x * y;
    }
}
}
命名空间控制台应用程序32
{
公共代表内部职能2(内部x,内部y);
班级计划
{
静态void Main(字符串[]参数)
{          
控制台写入线(“方法”);
Stopwatch sw2=Stopwatch.StartNew();
Console.WriteLine(“方法:”+函数(5,5));
sw2.Stop();
Console.WriteLine(sw2.Appeased);
控制台。写入线(“委托”);
Stopwatch sw4=Stopwatch.StartNew();
Function2g=功能;
控制台写入线(“委托:+g(5,5));
sw4.Stop();
Console.WriteLine(sw4.Appeased);
Console.WriteLine(“匿名”);
Stopwatch sw3=Stopwatch.StartNew();
function2f=委托(inta,intb){返回a*b;};
Console.WriteLine(“匿名:+f(5,5));
sw3.Stop();
Console.WriteLine(sw3.Appeased);
控制台写入线(“Lambda”);
Stopwatch sw5=Stopwatch.StartNew();
function2h=(x,y)=>{返回x*y;};
控制台写入线(“Lambda:+h(5,5));
sw5.Stop();
Console.WriteLine(sw5.Appeased);
Console.WriteLine(“职能代表”);
秒表sw=Stopwatch.StartNew();
Func函数=(x,y)=>x*y;
Console.WriteLine(“Func:+函数(5,5));
sw.Stop();
控制台写入线(软件运行时间);
}
静态整数函数(整数x,整数y)
{
返回x*y;
}
}
}

没有区别

delegate(int a, int b) { return a * b; };

将两者转换为具有匿名方法的相同匿名类。因此,从效率的角度来看,没有区别。但是lambda通常可读性更高、更短,因此它们用于LINQ语句中。在lambda中包装乘法函数并没有什么不好的,只是不要创建太多的乘法函数(例如,不要在循环中创建乘法函数),因为这可能会导致实例化许多匿名类

(x, y) => { return x * y; };