首页 > Windows开发 > 详细

C#匿名方法返回值赋值给变量

时间:2016-05-03 17:49:50      阅读:234      评论:0      收藏:0      [点我收藏+]
The problem here is that youve defined an anonymous method which returns a string but are trying to assign it directly to a string. Its an expression which when invoked produces a string its not directly a string. It needs to be assigned to a compatible delegate type. In this case the easiest choice is Func<string>

Func<string> temp = () => {return "test";};

This can be done in one line by a bit of casting or using the delegate constructor to establish the type of the lambda followed by an invocation.

string temp = ((Func<string>)(() => { return "test"; }))();
string temp = new Func<string>(() => { return "test"; })();

Note: Both samples could be shorted to the expression form which lacks the { return ... }

Func<string> temp = () => "test";
string temp = ((Func<string>)(() => "test"))();
string temp = new Func<string>(() => "test")();

 

C#匿名方法返回值赋值给变量

原文:http://www.cnblogs.com/lonelyxmas/p/5455612.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!