C# Namespaces
Objectives
- What is Namespaces?
- How to use namespaces?
- How to creating custom namespaces
What is Namespaces?
Namespaces are used to organize and group related classes, structs, interfaces, enums, and delegates.
Namespaces help in organizing large projects, improving code readability, and facilitating code reuse.
How to Use Namespaces?
To use a namespace in, you typically include the 'using' directive at the beginning of your code file.
The 'using' directive informs the compiler to look for types within the specified namespace.
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
Creating Custom Namespaces:
To create a custom namespace, you can define it using the 'namespace' keyword followed by the namespace name and its contents enclosed in curly braces {}.
namespace MyNamespace
{
public class MyClass
{
public void MyMethod()
{
Console.WriteLine("MyMethod called.");
}
}
}