码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2016-05-03 17:50:55      阅读:528      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!