Namespaces in C#
A namespace in C# is a way to organize code into groups. It’s like a container that holds classes, structs, interfaces, enums, and delegates, helping to keep related code together and avoid name conflicts.
Structure of namespace
namespace Namespacename
{
[access_modifier] class ClassName
{
//Execution code like methods , fields , properties , constructor etc
}
}
Why Do We Use Namespaces?
Organization:
- Helps keep related classes and other types together.
- Makes the code easier to manage and understand.
Avoiding Name Conflicts
- Prevents conflicts when you have multiple classes with the same name in different parts of your program or when using libraries.
Example: Organizing Animals with Namespaces
using System;
namespace Pets
{
class Animal
{
public void Dogmakesound()
{
Console.WriteLine(“The dog says: Woof Woof”);
}
}
}
namespace WildAnimals
{
class Animal
{
public void Lionmakesound()
{
Console.WriteLine(“The lion says: Roar”);
}
}
}
class Program
{
static void Main()
{
Pets.Animal myDog = new Pets.Animal();
WildAnimals.Animal myLion = new WildAnimals.Animal();
myDog.Dogmakesound(); // Outputs: The dog says: Woof Woof
myLion.Lionmakesound(); // Outputs: The lion says: Roar
}
}
Explanation:
using System;
Define a namespace named Pets
namespace Pets
Define a class named Animal within the Pets namespace
class Animal
Method to make the dog sound
public void Dogmakesound()
{
Console.WriteLine(“The dog says: Woof Woof”);
}
Prints the dog’s sound
Define a namespace named WildAnimals
namespace WildAnimals
Define a class named Animal within the WildAnimals namespace
class Animal
Method to make the lion sound
public void Lionmakesound()
{
Console.WriteLine(“The lion says: Roar”);
}
Prints the lion’s sound
Main program class
class Program
{
static void Main()
Create an instance of Dog from the Pets namespace
Pets.Animal myDog = new Pets.Animal();
Create an instance of Lion from the WildAnimals namespace
WildAnimals.Animal myLion = new WildAnimals.Animal();
Call the MakeSound method for the dog
myDog.Dogmakesound();
Outputs: The dog says: Woof Woof
Call the MakeSound method for the lion
myLion.Lionmakesound();
Outputs: The lion says: Roar
Course Video
Course Video In English
Task:
1. Namespace Organization:
Create a namespace named MyCompany.Utilities.
Inside this namespace, create a class named MathFunctions with a static method CalculateSquare(int number) that calculates and returns the square of the given number.
Use the namespace and class in the main method to calculate and print the square of a number like 10.
2. Using Directives:
Create a namespace named Geometry.Shapes.
Inside this namespace, create a class named Circle with properties Radius and a method CalculateArea() that calculates and returns the area of the circle.
Use the Geometry.Shapes namespace in the main method to create an object of the Circle class, set its radius, calculate the area, and print the result.
3. Namespace Aliases:
Create two namespaces: Finance and HR.
Inside each namespace, create a class Employee with properties Name, Id, and a method DisplayDetails() that prints the details of the employee.
Use namespace aliases to resolve any naming conflicts and create objects of both Employee classes in the main method, set their properties, and call the DisplayDetails() method.
.
A namespace in C# is a container that organizes classes, interfaces, enums, and other types, preventing name conflicts in large applications.
Namespaces help to avoid naming conflicts by grouping related classes and types, making code easier to manage and maintain.
Yes, namespaces can be nested within each other to create a hierarchy of namespaces.
The global namespace is the root namespace in C#. Types declared outside any namespace belong to the worldwide namespace.
Yes, two namespaces can have classes with the same name. You can specify which one to use with the fully qualified name, like Namespace1.ClassName.
The compiler will throw an ambiguity error unless you explicitly specify the fully qualified name of the class.
By grouping related types and avoiding conflicts, namespaces make the codebase more organized and easier to navigate.
The directive imports an entire namespace while using static imports only the static members of a type, allowing direct access to them.
Yes, Iqra Academy offers free online courses and tutorials in Hindi that explain C# namespaces with practical examples.
Here’s how you can use multiple namespaces:
namespace Namespace1 {
public class Class1 {
public void Show() {
Console.WriteLine(“Namespace1.Class1”);
}
}
}
namespace Namespace2 {
public class Class2 {
public void Show() {
Console.WriteLine(“Namespace2.Class2”);
}
}
}
// Using namespaces
Namespace1.Class1 obj1 = new Namespace1.Class1();
Namespace2.Class2 obj2 = new Namespace2.Class2();
obj1.Show(); // Output: Namespace1.Class1
obj2.Show(); // Output: Namespace2.Class2
Yes, free Hindi tutorials are available at Iqra Academy, explaining how to use namespaces and classes effectively in C#.
You can practice writing and using namespaces and classes in free online code editors like dotnetfiddle.net.