What is a C# Namespace?

What is a C# Namespace?

What is a Namespace?

Namespaces are structures that allow us to keep the code classes we create in a more organized manner. In fact, they allow us to categorize our code and facilitate its accessibility.

For C#, we call namespaces at the beginning of our project using "using". If we need to give an example of a namespace:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

As you can see above, there is a namespace under the name "System", and in the next example, there is a namespace under the name "System.Collections.Generic".

As can be explained from the example here, with namespaces we can actually find the classes we are looking for more easily and include them in our project. You can categorize and classify multiple classes you write for your project with the help of namespaces, and you can include and easily use these namespaces in the places you specify in your project.

Example of Namespace Usage

namespace SomeNameSpace
{
    public class MyClass
    {
        static void Main()
        {
            Nested.NestedNameSpaceClass.SayHello();
        }
    }

    // a nested namespace
    namespace Nested
    {
        public class NestedNameSpaceClass
        {
            public static void SayHello()
            {
                Console.WriteLine("Hello");
            }
        }
   

As you can see above, you can write your classes inside a namespace and then call and use them as in the "System" example at the very beginning of the text.