Saturday, February 19, 2011

Hello World!

The first thing you do when learning a new programming language is showing "Hello World!" on your screen by using the programming language that you're trying to learn. The reason we do this is because it's the simplest way to show something on the screen and to give you that extra motivation to keep going. The second reason is that it's a tradition after it was first introduced in the book "The C Programming Language".


First you have to start Visual Studio -> click File (upper left corner) -> click New Project -> click Console Application -> Fill in "HelloWorld" for the project name -> click OK.


Don't freak out, you don't have to understand everything all at once, except the three parts that are circled by red.

The Debug button (1) is where you click on to test your application. You basically just start the application within Visual Studio and check if your application has errors which will be shown in Visual Studio. If you click on it now you will see something opening and closing.

The Text Editor (2) is where you type your code. Your application starts in "static void Main(string[] args){ }" which is called a method. It's a written rule that your application will start in the method called "Main". Your first code will be written in-between the { } signs of that method.

The Solution Explorer (3) is a list of all the files that exist in your project and the made references to libraries that you are using in your project. You're going to write the code in a text file called "Program.cs" and the contents of that text file is shown in the Text Editor.


Let's get started. Type "Console.Write("Hello World!");" and on the next line type "Console.Read();".


"Console" is a class like "Program" is a class in your application right now. "Write" is the method that is in the class "Console" like "Main" is a method in the class "Program" of your application right now and both the classes and the class their methods are seperated by a dot. The only difference is that you won't be able to see what is inside the class "Console" (except a list of the method names it has) or the method "Write", because they're protected by the .NET Framework. "Hello World" is the parameter that you're giving to the method "Write". Basically you can type "Program.Main(null);" like you just did with "Console.Write("Hello World!");" (null means that you're giving it no data). Putting "Program.Main(null);" in your code is not a good idea though, because that will start your application an infinite times, which will destroy the world.

"Console.Write("Hello World!");" will write "Hello World!" on the console. "Console.Read();" will make the application wait until you press the "Enter" key on your keyboard. The application will close right away if you don't type "Console.Read();", because when a console application reaches the end of its "Main" method, the application closes.


Now click on the Debug button or press F5 on your keyboard to start your application. Your application will be read from top to bottom, so don't think every line of code will be executed at the same time. If your code is exactly like on the previous picture, then there should be no problems and the application should run perfectly. You might've forgotten a ; sign, so make sure you didn't. If everything went perfectly, then congratulations on your first C# console application! Save your project by pressing "Control + Shift + S" on your keyboard, unless you don't need to with your Visual Studio, because Visual Studio saved your project when you created it unlike my Visual Studio.

Source Code HelloWorld Project

>> Variables

1 comment: