简体   繁体   English

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

[英]Can anonymous delegate have return value as non void type?

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

Yes. 是。 Both the delegate { return xyz; } 两位delegate { return xyz; } delegate { return xyz; } and lambda x => x+1 syntax can return values. delegate { return xyz; }和lambda x => x+1语法可以返回值。

I also had this question, and wrote a test program. 我也有这个问题,并编写了一个测试程序。 The answer is yes. 答案是肯定的。

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"));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 转换为void返回委托的匿名函数无法返回值 - Anonymous function converted to a void returning delegate cannot return a value 具有返回值的匿名委托 - anonymous delegate with return value 为什么具有返回类型的方法不能与返回void的具有相同签名的委托匹配? - Why can't methods that have a return type match a delegate of the same signature that returns void? 分配给void委托的lambda会丢弃C#中的非void返回类型吗? - Do lambdas assigned to void delegate discard non-void return type in C#? 从匿名函数获取返回值,该返回值转换为无效返回委托 - Get a return from an anonymous function converted to a void returning delegate 从已编译的查询委托返回匿名类型 - return Anonymous type from compiled query delegate 如何从匿名方法/委托返回类型 - How to return a type from an anonymous method/delegate 如何创建一个可以返回非空值的异步方法? - How to create an async method that can return the non-void value? 为什么部分方法只能有void返回类型? - Why partial methods can only have void return type? 是否可以编译具有非无效返回类型但不返回值的C#方法? - Is it possible to compile a C# method that has non-void return type, but does not return a value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM