C#匿名方法返回值赋值给变量
2016-05-03 16:49
651 查看
The problem here is that you've defined an anonymous method which returns a string but are trying to assign it directly to a string. It's an expression which when invoked produces a string it's 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#之process类相关整理
- c#匿名类型
- Visual studio C#语言输出调试信息到Output窗口方法
- 记《learning hard C#学习笔记》 书中一个错误
- C#通过Thrift连接查询HBase主要方法总结
- C#中Dictionary小记
- 【C#】基础知识—程序调试与异常处理机制
- C# 提示语句总汇
- C#可变变量Params,out,ref
- C#中Collection,List和ArrayList的区别(转)
- C#课后习题---列表框实验
- C#课后习题--单选按钮和复选框实验
- c#大圣之路笔记——c#通过页面把excel中的数据导入到DB中(全部)
- C# Socket连接 无法访问已释放的对象
- C#socket通讯两个最经典错误解决方案
- 【C#设计模式-外观模式】
- C#中的Hook,即钩子,引用微软的文章,很有用的
- 《深入.NET平台和C# 编程》内测纠错记录
- c# 把时间写入文件
- 【C#设计模式-组合模式】