Page 33 - DCAP312_WEB_TECHNOLOGIES_II
P. 33
Unit 2: Introduction to C#
System.Console.WriteLine ( “Hello,”); Notes
Console.WriteLine ( “World!”);
Console.ReadLine ();
}
}
}
To compile hello.cs, run the following from the command line:
• For standard Microsoft installations of .NET 2.0, run C:\WINDOWS\Microsoft.NET\
Framework\v2.0.50727\csc.exe hello.cs
• For Mono run mcs hello.cs.
• For users of cscc, compile with “cscc-o <name>.exe <name>.cs”.
Doing so will produce hello.exe. The following command will run hello.exe:
• On Windows, use hello.exe.
• On Linux, use mono hello.exe or “ilrun <name>.exe”.
Alternatively, in Visual C# express, you could just hit F5 or the green play button to run the
code, even though that is for debugging.
Running hello.exe will produce the following output:
Hello,
World!
The program will then wait for you to strike ‘enter’ before returning to the command prompt.
Note that the example above includes the System namespace via the using keyword. That
inclusion allows direct references to any member of the System namespace without specifying
its fully qualified name.
The first call to the WriteLine method of the Console class uses a fully qualified reference.
System.Console.WriteLine (“Hello”);
The second call to that method shortens the reference to the Console class by taking advantage
of the fact that the System namespace is included (with using System).
Console.WriteLine (“World”);
C# is a fully object-oriented language. The following sections explain the syntax of the C#
language as a beginner’s course for programming in the language. Note that much of the power
of the language comes from the classes provided with the .Net framework, which are not part
of the C# language syntax.
2.1.1 Syntax
C# syntax looks quite related to the syntax of Java because both accede too much of their syntax
from C and C++. The object-oriented natural world of C# requires the high level structure of a
C# program to be defined in terms of classes, whose detailed behaviours are defined by their
statements.
Statements
The basic unit of carrying out in a C# program is the statement. A statement can announce a
variable, define an expression, perform a simple action by vocation a process, control the flow
LOVELY PROFESSIONAL UNIVERSITY 27