开发者

Why can't i declare a field using var [duplicate]

开发者 https://www.devze.com 2023-03-16 00:40 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Why class fields cannot be var?
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Why class fields cannot be var?

I'd like to avoid typing out complex/long type definitions f开发者_Python百科or fields like you can with local variables.

I'd like to know why this couldn't be done?

public class Foo
{
    public var barField = new Dictionary<string, int>(); // Does not work

    public void Method()
    {
        var barLocal = new Dictionary<string, int>(); // Works
    }
}


Straight from the source:

Why no var on fields?


You can't have global vars. Please check the remarks section of http://msdn.microsoft.com/en-us/library/bb384061.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消