Namespaces

First, I would like to tell you how enthused I am about Udemy – and yes it’s not sponsored blog post. If you don’t know what’s Udemy then pause reading my post and go check it out. I finished my studies in June and from there started self-learning via Udemy. I have bought 2 courses, both are made by Moshfegh Hamedani. Links are under this blog post, go check out how good teacher this guy is. Now, back to namespaces… 😀 Namespaces are used to organize code by declaring a scope that contains a set of related objects. Namespaces allow to develop new code alongside working code, it’s like coding sandbox. In C# you can declare following types in namespace:

  1. Another namespace
  2. Class
  3. Interface
  4. Struct
  5. Enum
  6. Delegate

By default C# namespaces are public and this can’t be modified. In following example I have created namespace name1 and inside that class c1.

Copy
1namespace name1
2{
3 class c1 { }
4}

To access c1 class outside that namespace we simply navigate to it by name1.c1. Or better, we can declare at the beginning of the document that we are using namespace name1. Simply just write at the beginning of document Using name1. Now we can navigate to c1 class without putting name1 before it. In this namespace we can’t declare class with same name, like this:

Wrong way:

Copy
1namespace name1
2{
3 class c1 { }
4
5 class c1 { }
6}

The compiler will now tell us that we already have declared class c1 in the current namespace. Now you may think that we can simply rename the new class and yes, that’s true. But imagine that you are not writing code alone. You have a team and another team member has declared this class already. And class name can’t be changed because this name describes it best. The solution is to use the same class name in other namespaces, like this:

Right way:

Copy
1namespace name1
2{
3 class c1 { }
4}
5
6namespace name2
7{
8 class c1 { }
9}
Available resources