C# Program Structure3 min read

Before we begin further in this tutorial, it is important to know the basic building of C# programming. You need to have the basic knowledge on what C# program structure contain of.

C# Program Structure

Let us check the basic program written in C#.

After execution of the above program, following output will be displayed.

Now let us understand each of these parts individually:

using System; This is first-line, “using” is a keyword in C# that is used to include the System namespace in the program. If System is included before in the program, then we do not need to specify a system for accessing the class of this namespace. In the above program, we use the Console without specifying the System.

/* Hello World Program */: This is the comment section of the program. These lines are not included during execution but these are for the programmer to comment on the necessary section as required. It is considered to be good practice for coders. However, you can also use // for single line comment and /**/ is used for multiple line comment.

namespace: Next is a namespace, It is the collection of classes that is we can create a class inside the namespace in order to maintain the classes in categories. We need to give that namespace name (above eg:- “HelloWorldApplication”).

public class: public is one of the modifiers used in C#. Making the class public means that it can be accessed from outside the class. class in C# contains all the data and methods used in a program and multiple methods can be created inside the class (above example, Main() is the method inside HelloWorld class).

Main: The main method is an entry point of the program when executed. The execution of the program starts from the Main method.

Console.WriteLine("Hello World!"); The Console class is defined in the namespace as used on first line of the program and WriteLine is the method of the Console class that displays the message on the screen.

{...}: This curly braces signifies the extent of the particular function. The blocks under the { } cannot be used outside the curly braces.

Compilation and Execution of C# Program

Go through the following step to compile and run the C# programs using the command-line.

  • First, open a text editor(eg notepad) on your computer and write your C# code.
  • Then, save the file as file_name.cs
  • Then open a command prompt and go to the directory where you have saved the file.
  • And on the command prompt type “csc file_name.cs” and press enter to compile your code. If there is no error then the command prompt takes you to the next line and the file_name.exe executable file will be created.
  • Type file_name to execute the program.
  • Then Hello world will be printed on the screen (considering the above code).

C# Basic Syntax

During the program, we use many programming components such as keywords, variables, member function, member variables, etc, which we will be studying in detail in further tutorial.

You can also write the C# program in 4 different ways as shown below.

1. Simple Syntax

2. Using System

3. Using modifier (public)

4. Using namespace

You can write C# program in any one of the above ways in a program according to your need and your comfortability.


String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More

Inverted Half Pyramid Pattern of Alphabets in C

In this tutorial, we will write a C program to print half Pyramid using alphabets/characters. Before that, you may go through the following topic in …
Read More