简体   繁体   中英

Can anonymous delegate have return value as non void type?

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

Yes. Both the delegate { return xyz; } delegate { return xyz; } and lambda x => x+1 syntax can return values.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM