Introduction To Namespaces in C#
The following article provides an outline for Namespaces in C#. Namespace basically is a set of names given to various objects each having a separate or different nature. These objects could be some type of symbols, system type, file systems and identifiers. Namespace follows a certain path and a hierarchical system or process where it can assign a unique set of names individual to each of these objects so that they can be identified easily and also the same hierarchy allows the repetition of the same names in totally different circumstances.
In simple terms, each individual person in this world has a different name but also sometimes has the same names. Then how to identify each person with the same name separately? Here comes the family name or surname. Although few different people can have the same first name but what uniquely identifies them is their family name or surname. So, generally, that’s what makes namespace significant, it can make use of the same name for entirely different purposes and also provide separate names for separates functioning.
What Do You Understand By Namespaces?
- Namespace is a uniquely identifier medium for different object types to be used in programming languages.
- To complete and write a program we use several types of objects in almost every line of code, although all of them have separate meaning and purpose of usage.
- But sometimes these objects have the same names although having different functionality altogether, so not to have confusion and accidentally use an object of a different meaning for a separate purpose in the lines of code while programming, we use namespace.
- Namespace’s uniquely identifier characteristics enable us to use the same names on different objects having different functionality without causing any error in the coding sequences. We can use separate names also for the objects.
Thus, this distinctive nature of Namespace gives us a huge advantage for the usages of objects of different functions more easily and smoothly in overall programming’s which sometimes involve several thousands of lines of coding sequences.
Need For Namespaces in C#
- Namespaces are designed to provide different ways to make each name and set of name uniquely different from each so not to confuse one from another.
- Namespaces in C# is used for the organization of several classes, having functionalities unique to each of them individually.
- For this reason, the class names of one namespace are not confused with the name of another class of a different namespace.
- Namespaces are used in C# programming on a huge scale and quantity as it involves the use of classes a lot.
- A namespace is mainly used to handle all these classes.
- In C#, the most commonly used namespace is the root namespace.
How To Create Namespaces In C#?
To create a namespace, we must use the namespace as a keyword, and then provide a name for the namespace to be identifiable.
Syntax:
Namespace namespace_name
{
Lines of codes…//
}
The usage of Namespace we are going to write a simple program as an example, how to create Namespace:
Code:
using System; // System is the namespace “System” of .NET Framework
namespace Application // “Application” is the namespace name given here
{
class Program // “Program” is the Class name of the Namespace
{
static void Main (string[] args)
{
Console.WriteLine("Hello World!"); // “Console” is also a Class in namespace
}
}
}
Output:
The above-mentioned program is a basic example to show how namespaces and class names are used properly by C# in the Microsoft .NET Framework.
How Access Namespaces In C#?
Firstly, namespaces and class names are accessed by using dot (.) operators as class names are identified by its own assigned namespaces. As already mentioned previously separate classes can have the same class names but not in the same space or namespace but it can be created inside two separate namespaces in the same program.
Below is an example of Namespaces in C#:
Code:
using System;
namespace Name1
{
public class Sign
{
public void saySign() { Console.WriteLine("Hello First Name"); }
}
}
namespace Name2
{
public class Sign
{
public void saySign() { Console.WriteLine("Hello Second Name"); }
}
}
class TestSpace
{
public static void Main ()
{
Name1.Sign S1 = new Name1.Sign();
Name2.Sign S2 = new Name2.Sign();
S1.saySign();
S2.saySign();
}
}
Output:
So you can see how namespace accessions are done while having the same class names but in different namespaces.
Conclusion
So it is clearly visible the significance of Namespaces in C# programming language and its implementation and also its usages. Overall, Namespace plays a vital role in C# coding sequences which involves thousands of lines of codes including several numbers of classes and methods having the same class names and also different. Namespace helps avoid conflictions and confusion among these classes having the same names but with different purposes all together to prevent any kind of error at the end of the programming that is Namespace helps in the overall organization of C# programming systematically and methodically.
Recommended Articles
This is a guide to Namespaces in C#. Here we discuss the introduction, need, how to create and access Namespaces in C#? respectively. You can also go through our other suggested articles to learn more –