Lession - 1 Basic Structure of C# Program

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

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.

Advantages of using the .NET framework from the C# point of view.


1. It provides GUI features. Earlier programming languages like C and C++ do not support GUI
features but C#.NET will provide complete GUI features. All GUI features are getting from the
framework.
2. We can connect with any database and perform the operations. Using ADO.NET and Entity
Framework technologies, we can perform the DB operations with any Database. ADO.NET
and Entity Framework are also a part of .NET Framework.
3. The Framework also helps us to develop WEB Based applications. Using ASP.NET
technology we can develop WEB Based Applications. ASP.NET itself alone cannot develop
the Web Applications; it requires language support. So, here we can use C# as programming
language. ASP.NET is also a part of the framework.

Different Types of Applications are Developed using C#.NET.


1. Windows Applications
2. Web Applications
3. Console Applications
4. Class Library
5. Restful Services
6. SOAP Based Applications. Etc.

What is the Visual Studio?


Visual Studio is one of the Microsoft IDE tools. Using this tool, we can develop, build, compile and run
applications with the .NET framework. This tool provides some features such as
1. Editor
2. Compiler
3. Interpreter, and many more

What is a Console Application?


1. A console application is an application that can be run in the command prompt. For any
beginner on .NET or anyone who wants to learn C# Language or anyone who wants to
becomes an expert in C# Language, building a console application is ideally the first step to
learning the C# Language.
2. The Console Applications contain a similar user interface to the Operating System like MS-
DOS, UNIX, etc.
3. The Console Application is known as the CUI application because in this application we
completely work with the CUI environment.
4. These applications are similar to C or C++ applications.
5. Console applications do not provide any GUI facilities like the Mouse Pointer, Colors, Buttons,
Menu Bars, etc.

Basic Structure of the C# Program


Now, let’s understand the Basic Structure of C# Program using a Console Application.

The above process is shown in the below diagram.


Note: C#.NET is a Case-Sensitive Language and Every Statement in C# should end with a
Semicolon.

Example to Understand the Basic Structure of a C# Program:


Now, we are going to use Visual Studio to create a Console-Type Project. Then we are going to use
the console application to display the message “Welcome to C#.NET”. Then, we will also see how to
build and run the Console Application using Visual Studio GUI.

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.

Understanding the Code:


Using Visual Studio, if we are creating a console application, then automatically we are getting four
sections which is shown in the below image.

Let’s understand each of these sections in detail.

Importing Namespace Section:


This section contains importing statements that are used to import the BCL (Base Class Libraries) as
well as user defined namespaces if required. This is similar to the include statements in the C
programming language.
Syntax: using NamespaceName;
Example: using System;

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;

Namespace Declaration Section:


Here a user-defined namespace is to be declared. In .NET applications, all classes related to the
project should be declared inside one namespace.
Syntax: namespace NamespaceName {}
Example: namespace MyFirstProject {}
Generally, the namespace name will be the same as the project name but it is not mandatory, you can
give any user defined name to the namespace.

Class Declaration Section:


This is to declare the start-up class of the project. In every .NET Desktop application like console and
windows, there should be a start-up class. In console application, the start-up class name is
Program.cs. A start up class is nothing but a class that contains a Main() method from where the
program execution is going to start.
Syntax:
class ClassName
{
}
Example:
class Program
{
}

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.

You might also like