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.


MORE

Java Program to find the sum of the Largest Forward Diagonal

in this tutorial, we will write a java program to find the sum of the Largest Forward Diagonal in an Arraylist (matrix). Java Program to …

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a numberĀ using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …