Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Namespaces and subnamespaces in C#

Tags:

namespaces

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!

like image 241
user198003 Avatar asked Nov 27 '12 11:11

user198003


People also ask

What is a namespace in C?

A namespace is a declarative region that provides a scope to the identifiers (the names of types, functions, variables, etc) inside it. Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries.

What is difference between namespace and package?

Packages are used in Java in order to prevent naming conflicts, to control access, to make searching/locating and usage of classes, interfaces, enumerations and annotations easier, etc. A namespace is designed for providing a way to keep one set of names separate from another.

What are the two types of namespaces?

When creating a namespace, you must choose one of two namespace types: a stand-alone namespace or a domain-based namespace. In addition, if you choose a domain-based namespace, you must choose a namespace mode: Windows 2000 Server mode or Windows Server 2008 mode.


2 Answers

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.

like image 82
J0HN Avatar answered Oct 21 '22 11:10

J0HN


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.

like image 45
Sergey Teplyakov Avatar answered Oct 21 '22 12:10

Sergey Teplyakov