开发者

Can anonymous delegate have return value as non void type?

开发者 https://www.devze.com 2023-01-24 12:17 出处:网络
Can anonymo开发者_开发百科us delegate have return value as non void type?Yes.Both the delegate { return xyz; } and lambda x => x+1 syntax can return values.I also had this question, and wrote a tes

Can anonymo开发者_开发百科us delegate have return value as non void type?


Yes. Both the 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"));
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消