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.
There are some properties of Namespaces as listed below:
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 }
Example:
namespace my_program_demo { class demo { public void myfunction() { // your code declarations } } }
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.
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(); } } }
Steps to execute the program:
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(); } }
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 } }
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(); } }
Compile and execute the above code, and you will get the result as shown in the image below:
The above is the detailed content of Namespaces in C#. For more information, please follow other related articles on the PHP Chinese website!