Basic Structure of C Sharp Program

HTML
CSS
C#
SQL

Basic Structure of C# Program

C# is a powerful and versatile programming language that can be used to develop various types of applications, such as web, desktop, mobile, and cloud. C# is also an object-oriented programming language that supports the four principles of OOP: abstraction, encapsulation, inheritance, and polymorphism.

In this document, we will learn how to create a simple console application using Visual Studio and C#. A console application is an application that runs in a command-line interface (CLI) and does not have a graphical user interface (GUI). It is a good way to start learning C# basics and syntax.

Creating a Console Application

To create a console application using Visual Studio, we need to follow these steps:

  • Open Visual Studio and click on Create a new project.
  • In the search box, type console and select Console App (.NET Framework) using C# language. Click on Next.
  • In the Configure your new project window, enter the project name and solution name. You can use the same name for both or different names. Choose the location where you want to save the project. Select the .NET Framework version that you want to use. The latest version is 4.8. Click on Create.
  • Visual Studio will create a console application project with the following structure:

The project contains some files and folders that are essential for the console application. Let’s understand what they are:

  • Properties: This folder contains the settings and information about the project, such as assembly name, version, target framework, etc.
  • References: This folder contains the references to the libraries and packages that are used by the project. By default, it contains some system libraries that provide basic functionality for the console application.
  • config: This file contains the configuration settings for the application, such as app settings, connection strings, etc.
  • cs: This file contains the C# code for the console application. It has a class called Program that has a method called Main. This is the entry point of the application where the execution begins.
Writing C# Code

To write C# code for the console application, we need to open the Program.cs file and edit the Main method. The Main method is a special method that is executed when the application starts. It can have some parameters and a return type, but for simplicity, we will use a void method with no parameters.

The Main method can contain any C# code that we want to execute. For example, if we want to display a message on the console, we can use the Console.WriteLine method, which is a built-in method that writes a line of text to the standard output stream. The Console class belongs to the System namespace, which is a collection of classes and interfaces that provide basic functionality for the .NET Framework.

To use the Console class or any other class from the System namespace, we need to import the namespace using the using keyword. The using keyword allows us to use the classes and interfaces from a namespace without specifying the full name every time. For example, instead of writing System.Console.WriteLine, we can write Console.WriteLine if we have imported the System namespace.

The using keyword can also be used to import other namespaces, such as user-defined namespaces or namespaces from other libraries and packages. For example, if we want to use the Math class from the System namespace, we can write using System.Math and then use the Math class without the System prefix.

The using keyword can also be used to create aliases for namespaces or types, which can be useful when there are name conflicts or when the names are too long. For example, if we want to use the System.Collections.Generic namespace, which contains generic collections, we can write using Generic = System.Collections.Generic and then use the Generic alias instead of the full name.

The using keyword can also be used to create a scope for an object that implements the IDisposable interface, which is an interface that provides a method to release the resources used by the object. For example, if we want to use a StreamReader object to read a file, we can write using (StreamReader sr = new StreamReader(“file.txt”)) and then use the sr object to read the file. The using statement will automatically call the Dispose method of the sr object when the scope ends, which will close the file and release the resources.

Here is an example of how to write C# code to display a message on the console using the Console.WriteLine method:

using System; // Importing the System namespace

namespace MyFirstProject // Declaring a user-defined namespace.
{
class Program // Declaring a class.
{
static void Main() // Declaring the Main method
{
Console.WriteLine(“Welcome to C#.NET”); // Writing a message to the console.
     }
  }
 }

A C# program consists of the following sections:

  • Using section: This section contains the statements that import the namespaces from the .NET framework or user-defined namespaces. These namespaces contain classes and interfaces that are used in the program. For example, using System; imports the System namespace that contains the Console class.

  • Namespace declaration: This section declares a user-defined namespace that contains the classes and interfaces related to the project. A namespace is a container that groups related types. For example, namespace MyFirstProgram {} declares a namespace called MyFirstProgram.

  • Class declaration: This section declares a class that contains the fields, properties, methods, and events that define the behavior and data of the program. A class is a blueprint for creating objects. For example, class Program {} declares a class called Program.

  • Main method: This section contains the main method that is the entry point or starting point of the program. The main method contains the logic and statements that are executed when the program runs. For example, static void Main(string[] args) {} declares a main method that takes an array of strings as a parameter.
Running the Console Application

To run the console application using Visual Studio, we need to click on the Start button or press Ctrl+F5. This will build and run the application and open a console window that shows the output of the application. For example, if we run the above code, we will see the following output:

Welcome to C#.NET

To close the console window, we can press any key on the keyboard.