In this session, we discuss on below points in C# programming using Visual Studio. These points are,

  1. The Basic Structure of A C# Program.
  2. What Is Namespace?
  3. Purpose of The Main Method.

Step 1: Create a console application:

Create a Console Application

Step 2: Open the Program.cs file and the content will be shown as below.

Here I will remove unwanted namespace declaration along with other lines of code as shown below. Make sure to compare these code blocks.

Step 3: Here I need to show a sample message to the end-user.

Here console is the class to read and write from and to the console and here I used the method called WriteLine() to show a sample message on the console window. So, to run the console application just use the ctrl+f5 key combination. You will get a message as shown below.

WriteLine() Method

Code Description

Here the namespace declaration is used that is system namespace.

This line tells that the rest of the code that we used here these are present inside that system’s namespace.  A namespace is used to organize your code and is a collection of classes, interfaces, structs, enums, and delegates. A namespace is a collection of classes. A namespace can contain other namespaces.

Namespace: System.Configuration

If you omit the using System, declaration, then you have to use the fully qualified name of the Console class as below otherwise we get a compile time error that the name ‘Console’ does not exist in the current context.

Compile Time Error

Here we can show the same message without using declaration as shown below:

But we need to avoid the above approach because instead of using system keyword every time inside code we need to system namespace declaration and we can access the classed, methods using system namespace as described earlier. That helps us less typing code and avoid complexity.

Another more important thing is class. Here class name is Program. Whatever we write code inside class block here only.

The next one is a function or method called Main(). It is the entry point of our console application. A function will have access modifiers, static modifiers, return types, method parameters, method names, etc.

Here return type is void and access modifier is Public and no method parameter passed inside brackets. The name of the function is Main. This function tells the program execution from starting curly brace and print the message then end at another curly brace. The below image will show the step-by-step execution of the Main method.

Main Method Execution

Check Main Method Is The Entry Point

Here I need to create another method inside the same class called Main1.

Let’s check which method will execute first and print the message. The Main method will execute first and print the message but the Main1 method will not execute.

But I want to print the message of the Main1 method. To do so, I need to call the Main1 method inside the Main method as shown below. So, that first the Main method will execute, then the Main1 method.

Now run this program and check the output as shown below. It prints both messages.

Running Program

Every console application should have a Main method. The program starts executing from the Main method.


In this write-up, we have learned the below details,

  1. The basic structure of a C# program.
  2. What is namespace?
  3. Purpose of the Main method.

Thank You and Stay Tuned For More!

Source link

Write A Comment