Simple C# Program


To write a simple C# program, you need to understand some basic elements. They are part of almost every C# program. You must understand these elements to do programming in C#.


This simple C# program consist of following elements -

1. Class
2. Namespaces
3. Main() Method
4. Console Class and Console.WriteLine Method

There are other elements as well in this simple C# program but we will discuss them in greater detail ahead. Till then type this code and see the magic.


using System;

class Program
{
  static void Main(String[] args)
  {
       Console.WriteLine("Welcome To CheezyCode !");
       Console.Read(); // TO SEE THE OUTPUT 
  }
}


What is Class?


C# is an Object Oriented Language. What does it mean? It means everything has to be written in a class. Class helps you define a type which is used to represent a thing or a concept in a program.

What is Namespace?


Every class that you create must have a unique name. In large application where there are tons of classes including third party classes - naming each class differently is a daunting task. To avoid naming conflict and to organize your code well - we have namespace. Namespace organize your classes and other code pieces in your application and help giving unique names that do not conflict.

For e.g In this simple program, we have Console class. This class is available in System Namespace. To use Console class in your program you have to include System Namespace. That is why we have 
using System;  at the top. This indicates to the compiler to include classes available in System Namespace.  

What is Main() Method?


Main is a special method which designates the entry point for your application. Usually, there is only one Main() method to start your application. You can have multiple Main() methods but then you have to specify which should be used to start the application. Having multiple main is a very rare scenario.

What is Console Class and Console.WriteLine?


Console class is a part of .NET Framework Classes. It is available in System Namespace. This class is used to interact with the Console Window. Console.WriteLine is used to write down something onto console. Console.WriteLine("Welcome To CheezyCode");  This line prints Welcome To CheezyCode on the console window.



NOTE: If you press Start on Visual Studio(Press F5), console window will disappear before you see the output. Press Ctrl + F5 to run the program or add Console.Read() after Console.WriteLine statement to see the console window in the above program. I have added the line in the code there to see the output on the screen.


Let us know for any concerns or query. Happy Learning. (refer full series here - C# Tutorial Index)


Comments

Popular posts from this blog

Create Android Apps - Getting Started

Kotlin Type Checking and Smart Cast with Examples