隐式类型;为什么只是局部变量?变量、局部、类型、隐式

2023-09-02 11:44:51 作者:突然好想你

有谁知道或不关心猜测为什么隐式类型仅限于局部变量?

  VAR啄=新的Foo();
 

但是,为什么不......

  VAR的getFoo(){
    返回新的Foo();
}
 

解决方案

埃里克利珀做了关于这个问题的一个完整的博客文章。

http://blogs.msdn.com/ericlippert/archive/2009/01/26/why-no-var-on-fields.aspx Java 自定义类 学习 Java 编程语言 026 竖屏

在总结,主要的问题是,它会需要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.aspx

In 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.