|
Introduction
What Is C#
Based on Microsoft, "C# is a modern, object-oriented language that enables programmers
to quickly build a wide range of applications for the new Microsoft .NET platform,
which provides tools and services that fully exploit both computing and communications."
My own definition will come after I know more about this language.
C# Programming Environment
The best environment to do C# programming at this time is Microsoft's .NET Framework SDK.
It is freely available from Microsoft Web site. You may follow the instructions bellow
download.NET Framework SDK:
1. Make sure you have Microsoft Windows 2000 with SP 2 applied.
2. Go to Internet at http://msdn.microsoft.com/netframework, and click "Download the .NET Framework".
3. Then click ".NET Framework SDK" to go to the download area.
4. Select the "Full SDK Download (131 MB)" next to the download icon.
5. Once the download is completed, double click the downloaded file in the File Explorer.
And following the instructions on the screen to finish installing .Net Framework SDK on your system.
6. Restart your system, and open command window to enter "csc". If this command
returns with message:
>csc
Microsoft (R) Visual C# .NET Compiler version 7.00.9466
for Microsoft (R) .NET Framework version 1.0.3705
Copyright (C) Microsoft Corporation 2001. All rights reserved.
fatal error CS2008: No inputs specified
you know that you have .Net Framework SDK installed correctly.
7. If you don't see the above message, check to you have the following included in the command path:
C:\WINNT\Microsoft.NET\Framework\v1.0.3705
First Program in C#
Let's follow the tradition of the programming language world, and write our
first C# program to print out "Hello world!" on the screen.
You can use notepad to write the following code and save it as c:\home\herong\csharp\hello.cs:
class Hello {
public static void Main() {
System.Console.WriteLine("Hello world!");
}
}
To compile and run the program, open command window and issue the following commands:
>cd \home\herong\csharp
>csc hello.cs
Microsoft (R) Visual C# .NET Compiler version 7.00.9466
for Microsoft (R) .NET Framework version 1.0.3705
Copyright (C) Microsoft Corporation 2001. All rights reserved.
>hello
Hello world!
Congratulation, you have successfully created, compiled, and executed your first C# program!
C# Program Structure
We have our first C# program written, compiled, and executed. Now let's look at the C# as
a programming language. First, let's exam our first program to understand some basic rules
about the structure of a C# program.
Rule: A C# program must have at least one class. Class is the basic unit of a C# program.
We will learn more about class later in this book. In our first program, the following structure
defines a class called "Hello":
class Hello {
...
}
Rule: A C# program must have an entry point, which must be defined as a "public",
"static", and "void" method named as "Main" in a class. In our first program, this entry point
is defined in the "Hello" class as the following:
class Hello {
public static void Main() {
...
}
}
Rule: When a C# program is launched to run, the execution will start with the first executable
statement in the entry point method, and continue to the end of this method. In our first program,
there is only one executable statement in the entry point method:
System.Console.WriteLine("Hello world!");
This statement will be executed, when this Hello program is launched to run. Once this statement
is done, the program will end.
Please note that the only statement in the main() method
calls a pre-defined method named as "WriteLine", which is defined in the Console
class in the System namespace provided as part of .NET Framework SDK. This method takes a string
of characters as a input parameter, and print this string to the screen. Note that the string of
characters must be enclosed in double quote characters.
Exercise: Modify the hello.cs to print the following message to the screen:
Hi There,
My name is X Y. I am enjoying this book.
|