Namespaces are used to organize classes, allowing us to control the scope of methods and classes in large .Net programming projects. Let's explore further about Namespaces in C# in the following article by Mytour.
What does Namespace mean in C#?
1. Understanding Namespace in C#.
2. Namespace Definition.
2.1. Syntax of Namespace Definition.
3. Accessing Members of Namespace.
3.1. Syntax for Accessing Members of Namespace.
3.2. Examples of Accessing Members of Namespace.
4. Using Keyword.
4.1. Syntax.
4.2. Example.
5. Nested Namespace.
5.1. Syntax for Nested Namespace.
5.2. Example.
1. What is Namespace in C#?
In simple terms, Namespace is a way to group a set of names (such as class names) different from other sets of names. The biggest advantage of using Namespace is that class names declared within this namespace do not conflict with class names declared in other namespaces.
Additionally, a namespace can be understood as a group of classes with common characteristics. Members of a namespace can be namespaces, interfaces, structures, and delegates.
2. Defining Namespace
To define a namespace in C#, we use the namespace keyword, followed by the namespace name and curly braces {} containing the body of the namespace as shown below:
2.1. Syntax for Namespace Definition
The syntax for defining a Namespace is as follows:
namespace namespace_name {
// Namespace (Nested Namespaces)
// Classes
// Interfaces
// Structures
// Delegates
}
Example
Below is an example of defining a namespace in C#:
3. Accessing Members of Namespace
Members of a namespace are accessed by using the dot operator (.). A fully qualified class in C# includes its corresponding namespace.
The syntax for accessing members of a Namespace is: [namespace_name].[member_name]
Notes:
- Two classes with the same name can be created from two different namespaces in the same program.
- Within a namespace, two classes with the same name cannot exist.
3.2. Example of Accessing Members of Namespace
The output format is: Hello Mytour!
In the example above:
- In System.Console.WriteLine(), 'System' is a namespace, where the class name is 'Console' and the method is 'WriteLine()'.
- It's not mandatory to group each class in C# into its own namespace, however, we can do so to organize code appropriately.
- Here the '.' is a delimiter used to separate the class name from the namespace and the function name from the class name.
4. Using Keyword
In practice, when calling a function or a class (or members of a namespace), we don't need to use its full name.
In the above example, System.Console.WriteLine('Hello Geeks!'); and first.Geeks_1.display(); are full names. Hence, C# provides the 'using' keyword to help users avoid writing full names repeatedly. Instead, users only need to refer to the namespace name at the beginning of the program.
4.1. Syntax
Below is the syntax for using the Using keyword: using [namespace_name][.][ sub_namespace_name];
In the above syntax, the dot (.) is used to include the sub namespace name in the program.
4.2. Example
Here are some examples of using the Using keyword in C#:
- Example 1:
- Example 2:
The output format is:
Hey Mytour!
5. Nested Namespace (Namespace within a Namespace)
Furthermore, you can define a namespace inside another namespace, known as a nested namespace. To access members of a nested namespace, we use the dot operator (.).
For instance, Generic is a nested namespace within the namespace group System.Collections.Generic.
5.1. Syntax for Nested Namespace
The syntax for nested namespaces is as follows:
namespace name_of_namespace_1
{
// declare and define members
namespace name_of_namespace_2
{
// declare and define members
.
.
}
}
5.2. Example
Here is an example of nested namespaces:
The output format is: Nested Namespace Constructor
This article has just introduced you to what Namespace is in C#. Additionally, readers can refer to other articles on Mytour to learn more about Enum in C#.