

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


We’ll be using Visual Studio, Microsoft development environment for building our C# programs. There are many others 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 free edition from http://www.microsoft.com/express/. Let’s first take a look at simple C# console application. Don’t worry about the things if you don’t get it yet. We’ll explain everything step by step. For now just look at 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 an additional 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.
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 depending upon your program.
After adding the namespace to our program we have defined our class on a new line. Adding spaces and new line in your program enhance 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 class keyword followed by the class name. For example:
class program{ //some code here