c##

C# Hello World Program

C# Hello World Program

C# Hello World Program is easy to learn. We’ll be using Visual Studio, Microsoft development environment for building our C# programs. There are many other ways to build C# applications but we recommend you to use visual studio because it is free and most widely used IDE. If you don’t have visual studio installed, you can easily download the free edition from http://www.microsoft.com/express/.

Let’s first take a look at simple C# Hello World console application. Don’t worry about the things if you don’t get it yet. We’ll explain everything steps by step. For now, just look at the code given below.

using System;
   class program
    {
        public static void Main(string[] args)
        {
        }
    }

The above example doesn’t do anything useful yet. These are just some empty lines of code. To make it do something you have to add some additional lines between the most inner curly braces of code. Below is the updated version of the above example.

using System;
   class program
    {
        public static void Main(string[] args)
        {
         Console.WriteLine("My first program...!");
        }
    }

Now you have written your first complete program in C#. Let’s take look at each part of the code step by step. You have noticed that the program is starting with a line beginning with using.

   using System;

These are called directives. These using directives help the C# compilers to work with what external resources or code this specific program will be using. These are must in every C# program otherwise you will end up having a lot of errors. The reason behind is that every C# program depends upon other pre-built .Net Framework class library. For example, in our updated version of the example, we have added a line that will print the “My First program” on the console screen. This line of code uses components from .Net Class library to display this message. The text after the using directive is a namespace. In our case it is System.

Related Articles

A namespace is a collection of classes and types. Every program you write in C# depends on other namespaces, even the most basic program cannot be built without using any other namespace. These namespaces contain pieces of code that can be reused without re-inventing the wheel. Fortunately, Microsoft has a large pre-built namespaces library. In our example, we have used a single namespace but you can use many as you want to depend upon your program.

After adding the namespace to our program we have defined our class on a new line. Adding spaces and a new line in your program enhances its readability. These spaces are also called whitespaces and usually ignored by the compiler. A C# class is a group of logically related members (for example variables and methods) that makes things simple to implement. Every C# program consists of pre-built classes and programmer-defined classes. You can define a class by using the class keyword followed by the class name. For example:

  class program{
    //some code here
  }

Keywords (e.g. class) are the reserved words for use in C#. These letters are always in lowercase. The name of the class is called an identifier. An identifier is a user-defined keyword that denotes something. The {} (curly braces) indicates the starting and end of the class body. The line starting with // are called comments. These lines are ignored by the compiler and used for describing the purpose of the program or piece of code inside a program.

Inside our class, we have the Main function also called a method. This is the entry point of our program. Without this Main method, our program is not executable. Every C# program contains one Main method that defines the entry point of the program. C# is a case sensitive language. It means writing main is not the same as Main.

Now examine the next line of code which outputs the message on the console screen. The Console is a class that displays console screen and WrtiteLine() is the method of this class that writes the message on the console screen.

 

Difference between C++ and C#

Show More
यौगिक किसे कहते हैं? परिभाषा, प्रकार और विशेषताएं | Yogik Kise Kahate Hain Circuit Breaker Kya Hai Ohm ka Niyam Power Factor Kya hai Basic Electrical in Hindi Interview Questions In Hindi