Namespaces are used to organize the classes. It helps to control the scope of methods and classes in larger .Net programming projects. In simpler words you can say that it provides a way to keep one set of names(like class names) different from other sets of names. The biggest advantage of using namespace is that the class names which are declared in one namespace will not clash with the same class names declared in another namespace. It is also referred as named group of classes having common features. The members of a namespace can be namespaces, interfaces, structures, and delegates.
Defining a Namespace
To define a namespace in C#, we will use the namespace keyword followed by the name of the namespace and curly braces containing the body of the namespace as follows:
Syntax:
namespace name_of_namespace {
// Namespace (Nested Namespaces)
// Classes
// Interfaces
// Structures
// Delegates
}
Example:
// defining the namespace name1
namespace name1
{
// C1 is the class in the namespace name1
class C1
{
// class code
}
}
Accessing the Members of Namespace
The members of a namespace are accessed by using dot(.) operator. A class in C# is fully known by its respective namespace.
Syntax:
[namespace_name].[member_name]
Note:
- Two classes with the same name can be created inside 2 different namespaces in a single program.
- Inside a namespace, no two classes can have the same name.
- In C#, the full name of the class starts from its namespace name followed by dot(.) operator and the class name, which is termed as the fully qualified name of the class.
Example:
namespace first {
class Geeks_1
{
public static void display()
{
System.Console.WriteLine( "Hello Geeks!" );
}
}
}
class Geeks_2
{
public static void Main(String []args)
{
first.Geeks_1.display();
}
}
|
Output:
Hello Geeks!
In the above example:
- In System.Console.WriteLine()” “System” is a namespace in which we have a class named “Console” whose method is “WriteLine()“.
- It is not necessary to keep each class in C# within Namespace but we do it to organize our code well.
- Here “.” is the delimiter used to separate the class name from the namespace and function name from the classname.
The using keyword
It is not actually practical to call the function or class(or you can say members of a namespace) every time by using its fully qualified name. In the above example, System.Console.WriteLine(“Hello Geeks!”); and first.Geeks_1.display(); are the fully qualified name. So C# provides a keyword “using” which help the user to avoid writing fully qualified names again and again. The user just has to mention the namespace name at the starting of the program and then he can easily avoid the use of fully qualified names.
Syntax:
using [namespace_name][.][sub-namespace_name];
In the above syntax, dot(.) is used to include subnamespace names in the program.
Example:
// predefined namespace name
using System;
// user-defined namespace name
using name1
// namespace having subnamespace
using System.Collections.Generic;
Program:
using System;
using first;
namespace first {
class Geeks_1
{
public static void display()
{
Console.WriteLine( "Hello Geeks!" );
}
}
}
class Geeks_2
{
public static void Main(String []args)
{
Geeks_1.display();
}
}
|
Output:
Hello Geeks!
Nested Namespaces
You can also define a namespace into another namespace which is termed as the nested namespace. To access the members of nested namespace user has to use the dot(.) operator.
For example, Generic is the nested namespace in the collections namespace as System.Collections.Generic
Syntax:
namespace name_of_namespace_1
{
// Member declarations & definitions
namespace name_of_namespace_2
{
// Member declarations & definitions
.
.
}
}
Program:
using System;
namespace Main_name
{
namespace Nest_name
{
class Geeks_1
{
public Geeks_1() {
Console.WriteLine( "Nested Namespace Constructor" );
}
}
}
}
class Driver
{
public static void Main( string [] args)
{
new Main_name.Nest_name.Geeks_1();
}
}
|
Output:
Nested Namespace Constructor
Similar Reads
C# Identifiers
In programming languages, identifiers are used for identification purposes. Or in other words, identifiers are the user-defined name of the program components. In C#, an identifier can be a class name, method name, variable name, or label. Example: public class GFG { static public void Main () { int
2 min read
Preprocessor Directives in C#
Preprocessor Directives in C# tell the compiler to process the given information before actual compilation of the program starts. It begins with a hashtag symbol (#) and since these preprocessors are not statements so no semi-colon is appended at the end. The C# compiler does not have a separate pre
5 min read
.NET Framework Class Library (FCL)
The Framework Class Library or FCL provides the system functionality in the .NET Framework as it has various classes, data types, interfaces, etc. to perform multiple functions and build different types of applications such as desktop applications, web applications, mobile applications, etc. The Fra
3 min read
C# Tutorial
C# (pronounced "C-sharp") is a modern, versatile, object-oriented programming language developed by Microsoft in 2000 that runs on the .NET Framework. Whether you're creating Windows applications, diving into Unity game development, or working on enterprise solutions, C# is one of the top choices fo
4 min read
Hello World in C#
The Hello World Program is the most basic program when we dive into a new programming language. This simply prints "Hello World!" on the console. In C#, a basic program consists of the following: A Namespace DeclarationClass Declaration & DefinitionClass Members(like variables, methods, etc.)Mai
4 min read
X-Macros in C
X-Macros are based on the property of nested macros and the ability to define macros inside other macros. X-Macros are very powerful pre-processor technique in the sense that it can create a self-maintaining and inter-dependent piece of code. When the change of one part of a program leads to a chang
4 min read
Misc C Programs
C Program to print environment variablesC Program to Swap two NumbersC program swap two numbers without using a temporary variableC Program to check if a given year is leap yearC Program to sum the digits of a given number in single statement?C program to print numbers from 1 to 100 without using lo
1 min read
C Programs
To learn anything effectively, practicing and solving problems is essential. To help you master C programming, we have compiled over 100 C programming examples across various categories, including basic C programs, Fibonacci series, strings, arrays, base conversions, pattern printing, pointers, and
8 min read
Macros and its types in C
In C programming, a macro is a symbolic name or constant that represents a value, expression, or code snippet. They are defined using the #define directive, and when encountered, the preprocessor substitutes it with its defined content. Example [GFGTABS] C //Driver Code Starts{ #include <stdio.h
5 min read
C/C++ Programs
Array C/C++ Programs String C/C++ Programs Linked List C/C++ Programs Stack and Queue C/C++ Programs Tree C/C++ Programs Graph C/C++ Programs Bit Magic C/C++ Programs Misc C/C++ Programs Mathematical C/C++ Programs Dynamic Programming C/C++ Programs Greedy Algorithm C/C++ Programs Backtracking C/C++
1 min read