开发者

Can anonymous delegate have return value as non void type?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜