使用VAR方法之外方法、VAR

2023-09-02 21:53:40 作者:哥就拽╮你能把我咋嘀

我想用 VAR 关键字,在我的类中声明的字段但 VAR 似乎只在里面工作的方法。

I wanted to use the var keyword to declare a field in my class however var only seems to work inside methods.

在code我是这样的:

The code I have looks like:

public static Dictionary<string, string> CommandList = new Dictionary<string, string>{};

和我想有:

public static var CommandList = new Dictionary<string, string>

为什么这是不可能的?

How come this isn't possible?

推荐答案

我关于这个问题的文章:

My article on the subject:

为什么没有变种领域?

要总结一下:

如果我们有无功的字段则字段的类型不能确定,直到前pression进行了分析,出现这种情况后,我们已经需要知道字段的类型。

If we have "var" fields then the type of the field cannot be determined until the expression is analyzed, and that happens after we already need to know the type of the field.

如果有长链,或在这些引用偶数周期?所有这些算法将不得不在那里顶层类型信息被确定从他们,而不是被消耗按他们。

What if there are long chains, or even cycles in those references? All of those algorithms would have to be rewritten and tested in a world where top-level type information is being determined from them rather than being consumed by them.

如果您有变种领域则初始化可能是匿名的类型。假设场是公开的。目前还没有在CLR任何标准或有关什么是正确的方式来揭露匿名类型的字段中的华彩。

If you have "var" fields then the initializer could be of anonymous type. Suppose the field is public. There is not yet any standard in the CLR or the CLS about what the right way to expose a field of anonymous type is.