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 check Krishnamurthy Number

In this tutorial, we will learn about Krishnamurthy numbers and write a Krishnamurthy Number program in Java. We will write two programs for Krishnamurthy number …
Read More

Java Program for ISBN Number

In this tutorial, we will learn about the ISBN (International Standard Book Number) and write a program to check for the ISBN Number in Java …
Read More

C++ Memory Management

Memory management refers to the process of managing the computer memory while assigning the space to the program’s variable to improve the overall performance. Requirement …
Read More

C++ Pointer to Pointer

As we know by now that a pointer stores the address of the pointed variable. But it is not the only use, pointer also stores …
Read More

C++ Passing Pointers to Functions

A function is a user-defined block of codes that executes some specific task assigned to it invoked by its name. If there is an argument …
Read More

C++ Null Pointer

We use a null pointer when we do not have the exact address to assign to a pointer. It is considered a good practice and …
Read More