Kamis, 18 Desember 2008

Display "Hallo World" with C# Language

This is the very simple program of C# language. It will display "Hallo World" and waiting input from user.

// Namespace Declaration

using System;

// Program start class
class Hallo_World
{
// Main begins program execution.
static void Main()
{
// Write to console
Console.WriteLine("Hallo World");

// Make window wait for input
Console.ReadLine();
}
}


After we run this program it will give an output:

Hallo World

And waiting input from user to terminate the program.



using system;
Indicates that we are referencing the System namespace. Namespaces contain groups of code that can be called upon upon by C# programs.
With the using System; declaration, you are telling your program that it can reference the code in the System namespace without pre-ending the word System to every reference.

class WelcomeCSS
The class declaration, contains the data and method definitions that your program uses to execute. Can use to decribe objects, such as structs, interfaces, delegates, and enum.
This particular class has no data, but it does have one method.

Main
The methed of class WelcomeCSS tells what this class will do when executed. It is the starting point of a program. Often called "entry point" If we recive the error message saying that it can't find the entry point, it means that we tried to compile an executable program without a Main method.

static
A static modifier preceeds the word Main, meaning that this method works in this spedific class only, rather than an insatne of class.
This is necessary, because when a program begins, no object instances exit.

void
Every mthod must have a return type. In this case it is void, means that Main doesnt return a value.

WriteLine(...)
Console is a class in the System namespace. WriteLine(...) is a methode in the Console class.
We use the ".", dot, operator to separate subordinate program elements.
Note that we could also write this statement

//
This is a single line comment mark, mean that they are valid until the end-of-line. For multiple lines use /*(statement)*/
Comment are ignore when we compile our program.

All statements end with a ";", semi-colon. Classes and methods begin with "{", left curly brace, and end with a "}", right curly brace. Any statements within and including "{" and "}" define a block. Blocks define scope (or lifetime and visibility) of program elements.

Console.ReadLine();
Waiting input from user.

Tidak ada komentar: