Friday 8 February 2013

C# Hello World (Console)

This is a short tutorial for your first application in C#. Although there are many implementations of C# (like Mono), I will provide tutorial in Microsoft's Visual Studio.

First we will create "Hello World" console application. These are the steps:

Click on "File"->"New"->"Project" or shortcut "CTRL+SHIFT+N".

New window will appear. On left side you can see different languages. Select Visual C#. This will open a list of most common project types on the right side of the window. Select Console Application. Now we have to name it - type MyFirstApplication in name field. While you do this, solution name will change accordingly. If you want you can also change default location of your project. When you are done click "OK" button.



Again new window appears with your first class already generated. You will see some premade code that looks like this:

 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Text;  
 namespace MyFirstApplication  
 {  
   class Program  
   {  
     static void Main(string[] args)  
     {  
     }  
   }  
 }  

Now add a line Console.WriteLine("This is my Hello World!"); within Main method so  it looks like following:

 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Text;  
 namespace MyFirstApplication  
 {  
   class Program  
   {  
     static void Main(string[] args)  
     {  
       Console.WriteLine("This is my Hello World!");  
     }  
   }  
 }  

Console.WriteLine("bla bla bla"); is used to print out whatever is contained within quotation marks (in this case it will print out bla bla bla.

To start program click on green "play" button or press F5. This will run your program, however you will most likely not be able to see what it prints out because console closes too fast. To avoid that we will add another line of code that requires user to press enter before console closes. Add this line below the last one
Console.ReadLine(); so that code looks like this:

 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Text;  
 namespace MyFirstApplication  
 {  
   class Program  
   {  
     static void Main(string[] args)  
     {  
       Console.WriteLine("This is my Hello World!");  
       Console.ReadLine();  
     }  
   }  
 }  

Now try running it again (F5). This time you can see the printed text and console waits for you to press enter in order to finish running.

Now let's try to enter a line of text and print out the text we entered. For this we need to create a String. It will save whatever we write in one line and then we can use it to print out what has been saved in it. To do this we need to insert two lines above our current code. First line is the declaration of String with name "message" and it should be String message;. Second line is for storing text from whatever line you write on keyboard and it should be message = Console.ReadLine();. Your code should now look like this:

 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Text;  
 namespace MyFirstApplication  
 {  
   class Program  
   {  
     static void Main(string[] args)  
     {  
       String message;  
       message = Console.ReadLine();  
       Console.WriteLine("This is my Hello World!");  
       Console.ReadLine();  
     }  
   }  
 }  

If you try to run it it will first request from you a line of text (when you press enter it will finish taking text), but it will print out This is my Hello World!. In order to change that we need to make it print out our message. To do this we simply remove "This is my Hello World!" and write message inside parentheses. Now the code should look like this:

 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Text;  
 namespace MyFirstApplication  
 {  
   class Program  
   {  
     static void Main(string[] args)  
     {  
       String message;  
       message = Console.ReadLine();  
       Console.WriteLine(message);  
       Console.ReadLine();  
     }  
   }  
 }  

And that's it!

No comments:

Post a Comment