Basic Structure of C# Program

Basic Structure of C# Program

In this article, I am going to discuss the Basic Structure of the C# Program using a Console Application. Please read our previous article before proceeding to this article where we discussed the Introduction & Environment Setup for C# Application development. As part of this article, I am going to discuss the following pointers in detail.

  1. What is C#.NET?
  2. Advantages of using the .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 the .NET Framework to develop different kinds 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 supports all 4 OOPs Principles such as Abstraction, Encapsulation, Inheritance, and Polymorphism.

Based on the features, we can define C# as 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 the .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 Web Applications; it requires language support. So, here we can use C# as the 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. Restful Web Services
  4. SOAP Based Services
  5. Console Applications
  6. Class Library
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. Interpreters, 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 become 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 systems 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 the C# Program using a Console Application.

Basic Structure of the C# Program

The above process is shown in the below diagram.

Basic Structure of the C# Program using a Console Application

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.

Creating the First Console Application using Visual Studio 2022

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.

Creating First Console Application using Visual Studio and C# Programming Language

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.

Creating First Console Application using Visual Studio and C# Language

Once you click on the Create button, visual studio will create the Console Application with the following structure.

Example to Understand the Basic Structure of a C# Program

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.

Example to Understand the Basic Structure of a C# Program

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.

Basic Structure of a C# Program

Once you click on the Start button, you should get the following console window showing the message.

Example to Understand the Basic Structure of a C# Program

Understanding the Code:

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

Basic Structure of a C# Program

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 included statements in the C programming language.
Syntax: using NamespaceName;
Example: using System;

If the required namespace is a member of another namespace, we have to specify the parent and child namespaces separated by a dot as follows:
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 some namespace. Generally, we put all the related classes under one namespace and in a project we can create multiple namespaces.
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 the 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 which the program execution is going to start.

Syntax:
class ClassName
{
}

Example:
class Program
{
}

Main() Method Section:

The main() method is the entry point or starting point of the application execution. When the application starts executing, the main method will be the 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 uses of using statements are there, which 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.

In the next article, I am going to discuss the Console Class Methods and Properties in detail. Here, in this article, I try to explain the Basic Structure of the C# Program step by step with one example. I hope you enjoy this article. 

8 thoughts on “Basic Structure of C# Program”

  1. blank

    Thank you for your great job, I have a remark you said that “In .NET applications, all classes related to the project should be declared inside one namespace.” it’s not necessary for example If a project contains several folders, each folder is a namespace.

  2. blank

    Thanks for giving clarity and easy understanding.
    Can you please provide a tutorial for “dependency injection in c#” which will be helpful for most of the developers

Leave a Reply

Your email address will not be published.