隐式类型;为什么只是局部变量? [英] Implicit typing; why just local variables?

查看:120
本文介绍了隐式类型;为什么只是局部变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  VAR啄=新的Foo();
 

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

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

解决方案

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

  • <一个href="http://blogs.msdn.com/ericlippert/archive/2009/01/26/why-no-var-on-fields.aspx">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.

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.

这篇关于隐式类型;为什么只是局部变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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