Advanced C# Programming (Part 1)
In this session, we discuss the basic structure of a C# program in Visual Studio, what a Namespace is, and the purpose of the Main method.
Join the DZone community and get the full member experience.Join For Free
In this session, we discuss on below points in C# programming using Visual Studio. These points are,
- The Basic Structure of A C# Program.
- What Is Namespace?
- Purpose of The Main Method.
Step 1: 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.
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.
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.
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.
Check Main Method Is The Entry Point
Here I need to create another method inside the same class called
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
Now run this program and check the output as shown below. It prints both messages.
Every console application should have a
Main method. The program starts executing from the
In this write-up, we have learned the below details,
- The basic structure of a C# program.
- What is namespace?
- Purpose of the Main method.
Thank You and Stay Tuned For More!
Opinions expressed by DZone contributors are their own.