Thursday, November 24, 2011

Learn C# Programming Tutorial Lesson 1 - Your First Program

Requirements

To be able to do this tutorial you will need to have a good understanding of how to use a computer. You will also need to either get Microsoft Visual Studio.Net or download the free C# compiler from Microsoft's website.

Writing the code

You have 2 choices when you write a C# program and they are to use the Visual Studio IDE to write your code or to use notepad. We will be using notepad. You can start by typing the following line in notepad.

using System;
The above code makes it easier for us to use certain commands in C#. Make sure that you use capital and small letters properly because C# is case sensitive. Next we must declare the program class. To do this you must use the class keyword and give the class a name. We will call it First. You must then put in some curly brackets to surround the class's contents.
using System;

class First
{
}

Now we have to create the Main function. The Main function is where a program starts executing commands from. You must always create a Main function or else your program will not be able to run. Here is how to create the Main function.
using System;

class First
{
   public static void Main()
   {
   }
}

The public keyword means that the function is accessible by anything. static means that the function must stay in memory. void is the return type. Main is the name of the function. The brackets are where you put optional program parameters but we have not for this example. You again use curly brackets to surround the commands in the Main function. The last thing we will do is use the Console.WriteLine command to write Hello on the screen.
using System;

class First
{
   public static void Main()
   {
      Console.WriteLine("Hello");
   }
}

A comment is something that is used to make a program easier to understand and is ignored and does not affect the way a program runs. A comment goes between a /* and a */ or after a //. Here is how you would comment the above program.
/* This program writes the
word Hello on the screen */


using System;

class First // class declaration
{
   public static void Main() // Main function. Program starts here
   {
      Console.WriteLine("Hello"); // Print Hello
   }
}

Save the file as First.cs and remember where you saved it.

Compiling and running

We are now ready to compile the program. When you compile a program it means you take the source code that you have just written and convert it to an executable program file. To do this we must open the Visual Studio.NET command prompt and enter the following.
csc First.cs
If you get errors while compiling then you need to go through this tutorial again and find out where your mistakes are. If there are no errors then it means that your program compiled successfully. To run the program enter its name at the command prompt. You should see the word Hello printed on the screen. Congratulations! You have just written your first C# program.

0 comments:

Post a Comment

Setec Institute || Group : SH6 | | Name : Kim Chanthy. Powered by Blogger.

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Hot Sonakshi Sinha, Car Price in India