Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing nested namespaces automatically in C#

Tags:

c#

.net

Sorry if this question was asked already. I started studying C# and noticed that C# doesn't automatically import nested namespaces. I don't understand:

using System;

should automatically import all classes contained in the System namespace right? So there should be no need for me to write

using System.Windows.Form;

I would understand if using Windows.Form even worked. But the compiler could not resolve it! What is the point of the using System; before it then? So why does using System; not import System.Windows automatically as well as System.Windows.Forms - sorry if the word import is wrong here.. maybe move to global namespace is the right terminology.

like image 926
Lews Therin Avatar asked Jan 26 '12 18:01

Lews Therin


People also ask

Can you nested namespaces?

In C++, namespaces can be nested, and resolution of namespace variables is hierarchical. For example, in the following code, namespace inner is created inside namespace outer, which is inside the global namespace.

What is the use of nested namespace?

A namespace inside a namespace is called a nested namespace in C#. This is mainly done to properly structure your code.

Why do we use namespace in C sharp?

The namespace keyword is used to declare a scope that contains a set of related objects. You can use a namespace to organize code elements and to create globally unique types. namespace SampleNamespace; class AnotherSampleClass { public void AnotherSampleMethod() { System. Console.

How to import nested namespaces in a C program?

C# doesn't import nested namespaces and this is by design. Namespace scope lets you organize code and gives you a way to create globally unique types. Nested namespaces are used to group related functionality, but use parts of it on-demand.

What are unnamed namespaces in C++?

In unnamed namespaces, name of the namespace in not mentioned in the declaration of namespace. The name of the namespace is uniquely generated by the compiler. The unnamed namespaces you have created will only be accessible within the file you created it in. Unnamed namespaces are the replacement for the static declaration of variables.

Does C# not import classes from other assemblies?

Good point - C# allows namespaces to be split across assemblies, so your project may not have imported (or want to import) the other assemblies. I just realized how confused I am. I keep confusing using with import? And I assume they must be different things.. Does C# not import classes at all? I know C++ and Java does..

How to use a type in a specific namespace?

In order to use a type you need to uniquely identify it. This is done through the namespace - the using directive simply means you don't have to type the fully qualified name of the type. So using will import all classes in the current namespace, but when it hits a nested namespace it ignores it? Unless I explicitly specify it.. is that the point?


1 Answers

C# is not Java.

A using directive is used so you don't have to type in the fully qualified name of a type. It also helps with disambiguating type names (using aliases for instance).

In the case of Console, for example, you don't need to type System.Console.

It is important to understand the difference between a namespace and an assembly - a namespace is a logical grouping of types. An assembly is a physical grouping of types. Namespaces can span assemblies.

When you reference an assembly (this is more like importing a package in Java), you gain access to all of the public types in it. In order to use a type you need to uniquely identify it. This is done through the namespace - the using directive simply means you don't have to type the fully qualified name of the type.

like image 57
Oded Avatar answered Sep 16 '22 22:09

Oded