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