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"));
}
}
精彩评论