Click here to Skip to main content
15,938,448 members
Please Sign up or sign in to vote.
4.33/5 (2 votes)
See more:
This statement is from Remark section of 'var' from MSDN:

"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."

I am not getting clear understanding on this remark. Could anyone elaborate it?

https://msdn.microsoft.com/en-us/library/bb384061.aspx[^]
Posted
Comments
Sergey Alexandrovich Kryukov 8-Apr-15 2:11am    
I agree, this phrase is not tremendously clear, so I up-voted this question; it is useful. You got obviously correct answers though.
—SA

var normally is a C#-Keyword that is used to declare a variable of which the compiler can infer the type by the assignment on the right side:
C#
var i = 1;            // i is of type Int32
var s = "abc";        // s is of type String
var d = DateTime.Now; // d is of type DateTime

var z; // not allowed because the compiler can't infer any type

But you are allowed to define an own type (a class or struct) with the name "var":
C#
class var
{
   // ...
}

var x; // x is of type var

"If a type named var is in scope" - means that the example above would work wherever your declaration of a type named var is known at. If you would declare the type var in a different namespace and not specify that namespace with a using-namespace-directive, then the compiler wouldn't know about that type and again treat an occurence of "var" as the C#-keyword.

This is -or at least should be- of academic interest only, it would be very bad practice to actually define a usertype named var because it will irritate everyone looking at the code.
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 8-Apr-15 2:08am    
5ed. Honestly, I did not correctly read this Microsoft statement from the first attempt :-). Interesting phenomenon is the relatively modern trend of introduction of keyword which are not reserved words.
—SA
Sascha Lefèvre 8-Apr-15 2:24am    
Thank you, Sergey! I have to admit, I had to check this myself before answering because I didn't expect it to be allowed ;-)
/Sascha
Maciej Los 8-Apr-15 2:23am    
+5
Sascha Lefèvre 8-Apr-15 2:26am    
Thank you, Maciej!
Mario Z 8-Apr-15 5:55am    
+5 for:
"... it would be very bad practice to actually define a usertype named var because it will irritate everyone looking at the code."
But how funny would that be :D, I can already imagine that everyone would gladly invest a lot of time searching for a developer who wrote this just to slap him in the face.
paste the following in a visual studio console project and compile it:

C#
namespace ATypeNamedVar
{
    class JustCreatingAScope
    {
        class var
        {
            public string SomeProperty { get; set; }
        }

        public static void AMethodUsingTheVarClass()
        {
            //var thisWillNotCompile = 1;
            var thisWillCompile = new var();
            Console.WriteLine("The variable thisWillCompile is of type: " + thisWillCompile.GetType().ToString());
        }

    }

    class Program
    {
        static void Main(string[] args)
        {
            var regularUse = 1;
            Console.WriteLine("The variable thisWillCompile is of type: " + regularUse.GetType().ToString());

            JustCreatingAScope.AMethodUsingTheVarClass();
        }
    }
}


Note the syntax highlighting in visual studio:
In the first use you get the type colouring and not the keyword coloring (unlike in the above)

So: to answer your question: if you have a type named var, like in the above, if you use the word var it will be interpreted by the compiler is that type and not as the keyword var. That is why the statement
var thisWillNotCompile = 1

does not compile, because you cannot assign an integer to the type var.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 8-Apr-15 2:08am    
Right, a 5.
—SA
Serge Desmedt 8-Apr-15 3:08am    
thanks
Maciej Los 8-Apr-15 2:23am    
+5
Serge Desmedt 8-Apr-15 3:08am    
thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900