C sharp (C#)

Namespace in C# with Examples

Namespace in c#:

Namespace in c#:- Before we move on, it seems advisable to spend some time on another important aspect of Namespace in c#. This is the way used in .NET, which allows you to create containers for application code so that both the code and its constituent parts are uniquely identified.

Namespace in c# are also used as a means of categorizing objects in the .NET Framework. Most of these objects are defined definitions of types, for example, definitions of simple types, which are discussed in detail in this article.

C# code is contained by default in the global namespace. This means thinks that objects in  # code can be accessed from any other code in the global namespace simply by their name. You can use the key the word namespace to explicitly set the namespace for any a block of code enclosed in curly braces. The names found in such a namespace, if accessed from outside the given namespace, must qualify.


A qualified name is a name that contains all of the information. formation regarding its hierarchy. This means that if we have a code, in the same namespace and you must use the name defined in another namespace, then we should use a reference to it is a namespace. In qualified names to separate levels namespaces use the dot symbol. For example:

namespace LevelOne

{

/ / a program in the LevelOne namespace

// it describes the name "NameOne"

/ / program residing in the global namespace

}

This program describes a single namespace, LevelOne. (Note that in this case, the program does not contain any use executable code. This is done in order to give the discussion as much general nature; on the contrary, a comment has been placed in the code that contains this description.) The code contained within the LevelOne namespace could simply refer to the name NameOne and no classification is required. However, in the code, located in the global namespace, you must use the class- A FIXED NAME LevelOne.NameOne TO REFER TO.

Within any namespace, we can describe nested spaces Namespace in c# using the same namespace keyword. When applying nested Namespace in c# should be referenced in their entire hierarchy, separating one level of the hierarchy from another using a dot. This is best illustrated by Example with an example. Consider the following Namespace in c#:

namespace LevelOne

{

// a program in the LevelOne namespace

namespace LevelTwo

{

// program located in the LevelOne.LevelTwo namespace

 // it describes the name "NameTwo"

}

}



In this case, referring to the name NameTwo from the global namespace MUST have the VIEW of LevelOne.LevelTwo.NameTwo, FROM LevelOne Namespace – LevelTwo.NameTwo, and FROM namespace LevelOne.LevelTwo is NameTwo. It should be noted here that names are identified by Namespace in c# in a unique way. We can describe the same name “NameThree” as In both the LevelOne Namespace AND In the LevelTwo Namespace:

namespace LevelOne

{

// the name "NameThree" is described here

namespace LevelTwo

{

// this describes the name "NameThree"

}

}

In this case, two different names are described that can be used independently of each other – LevelOne.NameThree AND LevelOne.LevelTwo.NameThree. Once the namespace is defined, it is possible to use the using statement to make it easier to access the names it contains. The using statement seems to say, “Yes, we really need access to names from a given namespace, so don’t force me to classify quote them every time. “For example, the following program assumes that code in the LevelOne namespace must have access to the names namespace LevelOne.LevelTwo without any classification:

namespace LevelOne

{

using LevelTwo;

namespace LevelTwo;

{

//the name "NameTwo"

}

}


Code in the LevelOne namespace can now access To Level Two. NameTwo is SIMPLE as TO NameTwo. There are cases, as in the example with NameThree, when this approach can lead to a conflict between names in different namespaces (and most likely the code will not compile). In such cases, we we can set a dummy name (alias) for the namespace in the using statement;

namespace LevelOne

{

using LT = LevelTwo;

// the name "NameThree" is described here

namespace LevelTwo

{

// this describes the name "NameThree "

}

}

Then in the LevelOne Namespace WE CAN refer to LevelOne.NameThree simply as NameThree and LevelOne.LevelTwo.NameThree as LT.NameThree.

Using statements affect the namespace in which they are, as well as all nested namespaces that may be contained in this namespace. In the above code, LT.NameThree cannot be USED in the global namespace. However, IF the using statement arrange as follows:

using LT  = LevelOne.LevelTwo;

namespace LevelOne

{

// the name "NameThree" is described here

namespace LevelTwo

{

// this describes the name "NameThree"

}

}

then use the name “LT. NameThree” will be possible from the global space Names, AND FROM the LevelOne Namespace.

There is one more important point that should be addressed. Attention. The using statement by itself does not provide access to names that are in other Namespace in c#. Until the code from the namespace is will be tied to our project in some way (for example, described in the source file of the project or described in some code) tied to this project ect, we will not get access to the names contained in it. Moreover, if the code which contains a certain namespace, is tied to our project, then we have access to the names contained in it, regardless of the use using statement.

The using statement just makes it easier to refer to these names and allows you to shorten otherwise heavily lengthening code, making it easier to understand. Returning to the ConsoleApplication program, with which we worked in the very at the beginning of this chapter, we can find the following lines of code there, referring to related to Namespace in c#:

using System;

namespace ConsoleApplication

{

...

}


The first line contains a using statement, which declares that the System namespace will be used throughout the C# program and access to it from any namespaces of a given file should be accessed without using classification. The System namespace is the root namespace in the .NET Framework and includes all the major functionalities features that may be needed for console applications. The next line declares the namespace for the application itself ConsoleApplication.

Related Article:

 

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button