1. Introduction C# (pronounced “See Sharp”) is a simple, modern, object-oriented, and type-safe programming language. C# has its roots in the C family of languages and will be immediately familiar to C, C++, and Java programmers. C# is standardized by ECMA International as the ECMA-334 standard and by ISO/IEC as the ISO/IEC 23270 standard. Microsoft’s C# compiler for the. NET Framework is a conforming implementation of both of these standards.
C# is an object-oriented language, but C# further includes support for component-oriented programming. Contemporary software design increasingly relies on software components in the form of self-contained and self-describing packages of functionality. Key to such components is that they present a programming model with properties, methods, and events; they have attributes that provide declarative information about the component; and they incorporate their own documentation. C# provides language constructs to directly support these concepts, making C# a very natural language in which to create and use software components. Several C# features aid in the construction of robust and durable applications: Garbage collection automatically reclaims memory occupied by unused objects; exception handling provides a structured and extensible approach to error detection and recovery; and the type-safe design of the language makes it impossible to have uninitialized variables, to index arrays beyond their bounds, or to perform unchecked type casts.
C# has a unified type system. All C# types, including primitive types such as int and double, inherit from a single root object type. Thus, all types share a set of common operations, and values of any type can be stored, transported, and operated upon in a consistent manner. Furthermore, C# supports both user-defined reference types and value types, allowing dynamic allocation of objects as well as in-line storage of lightweight structures. To ensure that C# programs and libraries can evolve over time in a compatible manner, much emphasis has been placed on versioning in C#’s design. Many programming languages pay little attention to this issue, and, as a result, programs written in those languages break more often than necessary when newer versions of dependent libraries are introduced.
Aspects of C#’s design that were directly influenced by versioning considerations include the separate virtual and override modifies, the rules for method overload resolution, and support for explicit interface member declarations. The rest of this chapter describes the essential features of the C# language. Although 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 “Hello, World” program is traditionally used to introduce a programming language.
Here it is in C#: using System; class Hello{ static void Main () { Console. Write Line (‘Hello, World’); }}C# source files typically have the file extension. cs. Assuming that the “Hello, World” program is stored in the file hello.
cs, the program can be compiled with the Microsoft C# compiler using the command line csc hello. cs which produces an executable assembly named hello. eye. The output produced by this application when it is run is Hello, World The “Hello, World” program starts with a using directive that references the System name space. Namespaces provide a hierarchical means of organizing C# programs and libraries. Namespaces contain types and other name spaces-for example, the System name space contains a number of types, such as the Console class referenced in the program, and a number of other name spaces, such as IO and Collections.
A using directive that references a given name space enables unqualified use of the types that are members of that name space. Because of the using directive, the program can use Console. Write Line as shorthand for System. Console. Write Line. The Hello class declared by the “Hello, World” program has a single member, the method named Main.
The Main method is declared with the static modifier. Unlike instance methods, which reference a particular object instance using the keyword this, static methods operate without reference to a particular object. By convention, a static method named Main serves as the entry point of a program. The output of the program is produced by the Write Line method of the Console class in the System name space. This class is provided by the. NET Framework class libraries, which, by default, are automatically referenced by the Microsoft C# compiler.
Note that C# itself does not have a separate run time library. Instead, the. NET Framework is the run time library of C#. 1. 2 Program structure The key organizational concepts in C# are programs, name spaces, types, members, and assemblies. C# programs consist of one or more source files.
Programs declare types, which contain members and can be organized into name spaces. Classes and interfaces are examples of types. Fields, methods, properties, and events are examples of members. When C# programs are compiled, they are physically packaged into assemblies.
Assemblies typically have the file extension. eye or. dull, depending on whether they implement applications or libraries.