Kaustubh Kaluskar
Kaustubh Kaluskar

Reputation: 15

Automatically added Using directive in C #

When using visual studio express to create a console C# application, I found that some namespaces are automatically added at the top of code at start:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

My understanding is first directive should allow me to use things defined in System and rest three directives are for namespaces which are defined within the overall System namespace which is already referred in first directive. Then why is VS 2013 adding rest of three using directives ?

Upvotes: 0

Views: 231

Answers (4)

John Hodge
John Hodge

Reputation: 1715

I think that the way to look at namespaces is that the "." is just another character in the name that makes it easier for humans to understand heirarchies of related namespaces and as far as Visual Studio is concerned, those are two distinct namespaces.

Upvotes: 1

Grant Winney
Grant Winney

Reputation: 66459

You're misunderstanding how namespaces work.

For example, say I could define two classes, in two separate namespaces:

namespace MyNamespace
{
    public class One
    {

    }
}

namespace MyNameSpace.SubNameSpace
{
    public class Two
    {

    }
}

And then I want to create an instance of each in another class, like this:

var one = new One();
var two = new Two();

I'd have to include two using directives in order to do that.

using MyNamespace;
using MyNameSpace.SubNameSpace;

Including only the first using directive is not enough; it doesn't automatically include classes defined in the second namespace.

Upvotes: 2

CS Pei
CS Pei

Reputation: 11047

You can treat the like this

namespace System {

//... stuff
     namespace Linq {
     }
...

}

Namespace of System is different from namespace of System.linq. The first line will allow you to use System.Console. But the first line alone won't allow you to use methods in namespace System.Linq.

Upvotes: 0

Steven Hansen
Steven Hansen

Reputation: 3239

The using system does not automatically include everything within sub-namespaces. The first namespace (System) does NOT bring in the following three.

Upvotes: 1

Related Questions