Hello, World


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]

class HelloWorld {  static void Main()  {    System.Console.WriteLine("Hello. My name is Inigo Montoya.");  } }

[1] Refer to the movie The Princess Bride if you're confused about the Inigo Montoya references.

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.

[2] When creating C#, the language creators sat down with the specifications for C/C++, literally crossing out the features that they didn't like and creating a list of the ones they did like. The group also included designers with strong backgrounds in other languages.

Compiling and Running the Application

The 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]

[3] Compilation using the Mono compiler, an open source compiler sponsored by Novell, is virtually identical, except that the compiler name is mcs.exe rather than csc.exe. Although I would very much like to have placed instructions for each platform here, doing so detracts from the topic of introducing C#. See Appendix A for details on Mono.

Output 1.1.

>csc.exe HelloWorld.cs Microsoft (R) Visual C# 2005 Compiler version 8.00.50727.42         for Microsoft (R) Windows (R) 2005 Framework version 2.0.50727      Copyright (C) Microsoft Corporation 2001-2005. All rights reserved.

Running the resulting program, HelloWorld.exe, displays the message shown in Output 1.2.

Output 1.2.

 >HelloWorld.exe Hello. My name is Inigo Montoya.

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.

Language Contrast: JavaFilename Must Match Class Name

In Java, the filename must follow the name of the class. In C#, this convention is frequently followed but is not required. In C#, it is possible to have two classes in one file, and in C# 2.0, it's possible to have a single class span multiple files.


Managed Execution and the Common Language Infrastructure

The 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

[4] Miller, J., and S. Ragsdale. 2004. The Common Language Infrastructure Annotated Standard. Boston: Addison-Wesley.

  • The VES or runtime

  • The CIL

  • A type system that supports language interoperability, known as the Common Type System (CTS)

  • Guidance on how to write libraries that are accessible from CLI-compatible languages (available in the Common Language Specification [CLS])

  • Metadata that enables many of the services identified by the CLI (including specifications for the layout or file format of assemblies)

  • A common programming framework, the Base Class Library (BCL), which developers in all languages can utilize

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

  • Language interoperability: interoperability between different source languages. This is possible because the language compilers translate each source language to the same intermediate language (CIL).

  • Type safety: checks for conversion between types, ensuring that only conversions between compatible types will occur. This helps prevent the occurrence of buffer overruns, a leading cause of security vulnerabilities.

  • Code access security: certification that the assembly developer's code has permission to execute on the computer.

  • Garbage collection: memory management that automatically de-allocates space for data allocated by the runtime.

  • Platform portability: support for potentially running the same assembly on a variety of operating systems. One obvious restriction is that no platform-dependent libraries are used; therefore, as with Java, there are inevitably some idiosyncrasies that need to be worked out.

  • BCL: provides a large foundation of code that developers can depend on (in all CLI implementations) so that they do not have to develop the code themselves.

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.





Essential C# 2.0
Essential C# 2.0
ISBN: 0321150775
EAN: 2147483647
Year: 2007
Pages: 185

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net