Lession - 1 Basic Structure of C# Program
Lession - 1 Basic Structure of C# Program
Lession - 1 Basic Structure of C# Program
Now, we going to discuss the Basic Structure of C# Program using a Console Application. Here, we
are going to discuss the following pointers.
1. What is C#.NET?
2. Advantages of using .NET Framework from the C# point of view.
3. Different Types of applications are developed using C#.NET.
4. What is the visual studio?
5. What is a Console Application?
6. How to Create a console application using the visual studio?
7. Understanding the basic structure of a C# Program.
Importing Section
Namespace Declaration
Class Declaration
Main() Method
So, here, first, we will understand what is C#.NET and Visual Studio and what type of applications we
can develop using C#.Net. Then we will discuss the basic structure of a C# program using a console
application.
What is C#.NET?
1. C#.NET is one of the Microsoft Programming Languages to work with .NET Framework to
develop different kind of applications such as Web, Console, Windows, etc.
2. It is the most powerful programming language among all programming languages available in
the .NET framework because it contains all the features of C++, VB.NET, JAVA, and also
some additional features. As we progress in this course, you will come to know the additional
features.
3. C#.NET is a completely Object-Oriented Programming Language. It means it support all 4 the
OOPs Principles such as Abstraction, Encapsulation, Inheritance, and Polymorphism.
Based on the features, we can define C# is a simple, secure, robust, portable, platform-
independent, architectural neutral, multithreaded, object-oriented programming language with
a strong type exception handling and type checking mechanism for developing different kinds
of applications such as Web, Windows Form, Console, Web Services, Mobile Apps, etc.
Step1
First, open Visual Studio 2022 (the latest version at this point in time) and then click on the Create a
New Project option as shown in the below image.
Step2
The next step is to choose the project type as a Console Application. Type Console in the search
bar and you will see different types of console applications using C#, and VB languages and using
both .NET Framework and .NET Core / .NET. Here, I am selecting Console App (.NET Framework)
using C# Language and then clicking on the Next button as shown in the below image.
Step3
The next step is to configure the new project. Here, you need to provide the project name and solution
name. You can also give the same name to both project and solution but it is not mandatory. Here, I
am providing the name MyFirstProject to both project and solution. You need to provide the location
where you need to create the project. Here, you also need to provide the .NET Framework version
that you want to use in this application. The latest version of the .NET Framework is 4.8. So, I am
selecting .NET Framework 4.8 and then clicking on the Create button as shown in the below image.
Once you click on the Create button, visual studio will create the Console Application with the
following structure.
A project called MYFirstProject will be created in Visual Studio. This project will contain all the
necessary required files to run the Console application. The Main program called Program.cs is the
default code file that is created when a new console application is created in Visual Studio. This
Program.cs class will contain the necessary code for our console application. So, if you look at
the Program.cs class file, then you will see the following code.
Step4
Now let’s write our code which will be used to display the message “Welcome to C#.NET” in the
console window. To do so, modify the Main method of the Program class as shown in the below code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyFirstProject
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Welcome to C#.NET");
Console.ReadKey();
}
}
}
Step5
The next step is to run the .NET Application. To run any program in Visual Studio, you just need to
click on the Start button or you can press CTRL+F5 as shown in the below image.
Once you click on the Start button, you should get the following console window showing the
message.
Note: If the required namespace is a member of another namespace, we have to specify the parent
and child namespaces separated by a dot.
using System.Data;
using System.IO;
Main() method:
The main() method is the entry point or starting point of the application execution. When the
application is start executing, the main method will be first block of the application be executed. The
Main method contains the main logic of the application.
What is using?
Using is a keyword. Using this keyword, we can refer to .NET BCL in C# applications i.e. including the
BCL namespaces as well as we can also include user defined namespaces that we will discuss as we
progress in this course. Apart from importing the namespace, other usage of using block is there, that
we will also discuss as progress in this course. For now, it is enough.
Note: In .NET the base class libraries are divided into a collection of namespaces. Each namespace
contains a set of predefined classes and sub-namespaces. The namespace contains another
namespace called sub-namespaces.