How do namespaces work in C #?

40

I'm studying C # and I came across namespaces .

How does it work and when is it applied?

If possible, some basic example.

    
asked by anonymous 01.04.2014 / 01:17

5 answers

38

Functions

Namespaces are confused by many programmers. There are two helpful insights.

First to organize your types logically . It is as if you put certain types (classes, structures, enumerations, delegations, interfaces, etc.) that are related in some way in the same "box". The syntax of language gives a good indication that this is what happens. But it's misleading.

The second understanding is disambiguating type names that appear to be conflicting . That is, you can have two or more types with the same name in your application (within your project or externally) as long as they are in different namespaces .

It does not encapsulate anything

This understanding shows that despite the view that developers have about the namespace being a module, a type box (as the first function indicates), it actually functions as a surname for the types. A surname functions as a way of naming a family. So you can have two Ricardo s in the same environment without confusion, because one is Oliveira and the other is Silva .

So when you create:

namespace Estoque {
    public class FormCadastro { ... }
}

In fact internally it is created:

public class Estoque.FormCadastro { ... }

The namespace disappears and gives a last name for the type. So, although it's a good idea to see the namespace as a box grouping types, this idea gives the wrong impression of the concept.

Think, if it was a real box, could you have that box separated into several pieces? Or worse, could you have the same box in several places? Physically, it is impossible. And the namespace allows this. So it's not a box, it's a last name. You can have the same family (same surname) separated in several places. Some members are in one place (one source file) and others are in another. In fact it is recommended to have only one member (one type) in each file).

Composite surnames

You can have more than one surname. For example when you have a namespace System.Collections . You are saying that the surname is composed of two words. You do not have a family named System and within this family a subfamily named Collections . Some people may understand that putting a using System in its source will include all types of System.Collections and this idea is wrong. They are independent things. There is no tree concept of namespaces . Since they are not boxes, namespaces can not be inside each other.

Saving typing

The advantage of being considered something separate from the type name and not an integral part is that you can use these types without the surname, as long as you indicate in which family you should look for the member (by type). This is done with the using directive. More information on this answer .

The idea that using functions as a include is not true. You can think of this to make it easier to understand, but in reality the closest thing to include in .NET is to include an assembly in the project (create a reference for it). Manually it would include something like this:

<References>
  <Reference
    Name = "Lib1"
    AssemblyName = "Lib1"
    HintPath = "\BuildServer\Latest\Release\SharedComponent\SomeControl.dll"
  />
</References>

Modules in .NET

In C #, or .NET as a whole, it is possible to have the actual cash concept. This is done through assembly . See in this answer .

The idea of include is closer to Java packages. Although package of Java is a conjunction of the assembly and the namespace found in C # and VB.NET. These languages separate concepts.

Conclusion

You need to understand what logical groupings are. This is not a concept of language, but a way of organizing your projects. You will probably want to organize everything that is part of Estoque into a same namespace , all part of the Estoque family. Some people might be tempted to group all the Forms s of the application into a same namespace but this would normally be an error. You are putting strange members in the same family just because they "carry on the same profession".

I placed it on GitHub for future reference .

    
22.05.2014 / 15:20
25

Namespaces serve two things to my eye:

  • organize the code, grouping classes, structures and the like

  • Avoid class name conflicts, structures, delegates, etc.

What does the namespace mean

When you create classes and others within a namespace, the namespace is actually part of the name of that class.

namespace EspacoNomes
{
    public class MinhaClasse
    {
    }
}

In fact, the full name of this class is EspacoNomes.MinhaClasse .

Using classes without indicating full name

The full class name can be reduced to only MinhaClasse when using using s at the top of the file, or at the beginning of the namespace:

using EspacoNomes; // faz com que todas as classes dentro do namespace possam
                   // ser referidas somente pelo nome final da classe

You can also completely rename a class with a using:

using NovoNome = EspacoNomes.MinhaClasse

You can now refer to EspacoNomes.MinhaClasse by simply using NovoNome .

Another important thing is that when a code is inside a namespace, it can directly reference everything that is directly in the same namespace. For example, two classes within the same namespace can refer to each other using only the final class name.

    
01.04.2014 / 01:22
20

namespaces in are sets of identifiers which serve to group common features. For example, in an MVC project, I have namespace for models ( Models ), one for controllers ( Controllers ) and one for views ( Views ).

When declaring a class, when we use another class (using the using statement), the use of namespaces is useful because adding a namespace to using causes the class to access to all items implemented in that namespace . In the MVC example, if you use using MeuProjeto.Controllers in some source, all the classes marked with namespace MeuProjeto.Controllers will be accessible in this source.

    
01.04.2014 / 01:24
1

Rather than saying a namespace is a middle of a class, struct, variable be localized.

Type:


namespace loc{
  public class usuario : Form { }
}

the namespace in an external location:


using loc; // a namespace utilizada acima

class Program{
   void Main(){
     usuario u =new usuario(); u.ShowDialog();
   }
}

If you want to use a class of a namespace without using it in whole, use the code:


using Thread = System.Threading.Thread;

class App{
 public App()
{
  Thread.Sleep(100); // em vez de usar System.Threading.Thread.Sleep(100); / using System.Threading.Thread;
}
}
    
08.07.2014 / 14:24
-1

Speaking roughly we can say that Namespace is like a bundle of classes and interfaces within your application. In Java the term used is Package referring to this.

    
19.03.2015 / 14:42