user198003
user198003

Reputation: 11151

Namespaces and subnamespaces in C#

begginers question about C#.

In every program I have to include several namespaces, like:

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

Why do I have to include:

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

... since they are included with first one:

using System;

Thank you in advance!

Upvotes: 6

Views: 5402

Answers (8)

Rune FS
Rune FS

Reputation: 21742

System and System.Text are two different namespaces. That System.Text seems to be a part of System is the semantics we as programmer put into it. There's no such thing as a nested namespace from a platform view;

But even if that was not the case what should happen if you had

namespace MySystem{
   namespace Foo{
       class Bar {...}
   }

   class Bar{...}
}

using MySystem;
class MyClass{
    private Bar _myBar; //Which one is it MySystem.Foo.Bar or MySystem.Bar?     
}

Upvotes: 2

J0HN
J0HN

Reputation: 26921

Because nested namespaces are not included with parent one. See using directive documentation for details

A using directive does not give you access to any namespaces that are nested in the namespace you specify.

Upvotes: 10

Mr_Green
Mr_Green

Reputation: 41822

Just an added example to make you understand clearly.

A child Namespace cant be accessed by calling parent namespaces

Namespace Main  //only code and classes are accessible
{
  //code for Main Namespace
  Namespace subMain     //only code and classes are accessible
  {
     //code for subMain
     Namespace verySubMain  //only code and classes are accessible
     {
        //code for verySubMain
     }
     Namespace otherVerySubMain  //only code and classes are accessible
     {
        //code for otherVerySubMain
     }
  }
}

Upvotes: 0

Miguel Angelo
Miguel Angelo

Reputation: 24182

Imagine these namespaces with these classes (the last name is a class):

  • A.B.Class1
  • A.Class2

Now you have the following:

using A - allows you to refer to Class2 directly... but not to Class1.

using A.B - allows you to refer to Class1 directly but not to Class2.

If you want to refer to both classes directly in your code, you need both usings.

Upvotes: 2

Prabhu Murthy
Prabhu Murthy

Reputation: 9261

Not all classes in .net is inside one big container.Doing so increases the chance of class name collision and it doesn't look good in the first place.Namespaces are containers that try to keep your library clean and make more sense.Having a FTP class and String class together under one container does not logically make any sense.They do two different things and they should be kept in separate containers.

Also a namespace can be nested.Sometime A namespace can all but have just another namespace,without any class.so to access a class you need to qualify the full namespace before you can use it.

In your case you use different classes ,which are on different containers.So if you need them you need to qualify them with their namespace.

Upvotes: 0

nemstr
nemstr

Reputation: 33

If you wanted access to a class in System.Text without the using statement, you would have to Text."Name of class/function" every time you wished to use it.

This way you can just call "name of class/function".

Not sure if you are a beginner, but that's the simplest way I can describe put it.

Upvotes: -1

Sergey Teplyakov
Sergey Teplyakov

Reputation: 11647

System and System.IO namespaces are different.

You can treat "subnamespace" as parent-child relationship in the object model. If you have access to the "Car" object does not mean that you have access to car's wheels.

System is a huge namespace that contains hundreds of nested namespace and thousands of classes. You should specify all nested namespaces separately to state what part of the module are you interested in.

Upvotes: 2

Viral Shah
Viral Shah

Reputation: 2246

Since you're beginner let me clarify one thing namespace in C# and package in Java are different things. no need to merge them.

Upvotes: 0

Related Questions