[edit] C# Hello World
Lets start off with some very simple code. I will guide you through it line by line. Now it is the time to open your .NET studio environment and copy and paste the following code in the compiler. I will explain the components of the C# code. Certain parts of the code will be colored or to distinguish between identifyers, keywords and regular code.
using System;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World");
}
}
}
Copy and paste this code in your microsoft C# editor. When you run this code you should see a black DOS screen appear then disappear. It does not give you a chance to view the output. This is because the code executes then unloads itself. I will show you a trick to pause the execution so you can have a chance to view the results. But, first lets go over the code above.
[edit] Line by line
[edit] using System
The first line of code above tells the compiler to use the System namespace. System enables you to use System commands such as Console.WriteLine instead of System.Console.WriteLine it is part of the System namespace. Whatever, command that you use you also must include its namepace otherwise the keyword will not work and your program will not compile correctly. you can specify multiple Namespaces using the using keyword.
Try deleting the very first line in your code to see what happens.
[edit] namespace ConsoleApplication2
Namespace declares the space where duplicate types and variable cannot reside. If you want to declare duplicate types and variables declare another namespace and place your data types in that space. Everything within the namespce block must have different names. I will cover techniques to access variables and data across different namespaces.
|
Note: The namespace keyword is not required in your program. It exists to section large amounts of code, it also makes many lines of code easier to read.
|
[edit] class Program
class Program names our piece of code Program. So, when you save or compile this code it will be named Program.
[edit] static void Main(string[] args)
A member declared in this class will be part of the class program. Main is a method of class Program. It is a special method that tells the computer the start of a program. So, this is where the start of the actual code begins. There are 4 separate techniques to start the Main section which will be explain later in this tutorial. Just remember that this section of the code marks the beginning of where you start to put your custom code.
[edit] Console.WriteLine("Hello World");
Console.WriteLine is a statement that tells the computer to write something to the screen. In this case it writes Hello World.
- You must use the System namespace to tell the computer where to find the Console class. Without using the system namespace the compiler will issue an error because it does not know where to find Console.WriteLine.
- Each statement must end by a semicolon.
[edit] Pause C# results
When you run the C# Hello World program above you will see a DOS screen appear then disappear and not allowing you to view what the program results. This is because the program tells the computer to run the program then unload when done. To halt the program once it is finished processing all of the commands you can trick the computer by forcing it to ask you a question before it terminates. This technique stops the processing and waits for you to enter something while giving you time to view all the output of the program. Here is how to use the C# program:
Its the same program as above but it just added the Console.Read(); command at the end.
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World");
Console.Read();
}
}
Run this C# code and you will be able to view your program output.
|