C# Namespaces are primarily formulated to separate one set of names from some other. Hence, the class names declared in one namespace do not conflict with the same class names declared in an another namespace. Thus, by carrying out the namespaces in code one can avoid any troubles that arise when reusing the code.
The keyword namespace is utilized to declare a scope that contains a set of related objects. Namespaces are also utilized to organize code elements and to create unique types globally. It is always a good practice to implement namespaces in code for the above mentioned reasons.
Conceptual
Program
Example
The program provided below shows an example of how to use a namespace in C#.
[csharp]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SPLessons
{
class Example
{
public void first()
{
Console.Write("From First");
}
}
}
Namespace SPLessons1
{
class Example1
{
public void second()
{
Console.Write("From Second");
}
}
}
class Program
{
static void Main(string[] args)
{
SPLessons.Example x = new SPLessons.Example(); //Call of class from namespace1
SPLessons1.Example1 y = new SPLessons1.Example1(); //Call of class from namespace2
x.first(); //Call of function from namespace1
y.second(); //Call of function from namespace 2
}
}
}
[/csharp]