Javatpoint Logo
Javatpoint Logo

Different ways to access namespace in C#

Class organisation is done by namespaces. In bigger classes, it helps in controlling the reach of methods.initiatives involving net programming. Stated differently, it offers a mechanism to maintain distinctions between one collection of names (such as class names) and other sets of names. Namespaces provide the main benefit of preventing class names from conflicting with one another when they are specified in different namespaces. An alternative name for it is titled set of classes with shared characteristics. Namespaces, interfaces, structures, and delegates can all be members of the same namespace.

Syntax:

It has the following syntax:

Example:

Let us take an example to illustrate how to access the namespace in C#.

Accessing the Members of Namespace

The dot(.) operator is used to access members of a namespace. In C#, a class is fully identified by its namespace.

Syntax:

Note:

  • Within a single program, two classes with the same name can be generated in two separate namespaces.
  • There cannot be a name conflict between two classes within a namespace.
  • Class names in C# are fully qualified names; they begin with the namespace name and end with the class name, separated by the dot(.) operator.

Using Keyword:

Using the function or class's fully qualified name each time we want to invoke it or, alternatively, members of a namespace is not really practical. The fully qualified names in the example above are first.Javtpoint_1.display(); and System.Console.WriteLine("Hello javatpoint");. The term "using" is provided by C# to assist the user in avoiding repeatedly writing fully qualified names. Utilizing fully qualified names can be effortlessly avoided by the user by just mentioning the namespace name at programme startup.

Syntax:

It has the following syntax:

Example:

Let us take an example to illustrate how to access the namespace using (Using) Keyword in C#.

Example Code:

Output:

Greetings from CustomClass!

Nested Namespaces:

This is known as a nested namespace, where you can define a namespace inside another namespace. Use of the dot(.) operator is required in order to access members of the nested namespace. Within the collections namespace like System, for instance, Generic is the nested namespace.

EXAMPLE CODE:

Let us take an example to illustrate how to access the namespace using nested namespace function in C#.

Output:

OuterClass method in OuterNamespace
InnerClass method in InnerNamespace

Conclusion:

In conclusion, namespace access is made flexible and controllable in C# with a variety of mechanisms for naming standards and code organisation. Importing namespaces and more easily utilising their types is made possible via the most popular method-the 'using' directive. Alternatively, fully qualified names spare naming issues by giving explicit access to types. For lengthy or frequently used namespaces, alias directives make it possible to create personalised shortcuts for them. In situations when there are name conflicts, the 'global' modifier guarantees clear references. Nested namespaces provide a hierarchical framework to further organise similar functionality within larger namespaces. This highlights the significance of clear and maintainable code practices in C# and helps determine which approach is best based on the particular requirements of the codebase.







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