Constraints apply to declarations of implicitly typed variables.

I read about implicitly typed local variables (var) on

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

It indicates one limitation:

If a type named var is in scope, the var keyword will be allowed for that type name and will not be considered as part of an implicitly typed declaration of a local variable.

Can someone explain what expression means using C # example?

+8
c #
source share
1 answer

What if you do this:

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

myVar will be of type var , not type int .

( operator added, so there is an implicit conversion from int to var ).

This rule was inserted because var not a reserved keyword in C # (and still not ... If you look here you will see this "context keyword"), so the / struct / enum class named var was valid in C # 2.0

  • If the type named var is in scope: if there is a class / struct / enum named var that is in scope (so that it is "reachable" by simply writing var without using the namespace)

  • then the var keyword will resolve this type name: then var means "your custom type" and not "var keyword"

+12
source share

All Articles