Let's start with a first example of C# program, probably the simplest one which is capable of "doing something useful." It writes a text on the screen. We saw it in the previous section. Let's look at it now in more detail:
public class Example01
{
public static void Main()
{
System.Console.WriteLine("Hello");
}
}
It displays "Hello" on screen. But there are many words around that "Hello", so let's comment a bit about them before going on, even though many details will become clear later. In this first analysis, we will go inside out:
- WriteLine("Hello"); - "Hello" is the text we want to write, and WriteLine is the command which writes a text on screen (and moves the cursor to the next line).
- Console.WriteLine("Hello"); - because WriteLine is a command related to “Console” (the black text screen we can use when we use the operating system).
- System.Console.WriteLine("Hello"); - because the commands related to Console belong to “System” category.
- The curly braces { and } are used to mark the beginning and the end of a program block (now, the “main” block of the program).
- public static void Main() - Main is the "body" of our program (a program can be split intro several parts, as we'll see later). Every C# program must have a "Main" block. Why "void"? Why "static"? Why "public"? Why those empty parentheses ? We'll see those details later. For now, we'll just remember that this is the usual way to write "Main".
- public class Example01 – At this point, we'll just consider that "Example01" is the name for our program, and that a line like that must exist in all of our programs. Again, we'll see later the details on why is it a "class" and why is it "public".
As you can see, much of this program is still almost a "leap of faith" for us. We must believe that "it will work properly." Little by little we will be explaining why "public", "static", "void", "class"... For now we will just "fill in" the body of the program to understand the basic concepts of programming, and skip these advanced details.
Suggested exercise: Write a C# program which greets you using your name (eg: "Hi, Nacho").
Only a few more things before proceeding:
- Each C# statement must end with a semicolon (;)
- C# is a free format language, so we can write several commands on one line, and we can split statements into several lines or using spaces. For this reason, the above program could also be written like this (though not advisable, because it can be less readable):
public class Example01 {
public
static
void Main() { System.Console.WriteLine("Hello"); } }
In fact, there are two particularly common places for the starting brace, and I will use both interchangeably. One is as we have done in the first example, placing the opening brace alone in a line alone, exactly over the corresponding closing brace. This is what many authors call the "C Style". The second common way is by placing it after the name of the starting block (the "Java style"), as follows:
public class Ejemplo01 {public static void Main(){System.Console.WriteLine("Hola");}
}
(This is the way I will preferably use in this text when we are working with big source files, so that they take a little less space)
- The vast majority of orders that we can find in the C# language are English words or abbreviations of them. But keep in mind that C# is case sensitive, so "WriteLine" is a recognized word, but "writeLine", "WriteLine" or "WriteLine" are not.
No comments:
Post a Comment