Simple C# Program Example with Explanation

Now that you have gained the basic knowledge about Object Oriented Programming Concepts, Lets write a simple C# Program and try to dissect it.

This is a sample C# Program to help you 
kick-start with C#.NET.
public class MyFirstClass
    // The main() method is the starting point of execution of a C# Program
    public static void main(String[] args)
		Console.WriteLine("Hurray!! My First C# Program actually Executes !!!");


Dissection of a C# Program

1. Multi Line Comment

 Multi line comments in C#

The first few lines starting with "/*" and ending with "*/" are multi-line comments. Multi line comments are useful while describing about a method or a class to help other developers understand the code.

2.  public class MyFirstClass

 The line "public class MyFirstClass" states that a new class is being created.

 class declaration in c#

The statement "class MyFirstClass" will also work  as the default access specifier in C# is "Default".


3.  public static void main (String[] args)

The meaning of "public static void main (String[] args)" is explained through the following figure.

 main method declaration in c#

  1. Public Access specifier allows the method to be accessed from outside the class.
  2. Static means that to call the main method an instance variable of the class is not required. It can be accessed directly using ClassName.functionName()
  3. void means that the method main does not return anything.
  4. The arguments String[] args means that main accepts as arrays of Strings . In this case since main is called by the OS, the arguments of main() method is a list of command line arguments.

4. Single Line Comment in C#.NET

Single line comments start with // and are used often to provide short description of statement in a C# program. 

single line comment in c# 

5. Console.WriteLine

The statement "Console.WriteLine" prints the parameter passed to it on the console by default.

It can also evaluate an expression passed to it as an argument and then print the result of the expression.

There are two variations of it:

  1. Write() : It prints in a single line and does not append a newline character at the end.
  2. WriteLine() : It appends a newline character at the end.


writing console output

Banner to

Hide Page Information

Your First C#.NET Program Example with Explanation

We have tried to explain a basic C#.NET Program in detail in this tutorial

What is the meaning of public static void main?

We have explained in this article about simple program