限制适用于隐式类型的变量声明(Restrictions apply to implicitly-typed variable declarations)

我正在阅读关于隐式键入的局部变量(var)

http://msdn.microsoft.com/en-us/library/bb384061.aspx

它规定了一个限制:

如果名为var的类型处于作用域中,那么var关键字将解析为该类型名称,并且不会将其视为隐式类型局部变量声明的一部分。

任何人都可以用C#示例来解释声明的含义吗?

I was reading about Implicitly Typed Local Variables (var) on

http://msdn.microsoft.com/en-us/library/bb384061.aspx

It states one restriction :

If a type named var is in scope, then the var keyword will resolve to that type name and will not be treated as part of an implicitly typed local variable declaration.

Can anybody explain what is mean by statement with C# example?

最满意答案

如果你这样做:

class var { public static implicit operator var(int value) { return new var(); } } var myVar = 5;

myVar的类型是var而不是int类型的。

(我添加的operator是这样的,从int到var有一个隐式转换)。

插入此规则是因为var不是C#中的保留关键字(并且仍然不是......如果你看这里,你会看到它是一个“上下文关键字”),所以名为var的类/结构体/枚举是有效的在C#2.0中。

如果名为var的类型处于作用域中 :如果存在名为var的类/结构体/枚举(它在范围内)(因此只需编写var即可“可达”,而无需使用名称空间)

那么var关键字将解析为该类型的名称 :然后, var意味着“您的用户定义类型”而不是“关键字var”

That if you do this:

class var { public static implicit operator var(int value) { return new var(); } } var myVar = 5;

The myVar will be of type var and not of type int.

(the operator I've added is so that there is an implicit conversion from int to var).

This rule was inserted because var wasn't a reserved keyword in C# (and still isn't... If you look here you'll see it's a "contextual keyword"), so a class/struct/enum named var was valid in C# 2.0 .

If a type named var is in scope: if there is a class/struct/enum named var that is in scope (so "reachable" by simply writing var, without having to use a namespace)

then the var keyword will resolve to that type name: then var means "your user defined type" and not "the keyword var"

限制适用于隐式类型的变量声明(Restrictions apply to implicitly-typed variable declarations)

我正在阅读关于隐式键入的局部变量(var)

http://msdn.microsoft.com/en-us/library/bb384061.aspx

它规定了一个限制:

如果名为var的类型处于作用域中,那么var关键字将解析为该类型名称,并且不会将其视为隐式类型局部变量声明的一部分。

任何人都可以用C#示例来解释声明的含义吗?

I was reading about Implicitly Typed Local Variables (var) on

http://msdn.microsoft.com/en-us/library/bb384061.aspx

It states one restriction :

If a type named var is in scope, then the var keyword will resolve to that type name and will not be treated as part of an implicitly typed local variable declaration.

Can anybody explain what is mean by statement with C# example?

最满意答案

如果你这样做:

class var { public static implicit operator var(int value) { return new var(); } } var myVar = 5;

myVar的类型是var而不是int类型的。

(我添加的operator是这样的,从int到var有一个隐式转换)。

插入此规则是因为var不是C#中的保留关键字(并且仍然不是......如果你看这里,你会看到它是一个“上下文关键字”),所以名为var的类/结构体/枚举是有效的在C#2.0中。

如果名为var的类型处于作用域中 :如果存在名为var的类/结构体/枚举(它在范围内)(因此只需编写var即可“可达”,而无需使用名称空间)

那么var关键字将解析为该类型的名称 :然后, var意味着“您的用户定义类型”而不是“关键字var”

That if you do this:

class var { public static implicit operator var(int value) { return new var(); } } var myVar = 5;

The myVar will be of type var and not of type int.

(the operator I've added is so that there is an implicit conversion from int to var).

This rule was inserted because var wasn't a reserved keyword in C# (and still isn't... If you look here you'll see it's a "contextual keyword"), so a class/struct/enum named var was valid in C# 2.0 .

If a type named var is in scope: if there is a class/struct/enum named var that is in scope (so "reachable" by simply writing var, without having to use a namespace)

then the var keyword will resolve to that type name: then var means "your user defined type" and not "the keyword var"