C#慢慢学 (一)(e文转)
点击次数:16 次 发布日期:2008-11-06 08:08:12 作者:源代码网
|
源代码网推荐 C# is provided as a part of Microsoft Visual Studio 7.0. In addition to C#, Visual Studio supports Visual Basic, Visual C , and the scripting languages VBScript and JScript. All of these languages provide access to the Next Generation Windows Services (NWGS) platform, which includes a common execution engine and a rich class library. The .NET software development kit defines a "Common Language Subset" (CLS), a sort of lingua franca that ensures seamless interoperability between CLS-compliant languages and class libraries. For C# developers, this means that even though C# is a new language, it has complete access to the same rich class libraries that are used by seasoned tools such as Visual Basic and Visual C . C# itself does not include a class library. 源代码网推荐 The rest of this chapter describes the essential features of the language. While later chapters describe rules and exceptions in a detail-oriented and sometimes mathematical manner, this chapter strives for clarity and brevity at the expense of completeness. The intent is to provide the reader with an introduction to the language that will facilitate the writing of early programs and the reading of later chapters. 源代码网推荐 1.1 Hello, world 源代码网推荐 The canonical “Hello, world” program can be written in C# as follows: 源代码网推荐 using System; 源代码网推荐 class Hello 源代码网推荐 { 源代码网推荐 static void Main() { 源代码网推荐 Console.WriteLine("Hello, world"); 源代码网推荐 } 源代码网推荐 } 源代码网推荐 The default file extension for C# programs is .cs, as in hello.cs. Such a program can be compiled with the command line directive 源代码网推荐 csc hello.cs 源代码网推荐 which produces an executable program named hello.exe. The output of the program is: 源代码网推荐 Hello, world 源代码网推荐 Close examination of this program is illuminating: 源代码网推荐 The using System; directive references a namespace called System that is provided by the .NET runtime. This namespace contains the Console class referred to in the Main method. Namespaces provide a hierarchical means of organizing the elements of a class library. A “using” directive enables unqualified use of the members of a namespace. The “Hello, world” program uses Console.WriteLine as a shorthand for System.Console.WriteLine. What do these identifiers denote? System is a namespace, Console is a class defined in that namespace, and WriteLine is a static method defined on that class. 源代码网推荐 The Main function is a static member of the class Hello. Functions and variables are not supported at the global level; such elements are always contained within type declarations (e.g., class and struct declarations). 源代码网推荐 The “Hello, world” output is produced through the use of a class library. C# does not itself provide a class library. Instead, C# uses a common class library that is also used by other languages such as Visual Basic and Visual C . 源代码网推荐 源代码网供稿. |
