Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.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/.net/20.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# 匿名委托是否可以将返回值作为非void类型?_C#_.net - Fatal编程技术网

C# 匿名委托是否可以将返回值作为非void类型?

C# 匿名委托是否可以将返回值作为非void类型?,c#,.net,C#,.net,匿名委托是否可以将返回值作为非void类型?是。委托{return xyz;}和lambdax=>x+1语法都可以返回值。我也有这个问题,并编写了一个测试程序。答案是肯定的 using System; public delegate int ReturnedDelegate(string s); class AnonymousDelegate { static void Main() { ReturnedDelegate len = delegate(stri

匿名委托是否可以将返回值作为非void类型?

是。
委托{return xyz;}
和lambda
x=>x+1
语法都可以返回值。

我也有这个问题,并编写了一个测试程序。答案是肯定的

using System;

public delegate int ReturnedDelegate(string s);

class AnonymousDelegate
{
    static void Main()
    {
        ReturnedDelegate len = delegate(string s)
        {
            return s.Length;
        };
        Console.WriteLine(len("hello world"));
    }
}