日期:2014-05-17  浏览次数:21026 次

能否把一个lambda赋值给一个var类型的变量?
我想写一个lambda,其实就是对int.Parse的包装,输入一个string,输出一个int,如下:

        static void Main(string[] args)
        {
            var s = () => this.Output = int.Parse(this.Input);

var这句编译报错,而且报了3个错误!
error CS0815: Cannot assign lambda expression to an implicitly-typed local variable
error CS0026: Keyword 'this' is not valid in a static property, static method, or static field initializer
Keyword 'this' is not valid in a static property, static method, or static field initializer

是不是我的lambda写错了呢?

------解决方案--------------------
var s1 = new Func<string, int>(int.Parse);
这样也可以。