Tuesday, March 27, 2012

Learning to Code - C# Hello World

Links:
Learning to Code - Introduction

Start

Open Notepad on your windows operating system of choice.
Type the following code into your Notepad window:

 class HelloWorld  
 {  
   static void Main()  
   {  
     System.Console.WriteLine("Hello, world!");  
   }  
 }  

Now save this file as HelloWorld.cs

Now you have a "program" it is a really simple console application that will show the words "Hello, world!" when it is run.  But the first thing we have to do is to convert the human readable file (Specifically in the Microsoft .Net C# language) to one that can be read and executed by a machine (Binary Code, think lots or 1's and 0's strung together).  This is called "Compiling" and the program that is used to compile this is called CSC.exe.

The compiler can be found at the following location(s) on your computer depending on what version of the .Net compiler is installed :

The x86 locations are...
C:\Windows\Microsoft.NET\Framework\v2.0.50727
C:\Windows\Microsoft.NET\Framework\v3.5
C:\Windows\Microsoft.NET\Framework\v4.0.30319

The 64bit locations are...

C:\Windows\Microsoft.NET\Framework64\v2.0.50727
C:\Windows\Microsoft.NET\Framework64\v3.5
C:\Windows\Microsoft.NET\Framework64\v4.0.30319

Open up either a DOS window, or if your a power user you can open a Powershell window.
Change to the directory where your HelloWorld.cs file is, for example on my machine I would type:

CD D:\jp\prj\HelloWorld

Then call the csc.exe file at the location you chose with one paramater which is the name of your C# Helloworld file for example:
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe HelloWorld.cs

This will compile your file into a file that can be executed from your DOS console. It will be named HelloWorld.exe

Now you can run your first C# console application


HelloWorld.exe
Hello, world!

Next we will examine just what that code is doing.









No comments:

Post a Comment