Namespace and Main Method in C Sharp Programming

Namespaces

Main Method and Namespaces. If we want to understand what the namespaces are, we need to first understand why we need them. Well, there are two main reasons why we need namespaces.

Namespaces are also called Libraries

  1. The namespaces are used to organize your Classes.
  2. The namespaces are helpful when you want to call a Class.

The namespaces are used to organize your Classes

They are used to resolve the same class name conflict. First, understand this, a namespace is a folder where your classes, structs, interfaces, enums, delegates get stored. You already know, you can’t use the same name for two different files in the same folder. In case you really want to create two files with the same name you will have to create another folder and save the one file in that folder.

A class is also a file where your code is written that gets compiled by the compiler and processed by the processor. If you want to create two classes with the same name, you will have to create another library and save your class in that library. By following this method you can create n number of classes with the same name in different libraries.

The namespaces are helpful when you want to call a class

Whenever you want to use a class from a particular library, you will have to let the compiler know the name of that library. The keyword “using” is used to tell the compiler about the library name. Do not worry if you do not understand it at this point, things will be easy to understand with an example program. Look at the program below.

using System;
namespace Namespace
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to C Sharp");
            Console.ReadLine();
        }
    }
}
namespace

As you can see in the program above, the library named System has the definition of Console class. The System library is already saved in the .net Framework. In the below screenshot you can see the System Library is removed and the Console class is not being recognized by the compiler.

Can you use a class without adding its namespace?

Yes, you can do it but you will have to use the Fully Qualified Domain Name. That means you will have to mention the library name with the class name. Look at the example below.

using System.Collections.Generic;
namespace Namespace
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Console.WriteLine("Welcome to C Sharp");
            System.Console.ReadLine();
        }
    }
}

Look at the example above, I have added the System with Console and the compiler has no problem. Because System Library is already available in the .net Framework and it is found by the compiler when it searches for it.

Whenever you use the library and class name together, it becomes fully qualified domain name. Using Fully Qualified Domain Name is the repetition of code because each time you will have to mention the namespace. It is better we use the ‘using’ phrase and mention it at the beginning of the program.

Main Method

The Main Method is the entry point of your program. It is the default method inside the program which gets created when you create your program, you can not avoid it, you can not rename it. The Main Method gets executed by .net Framework when you run your program and it calls other methods inside your program to perform the desired task. You can see the Main Method in the above-mentioned example program where the Main Method is a static program. What is a Static method is the topic for another article that I will be writing very soon.

I hope the article was helpful. In case you have any queries, feel free to comment and share your feedback.

Take Care Guys!

Share your love
Nadeem
Nadeem

Leave a Reply

Your email address will not be published. Required fields are marked *