| for RuBoard |
.NET
The Common Language Runtime provides a solid base for developing applications of the future. The CLR is the foundation whose elements are the Common Type System, metadata, the Common Language Specification, and the Virtual Execution System (VES) that executes managed code. [14] As we shall see in future chapters, .NET makes it easier to develop Internet applications for both service providers and customer-based solutions. With the unified development platform .NET provides, it will be much easier than in the past for Microsoft or others to provide extensions.
[14] The Base Class Libraries classes (BCL) are also part of the CLR.
All this is made possible by
We shall expand on these topics in the course of the book. We
| for RuBoard |
| for RuBoard |
In this chapter we quickly cover the essentials of the C# language, which should be quite easy for you to learn if you have experience with C++ or Java. A "hello, world" program introduces the basic structure of C# programs. We then cover
C# has a
string
type, and the
StringBuilder
class can be used for dynamically changing strings. We examine arrays in C# and some operations provided by the
System.Array
class. We then cover some additional topics concerning methods, including parameter passing, variable length parameter lists, method overloading, and operator overloading. We discuss exception handling in C# in some detail, including the use of
We conclude the chapter by looking at how you can have "unsafe" sections of C# code, which can be used to work with pointers for efficiency or for interoperating with legacy code.
| for RuBoard |
| for RuBoard |
Whenever learning a new programming language, a good first step is to write and run a simple program that will display a single line of text. Such a program
Here is "Hello, World" in C#. (See the Hello directory for this chapter.)
// Hello.cs
using System;
class Hello
{
public static int Main(string[] args)
{
Console.WriteLine("Hello, World");
return 0;
}
}
You can learn how to use the Microsoft Visual Studio.NET IDE (integrated development environment) in Appendix A. You can also use the command-line tools of the .NET Framework SDK. Be sure to get the environment
>csc Hello.cs
An executable file Hello.exe will be generated. To execute your program, type at the command line:
>Hello
The program will now execute, and you should see displayed the greeting:
Hello, World
Setting Environment Variables
In order to run command-line tools such as the C# compiler using a simple
I
If you have Visual Studio.NET installed, you can ensure that the environment variables are set up by starting your command prompt session from Start Programs Microsoft Visual Studio.NET 7.0 Microsoft Visual Studio Tools Microsoft Visual Studio.NET Command Prompt. |
// Hello.cs class Hello { ... }
Every C# program has at least one
class
. A class is the foundation of C#'s support of object-oriented programming. A class encapsulates data (represented by
variables
) and behavior (represented by
Note the comment at the beginning of the program. A line beginning with a double slash is present only for documentation purposes and is ignored by the compiler. C# files have the extension . cs .
// Hello.cs
...
An alternate form of comment is to use an opening /* and a closing */.
/* This is a comment that may be continued over several lines */
There is a distinguished class, which has a method whose name must be Main . The method should be public and static . An int exit code can be returned to the operating system. Note that in C# the file name need not be the same as the name of the class containing the Main method.
// Hello.cs
using System;
class Hello
{
public static int Main(string[] args)
{
...
return 0;
}
}
Use void if you do not return an exit code.
public static void Main(string[] args)
Command-line arguments are passed as an array of strings. The runtime will call this Main method ”it is the entry point for the program. All the code of the Main method will be between the curly braces.
// Hello.cs
using System;
class Hello
{
public static int Main(string[] args)
{
Console.WriteLine("Hello, World");
return 0;
}
}
Every method in C# has one or more
statements
. A statement is
The Console class provides support for standard output and standard input. The method WriteLine displays a string, followed by a new line.
Much standard functionality in C# is provided through many classes in the .NET Framework. Related classes are grouped into namespaces . Many useful classes, such as Console , are in the System namespace. The fully qualified name of a class is specified by the namespace followed by a dot followed by a class name.
System.Console
A using statement allows a class to be referred to by its class name alone.
// Hello.cs using System; class Hello { public static int Main(string[] args) { Console .WriteLine("Hello, World"); return 0; } }
| for RuBoard |