Javatpoint Logo
Javatpoint Logo

How to use Namespace Alias Qualifier in C#

In this article, you will learn about how to use namespace alias qualifier in C# with its syntax and implementation.

Introduction

In C#, namespace alias modifiers improve the syntax enabling gaining permission to types that are contained in lengthy or nested namespaces.

How to use Namespace Alias Qualifier in C#

They facilitate the definition of aliases for namespaces, which makes it simpler to use types from those namespaces in your source code. In C#, namespace alias qualifiers can be employed as follows:

What a Namespace Alias Means:

Use the specifying directive, the alias name, and the namespace you wish to alias after that to create a namespace alias. As an illustration:

This generates a placeholder called MyAlias for the namespace MyLongAndNestedNamespace.

Using the Alias for Obtaining Types: After the alias has been defined, it may be used to access types inside the aliased namespace. Just put the alias preceding the formatted name, followed by a double colon (::). As an illustration:

In this case, the alias MyAlias is used to access the type A certain kind, which is located in the MyLongAndNestedNamespace.

Code Usage: The namespace alias qualifier can be used whenever types from the aliased namespace need to be referenced. It can appear anywhere in your code, including method declarations and class definitions.

Benefits: When working with large namespaces, namespace alias qualifiers can help your code be more legible and succinct. Additionally, by altering the alias definition in a single location rather than updating several references across the source code, they facilitate code refactoring.

Example:

Let us take an example to illustrate the Namespace Alias Qualifier in C#.

Output:

Hello, using namespace alias qualifiers!

Explanation:

In this example, we specify the namespace System and its alias. Write a message to the console using WriteLine to illustrate how to utilize the alias.

We additionally demonstrate how to implement the fully qualified name System to access the ReadLine function as System.Console.ReadLine().

Upon executing the application, the console will display "Hello, using namespace alias qualifiers!" before requesting input from you. It is going to display the input you typed once you enter it.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA