The best way to learn a new programming language is to write code. The first example is the classic HelloWorld program. In this program, you will display some text to the screen. Listing 1.1 shows the complete HelloWorld program; in the following sections, you will compile the code. Listing 1.1. HelloWorld in C# [1]
Note C# is a case-sensitive language: Incorrect case prevents the code from compiling successfully. Those experienced in programming with Java, C, or C++ will immediately see similarities. Like Java, C# inherits its basic syntax from C and C++. [2] Syntactic punctuation (such as semicolons and curly braces), features (such as case sensitivity), and keywords (such as class, public, and void) are familiar to programmers experienced in these languages. Beginners and programmers from other languages will quickly find these constructs intuitive.
Compiling and Running the ApplicationThe C# compiler allows any file extension for files containing C# source code, but .cs is typically used. After saving the source code to a file, developers must compile it. (Appendix A provides instructions for installing the compiler.) Because the mechanics of the command are not part of the C# standard, the compilation command varies depending on the C# compiler implementation. If you place Listing 1.1 into a file called HelloWorld.cs, the compilation command in Output 1.1 will work with the Microsoft .NET compiler (assuming appropriate paths to the compiler are set up).[3]
Output 1.1.
Running the resulting program, HelloWorld.exe, displays the message shown in Output 1.2. Output 1.2.
The program created by the C# compiler, HelloWorld.exe, is an assembly. Instead of creating an entire program that can be executed independently, developers can create a library of code that can be referenced by another, larger program. Libraries (or class libraries) use the filename extension .dll, which stands for Dynamic Link Library (DLL). A library is also an assembly. In other words, the output from a successful C# compile is an assembly regardless of whether it is a program or a library.
Managed Execution and the Common Language InfrastructureThe processor cannot directly interpret an assembly. Assemblies consist of a second language known as the Common Intermediate Language (CIL), or IL for short. Note A third term for CIL is Microsoft IL (MSIL). This book uses the term CIL because it is the term adopted by the CLI standard. IL is prevalent in conversation among people writing C# code because they assume that IL refers to CIL rather than other types of intermediate languages. The C# compiler transforms the C# source file into this intermediate language. An additional step, usually performed at execution time, is required to change the CIL code into machine code that the processor can understand. This involves an important element in the execution of a C# program: the Virtual Execution System (VES). The VES, also casually referred to as the runtime, compiles CIL code as needed (this process is known as just-in-time compilation [jitting]). The code that executes under the context of an agent like the runtime is managed code, and the process of executing under control of the runtime is managed execution. It is called managed code because the runtime controls significant portions of the program's behavior by managing aspects such as memory allocation, security, and just-in-time compilation. Code that does not require the runtime in order to execute is unmanaged code. Note The term runtime can refer to either execution time or the Virtual Execution System. To help clarify, this book uses the term execution time to indicate when the program is executing, and it uses the term runtime when discussing the agent responsible for managing the execution of a C# program while it executes. The specification for a VES is included in a broader specification known as the Common Language Infrastructure (CLI) specification.[4] An international standard, the CLI includes specifications for
Running within the context of a CLI implementation enables support for a number of services and features that programmers do not need to code for directly, including
Note This section gives a brief synopsis of the CLI to familiarize you with the context in which a C# program executes. It also provides a summary of some of the terms that appear throughout this book. Chapter 18 is devoted to the topic of the CLI and its relevance to C# developers. Although the chapter appears last in the book, it does not depend on any earlier chapters, so if you want to become more familiar with the CLI, you can jump to it at any time. |