C# namespace

www.‮gi‬iftidea.com

In C#, a namespace is a way to group related classes and other types together. Namespaces are used to organize code, avoid naming conflicts, and make it easier to understand and maintain code.

Here's an example of a namespace:

namespace MyNamespace
{
    public class MyClass
    {
        // ...
    }

    public enum MyEnum
    {
        // ...
    }

    // ...
}

In this example, the MyNamespace namespace contains a class called MyClass and an enum called MyEnum.

To use a type that's defined in a namespace, you need to specify the namespace using the using keyword, like this:

using MyNamespace;

MyClass myObject = new MyClass();

In this example, the using MyNamespace; statement tells the compiler to use the types defined in the MyNamespace namespace. You can then use the MyClass type without specifying the namespace every time.

If you have multiple namespaces with the same type names, you can use the fully qualified name to specify which type you want to use. For example:

using System;

System.Console.WriteLine("Hello, world!");

In this example, the using System; statement tells the compiler to use the types defined in the System namespace. The Console.WriteLine method is then called using the fully qualified name, System.Console.WriteLine.

Namespaces are an important part of organizing and maintaining large codebases in C#. By grouping related types together, you can make your code easier to understand and use.