namespace HelloWorld
{
This line declares that everything inside the curly braces {...} is within the HelloWorld namespace. The reason that it is named HelloWorld is simply because that is what I named our first Console program when I created it in Visual Studio 2008...We will discuss namespaces in greater detail at a later time.
public class Program
{
Now this statement indicates that everything inside the curly braces {...} is part of the Program class (and public indicates that it is accessible outside the code). Program is the default name for the class because Visual Studio names it the same as it names the source code file. IMPORTANT NOTE: If you wish to change the name of the class, or the name of the source file, you may do so, but you MUST change them both to the same name. For our simple program, a Single class will do. We will discuss this further in the Section about Classes/Objects.
public static void Main()
{
Now this would be our very first method header. Every program has at least one method (and only one named Main). The words 'public' and 'static' are access specifiers. They indicate how other parts of the program can access the Method. In this case, public indicates that Main will be available to code outside the program (because it is called from the Operating System). static indicates that the method belongs to the class Program (and not objects which are instances of the class Program) and void means that there is no return type for the method named 'Main'. More on this in the Method and Classes sections.
Next up, Comments. Although our program is simple, we may still want to document what does what, and perhaps who did what...Two types of comments below are:
//This is a comment on a line
/*
This is a multi-lne comment.
*/
The // tells the complier to ignore the rest of that line (until the 'enter' key has been pushed). The /* tells the compiler to ignore everything until the next instance of */ is found. Now when I say Ignore.. I mean just that. When the compiler does it's work, it ignores any white space and any comments. It strips out just the source code and compiles it into a working executable.
Now on to the 'meat' of our program.
The below two statements of code are simple examples of how you would declare variables in C#.
int x = 123;
string helloWorld = "Hello Again";
The general form for declaring variables is ; | [= ;]
For int x = 123, we are declaring an Integer variable named x and initializing it with the value 123. We could have just as easily written it as two separate statements. One to declare x as an integer, and one to initialize it with the value 123 such as:
int x;
x = 123;
IMPORTANT NOTE: When assigning values to number variables (= is referred to as the assignment operator), the value must not have any quotes surrounding it.
string helloWorld = "Hello Again";
Now, when declaring a string variable (in which string is actually a class, but we will discuss that later), we DO need quotes surrounding the value that we are assigning to the variable name. We could have also written this in two statements:
string helloWorld;
helloWorld = "Hello Again";
Now we saw how Main was a method declared for our Program, in the following three lines, we use a Method created to output things to the screen. The first, takes a 'string literal' within double quotes and passes that information to the console to output to the screen.
Console.WriteLine("Hello C# Programming World!");
The following calls the same Console.WriteLine method and passes the variable 'x' to it for output to the screen.
Console.WriteLine(x);
And again, we call the Console.WriteLine method and pass a string variable to it for output to the screen.
Console.WriteLine(helloWorld);
Our last statement:
Console.Read();
Merely tells the program to wait for the user to press a key before it closes.
} <-- This curly brace indicates the end of the Main() method.
} <-- This curly brace indicates the end of the Class Program
} <--This curly brace indicates the end of our HelloWorld namespace.
IMPORTANT: You will notice that at the end of every statement in our program, we have a semicolon (;) to terminate the line. This tells the compiler that the statement has ended.
Now that is a very simple definition of our very first Console application in C#. If you type the program into notepad (or Visual Studio, then compile and run it.. you would see the following output:
Hello C# Programming World!
123
Hello Again
Press any key to continue....
The next section will discuss user defined names (variables) and the basic operators.