Namespaces in C# Programming: In this tutorial, we will learn about Namespaces, how to define it, access its numbers, and use it in a C# program.
Namespaces in C# are used to coordinate such a large number of classes so that it very well may be not difficult to deal with the application
Namespaces are used in C# to sort out and give a degree of partition of codes. They can be considered as a compartment which comprises of other namespaces, classes, and so on
A namespace can have following sorts as its numbers:
- Namespaces (Nested Namespace)
- Classes
- Interfaces
- Structures
- Delegates
We will examine about these points in later tutorials. For the present we will stick with classes and namespaces.
Namespaces are not mandatory in a C# program, but rather they do assume a significant role in writing cleaner codes and overseeing larger projects.
Let’s comprehend the idea of namespace with a real life situation. We have an enormous number of files and folders in our computer. Imagine how troublesome it is oversee them on the off chance that they are set in a single directory. This is the reason we put related files and folders in a different index. This assists us with dealing with our data appropriately.
The idea of namespace is comparative in C#. It assists us to organize various numbers by placing related individuals in the equivalent namespace.
Namespace likewise takes care of the issue of naming conflict. At least two classes when placed into various namespaces can have same name.
In this article, you will learn-
Defining Namespace in C#
We can define a namespace in C# using the namespace watchword as:
namespace Namespace-Name { //Body of namespace }
For instance:
namespace MyNamespace { class MyClass { public void MyMethod() { System.Console.WriteLine("Creating my namespace"); } } }
In the above example, a namespace MyNamespace is created. It comprises of a class MyClass as its part. MyMethod is a technique for class MyClass.
Accessing Members of Namespace in C#
The numbers of a namespace can be gotten to using the dab(.) operator. The syntax for getting to the member of namespace is,
Namespace-Name.Member-Name
For instance, in the event that we need to create an object of MyClass, it very well may be done as,
MyNamespace.MyClass myClass = new MyNamespace.MyClass();
We will discuss about creating objects in later tutorial. For the time being center around how the class MyClass is accessed.
Example 1: Introducing Namespace in C# Program
using System; namespace MyNamespace { public class SampleClass { public static void myMethod() { Console.WriteLine("Creating my namespace"); } } } namespace MyProgram { public class MyClass { public static void Main() { MyNamespace.SampleClass.myMethod(); } } }
At the point when we run the program, the output will be:
Creating my namespace
In the above program, we have created our own namespace MyNamespace and accessed to its numbers from Main() strategy inside MyClass. As said before, the dot(.) operator is used to access the number of namespace.
In the Main() technique, myMethod() strategy is called using the dot (.) operator.
Using a Namespace in C# [The using Keyword]
A namespace can be remembered for a program using the using watchword. The syntax is,
using Namespace-Name;
For instance,
using System;
The advantage of this methodology is we don’t need to determine the completely qualified name of the numbers of that namespace each time we are accessing it.
When the line
using System;
is incorporated at the top of the program. We can write
Console.WriteLine("Hello World!");
Rather than the completely qualified name for example
System.Console.WriteLine("Hello World!");
Nested Namespace in C#
A namespace can contain another namespace. It is called settled namespace. The nested namespace and its numbers can likewise be accessed using the dot (.) operator.
The syntax for creating nested namespace is as per the following:
namespace MyNamespace { namespace NestedNamespace { // Body of nested namespace } }
Example 2: Nested Namespace in C#
using System; // Nested Namespace namespace MyNamespace { namespace Nested { public class SampleClass { public static void myMethod() { Console.WriteLine("Nested Namespace Example"); } } } } namespace MyProgram { public class MyClass { public static void Main() { MyNamespace.Nested.SampleClass.myMethod(); } } }
At the point when we run the program, the output will be:
Nested Namespace Example
This example shows how nested namespace can be implemented in C#.
Here, we currently have an extra namespace inside MyNamespace called Nested. Thus, rather than using MyNamespace.SampleClass.myMethod(), we need to use MyNamespace.Nested.SampleClass.myMethod().
Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.