为什么类字段不能无功? [英] Why class fields cannot be var?

查看:69
本文介绍了为什么类字段不能无功?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class A
{
    A()
    {
        var x = 5;   // this is allowed
    }

    var _x = 5;   // the compiler is unhappy
}

我想,编译器必须能够推断为成员变量只是它它用于本地一个相同的方式的类型。那么,有什么区别呢?

I guess the compiler must be able to deduce the type for the member variable just the same way it does it for the local one. So what's the difference?

推荐答案

埃里克利珀回答你的问题就在这里:的 为什么领域没有变种?

Eric Lippert answered your question right here: Why no var on fields?

基本上,它需要一般情况下重新编写C#编译器,因为它的方式目前并类型推断不会为<$​​ C $ C> VAR 字段变量赋值的循环工作

Basically, for the general case it would require re-writing the C# compiler, as the way it currently does type inference would not work for cycles of var field variable assignments.

这篇关于为什么类字段不能无功?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆