Namespaces in C# with Examples

Namespaces in c#:

Namespaces in c#:- Before we move on, it seems advisable to spend a while on another important aspect of Namespaces 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.

Namespaces 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 are found in this chapter (system.mt32, etc.).

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 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:

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 Namespaces in c# using the same namespace keyword. When applying nested Namespaces 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#:

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 Namespaces in c# in a unique way. We can describe the same name “NameThree” as In both the LevelOne Namespace AND In the LevelTwo Namespace:

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 namespace of the name LevelOne.LevelTwo without any classification:

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 can set a dummy name (alias) for the namespace in the using statement;

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:

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 Namespaces in c#. Until the code from the namespace, it 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 Namespaces in c#:

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 Articles:

Operators in C#: Arithmetic, Assignment, logical, relational and bitwise

Variables in C# with Programming Examples

Related Articles

Leave a Reply

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

Back to top button