Namespaces in C#

WBOY
Release: 2024-09-03 15:03:00
Original
919 people have browsed it

In C#, the namespace is used for organizing many classes to handle the application very easily. It is helpful for keeping a set of names separate from another. You can not conflict with one class name declared in one namespace with the same class name declared in another. It allows organizing the code systematically with a hierarchical system in a group. The hierarchical system can be used to define nested namespaces. You can keep the code in an organized manner by placing the code in different namespaces.

The namespace is a descriptive area within which identifiers (type names, features, variables, etc.) are given a scope. Namespaces are used to arrange code into logical groups and to discourage name collisions, particularly when various libraries are included in your codebase.

In c#, the root namespace is considered as a global namespace. The global:: System defines the namespace “System” of .Net Framework. The System.console can be used in C# programs. The System can be defined as namespace, and Console is considered as a class. By default, .NET Framework provides numerous namespaces to implement the applications easily.

Overview of Namespace

There are some properties of Namespaces as listed below:

  • Namespaces are used to organize bigger code projects.
  • Namespaces are delimited by using the dot (.) operator.
  • In C #, the class’s full name begins with its Namespace name followed by the dot operator and the class name, which is called as class’s fully qualified name.
  • The directive “using” eliminates the requirement of specifying the name of the namespace for every class.
  • In one namespace, the class names declared will not conflict with the same class names declared in another namespace.
  • The global namespace is root namespace, and global:: System refers to the .NET System namespace.

Why do we Need Namespaces in C#?

  • Namespaces in the C# program helps to organize your programs. These Namespaces also help in avoiding a clash between name classes in the two sets of code.
  • If you want to reuse some of your code, then it’s good practice to implement the Namespaces in your own code. The file or directory names don’t correspond to Namespaces. If these are corresponding to Namespaces, you may do so to organize the code.
  • Namespaces play a vital role in writing cleaner codes and managing bigger projects.
  • The main purpose of using Namespace in c# is to reduce code redundancy in .NET applications. To put this in simple words, Namespace is a group of classes, whereas classes are the collection of objects and methods. You can easily access all the class methods just by using namespaces and importing the namespace in the application. Having namespaces enables you to manage the class’s scope and methods. If there are no namespaces, multiple classes with the same name could not be used.

How to define Namespaces in C#?

The Namespace can be defined by using the keyword namespace followed by the namespace_name. The below syntax describes how to define namespace in a c# programming language:

namespace namespace_name {
// body of namespace
}
Copy after login

Example:

namespace my_program_demo
{
class demo
{
public void myfunction()
{
// your code declarations
}
}
}
Copy after login

In the above code snippet, my_program_demo is a namespace, and it includes a class demo as its member and myfunction() is a method of demo class.

How to Access Namespaces in C#?

The class of namespace can be accessed with the help of using the keyword, which specifies that names are using by the program in the given namespace. This keyword provides access to related classes and methods to use in .NET applications. The using keyword allows using the class without having to define the namespace.

In c#, you can also access members of Namespace by using the dot operator. (namespace_name. member_name)

Example:

using System;
namespace Demo {
class DemoExample {
static void Main(string[] args) {
Console.WriteLine("Welcome to C# namespace...");
Console.ReadKey();
}
}
}
Copy after login

Steps to execute the program:

  • Compile the C# program by using the command-line instead of the Visual Studio IDE as shown below:
  • Open a text editor, include the above code and save the file as DemoExample.cs
  • Open the command prompt and go to the directory where the file is saved.
  • Type csc DemoExample.cs and press enter to compile the code.
  • Go to the directory, and you will see the DemoExample.exe executable file.
  • Type DemoExample to execute the program, and output will be displayed on the screen.

Example:

using keyword: The below example specifies usage of using keyword.

using System;
using first_demo;
using second_demo;
namespace first_demo {
class myclass {
public void func1() {
Console.WriteLine("Helloworld.....");
}
}
}
namespace second_demo {
class myclass1 {
public void func2() {
Console.WriteLine("Welcome to EDUCBA.....");
}
}
}
class DemoClass {
static void Main(string[] args) {
myclass cls = new myclass();
myclass1 cls1 = new myclass1();
cls.func1();
cls1.func2();
Console.ReadKey();
}
}
Copy after login

Compile and execute the above code, and you will get the result as shown in the image below.

Nested Namespaces: The syntax for creating a nested namespace is as follows

namespace NamespaceDemo
{
namespace NestedNamespace
{
// code for nested namespace
}
}
Copy after login

The below example shows usage of nested namespaces: The members of a nested namespace can be accessed by using dot (.) operator:

using System;
using first_demo;
using first_demo.second_demo;
namespace first_demo {
class myclass {
public void func1() {
Console.WriteLine("Helloworld.....");
}
}
namespace second_demo {
class myclass1 {
public void func2() {
Console.WriteLine("This is example of nested namespace.....");
}
}
}
}
class NestedNamespaceDemo {
static void Main(string[] args) {
myclass cls = new myclass();
myclass1 cls1 = new myclass1();
cls.func1();
cls1.func2();
Console.ReadKey();
}
}
Copy after login

Compile and execute the above code, and you will get the result as shown in the image below:

Conclusion

  • Using namespaces, c# programs are structured and use the directives to promote the use of namespaces. From this document, we can comprehend the need and use of namespaces in classes. Namespaces can also contain other types as their members, such as classes, interfaces, structures, enumerations, and delegates.
  • Namespaces are used as both an inner organizational system for a program and as an external organizational system to present program aspects exposed to other programs. It regulates the scope of methods and classes in bigger projects. Net programming. Namespaces are also used for solving the naming conflict problem.

The above is the detailed content of Namespaces in C#. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template