Introduction
This article helps you to explain Namespace.
- How to use namespace in C#.
- Explain about alias namespace in C#.
- Explain about fully qualified namespace.
- How to remove ambiguty error when mutltiple namesapces are created.
How to use namespace In c#
First, we will discuss namespace. Namespace organizes your program. It makes code easily readable. It also provides assistance in avoiding name clashes.
First, we need to create C# console application. You can follow the steps to create a console application, using Visual Studio.
Open Visual studio ->File->New->Project after clicking project, a pop up Window will pop up , which is shown below.
After opening a pop up under C# menu, I will discuss about sample code, using console Application. I chose console Application. Enter my Project name Namespacedemo after click OK. Solution Explorer will display.
- using System;
- using Group.SubGroupA;
- namespace Namespacedemo
- {
- class Program
- {
- static void Main(string[] args)
- {
-
-
- classGroup.print();
-
- Group.SubGroupB.classGroup.print();
- Console.ReadKey();
- }
- }
- }
-
- namespace Group
- {
- namespace SubGroupA
- {
- class classGroup
- {
- public static void print()
- {
- Console.WriteLine("Hi All Group A print The value ");
-
- }
- }
- }
- }
- namespace Group
- {
- namespace SubGroupB
- {
- class classGroup
- {
- public static void print()
- {
- Console.WriteLine("Hi All Group B print The value ");
-
- }
- }
- }
- }
In My Code, I will explain that I have one main Namespace, which is called with the name Group. Group has two subgroup Names- SubGroupA and SubGroupB, where both have void return type. I enter method name Print. Both two submenus have two different output strings. Now, I need to invoke both two namespaces in the main class of the Program. Now, I should invoke both SubGroupA and SubgroupB to main class. There are different ways to invoke namespace's main class Program.
- Full Qualified namespace
- Alias Namespace
Proceed, as shown below and I have added namespace, using Directive namespace.
In Full Qualified namespace on needs to have namespace name, classname and method.
- Group.SubGroupB.classGroup.print();
Write the code, as shown below.
- using System;
- using GroupA = Group.SubGroupA;
- using GroupB = Group.SubGroupB;
- namespace Namespacedemo
- {
- class Program
- {
- static void Main(string[] args)
- {
-
- GroupA.classGroup.print();
- GroupB.classGroup.print();
- Console.ReadKey();
- }
- }
- }
-
- namespace Group
- {
- namespace SubGroupA
- {
- class classGroup
- {
- public static void print()
- {
- Console.WriteLine("Hi All Group A print The value ");
-
- }
- }
- }
- }
- namespace Group
- {
- namespace SubGroupB
- {
- class classGroup
- {
- public static void print()
- {
- Console.WriteLine("Hi All Group B print The value ");
-
- }
- }
- }
- }
I use it, when I've got multiple namespaces with conflicting sub namespaces and/ or object names. Alias namespace avoids an ambiguty error. After running the code, the result page will look, as shown below.
Output
Conclusion
I hope you understood how to use namespace in C# and how to use alias Directive and Fully Qualified namespace in C#. Please share your valuable feedback and comments to improve my future articles.