var keyword without 'using someNamespace'

Posted by RichK on Stack Overflow See other posts from Stack Overflow or by RichK
Published on 2010-05-07T14:33:50Z Indexed on 2010/05/07 14:38 UTC
Read the original article Hit count: 314

Filed under:
|
|

How does Visual Studio/intellisense know what to do with a variable declared as var even if you don't include the necessary using declaration at the top?

For example, I have class MyDomainObject defined in a different namespace If I don't declare using TheOtherNameSpace; in the file the following code won't compile:

private void Foo()
{
   MyDomainObject myObj = new MyDomainObject(); 
   // Doesn't know what this class is
}

But if I use var

var myObj = new MyDomainObject();

This will compile, and intellisense knows exactly what I can with it.

So how the heck does it know what the type is without the using?

(And as an aside, if it knows without the using, why do we need usings at all?!)

© Stack Overflow or respective owner

Related posts about c#

Related posts about using