有谁知道或不关心猜测为什么隐式类型仅限于局部变量?
VAR啄=新的Foo();
但是,为什么不......
VAR的getFoo(){
返回新的Foo();
}
解决方案
埃里克利珀做了关于这个问题的一个完整的博客文章。
http://blogs.msdn.com/ericlippert/archive/2009/01/26/why-no-var-on-fields.aspx在总结,主要的问题是,它会需要C#编译器的一个主要的重新架构这样做。声明在一个单一的传球方式当前处理。这将需要多遍的,因为以形成推断变量之间的周期的能力。 VB.net大致有同样的问题。
Does anyone know or care to speculate why implicit typing is limited to local variables?
var thingy = new Foo();
But why not...
var getFoo() {
return new Foo();
}
解决方案
Eric Lippert did an entire blog post on the subject.
http://blogs.msdn.com/ericlippert/archive/2009/01/26/why-no-var-on-fields.aspxIn summary, the main problem is that it would have required a major re-architecture of the C# compiler to do so. Declarations are currently processed in a single pass manner. This would require multiple passes because of the ability to form cycles between inferred variables. VB.net has roughly the same problem.