- 1 A First C# Program
- 2 Namespaces
- 3 Alternative Forms of the Main() Function
- 4 Making a Statement
- 5 Opening a Text File for Reading and Writing
- 6 Formatting Output
- 7 The string Type
- 8 Local Objects
- 9 Value and Reference Types
- 10 The C# Array
- 11 The new Expression
- 12 Garbage Collection
- 13 Dynamic Arrays: The ArrayList Collection Class
- 14 The Unified Type System
- 15 Jagged Arrays
- 16 The Hashtable Container
- 17 Exception Handling
- 18 A Basic Language Handbook for C#
My daughter has cycled through a number of musical instruments. With each one she is anxious to begin playing the classicsno, not Schubert or Schoenberg, but the Backstreet Boys and Britney Spears. Her various teachers, keen to keep her interest while grounding her in the fundamentals, have tended to indulge her. In a sense this chapter attempts the same precarious balance in presenting C#. In this context the classics are represented by Web Forms and Type Inheritance. The fundamentals are the seemingly mundane predefined language elements and mechanisms, such as scoping rules, arithmetic types, and namespaces. My approach is to introduce the language elements as they become necessary to implement a small first program. For those more traditionally minded, the chapter ends with a summary listing of the predefined language elements.
C# supports both integral and floating-point numeric types, as well as a Boolean type, a Unicode character type, and a high-precision decimal type. These are referred to as the simple types. Associated with these types is a set of operators, including addition (+), subtraction (-), equality (==), and inequality (!=). C# provides a predefined set of statements as well, such as the conditional if and switch statements and the looping for, while, and foreach statements. All of these, as well as the namespace and exception-handling mechanisms, are covered in this chapter.
1.1 A First C# Program
The traditional first program in a new language is one that prints Hello, World! on the user's console. In C# this program is implemented as follows:
// our first C# program using System; class Hello { public static void Main() { Console.WriteLine( "Hello, World!" ); } }
When compiled and executed, this code generates the canonical
Hello, World!
Our program consists of four elements: (1) a comment, introduced by the double slash (//), (2) a using directive, (3) a class definition, and (4) a class member function (alternatively called a class method) named Main().
A C# program begins execution in the class member function Main(). This is called the program entry point. Main() must be defined as static. In our example, we declare it as both public and static.
public identifies the level of access granted to Main(). A member of a class declared as public can be accessed from anywhere within the program. A class member is generally either a member function, performing a particular operation associated with the behavior of the class, or a data member, containing a value associated with the state of the class. Typically, class member functions are declared as public and data members are declared as private. (We'll look at member access levels again as we begin designing classes.)
Generally, the member functions of a class support the behavior associated with the class. For example, WriteLine() is a public member function of the Console class. WriteLine() prints its output to the user's console, followed by a new-line character. The Console class provides a Write() function as well. Write() prints its output to the terminal, but without inserting a new-line character. Typically, we use Write() when we wish the user to respond to a query posted to the console, and WriteLine() when we are simply displaying information. We'll see a relevant example shortly.
As C# programmers, our primary activity is the design and implementation of classes. What are classes? Usually they represent the entities in our application domain. For example, if we are developing a library checkout system, we're likely to need classes such as Book, Borrower, and DueDate (an aspect of time).
Where do classes come from? Mostly from programmers like us, of course. Sometimes, it's our job to implement them. This book is designed primarily to make you an expert in doing just that. Sometimes the classes are already available. For example, the .NET System framework provides a DateTime class that is suitable for use in representing our DueDate abstraction. One of the challenges of becoming an expert C# programmerand not a trivial one at thatis becoming familiar with the more than 1,000 classes defined within the .NET framework. I can't cover all of them here in this text, but we'll look at quite a number of classes, including support for regular expressions, threads, sockets, XML and Web programming, database support, and the new way of building a Windows application.
A challenging problem is how to logically organize a thousand or more classes so that users (that's us) can locate and make sense of them (and keep the names from colliding with one another). Physically, we can organize them within directories. For example, all the classes supporting Active Server Pages (ASP) can be stored in an ASP.NET directory under a root System.NET directory. This makes the organization reasonably clear to someone poking around the file directory structure.
What about within programs? As it turns out, there is an analogous organizing mechanism within C# itself. Rather than defining a physical directory, we identify a namespace. The most inclusive namespace for the .NET framework is called System. The Console class, for example, is defined within the System namespace.
Groups of classes that support a common abstraction are given their own namespace defined within the System namespace. For example, an Xml namespace is defined within the System namespace. (We say that the Xml namespace is nested within the System namespace.) Several namespaces in turn are nested within the Xml namespace. There is a Serialization namespace, for example, as well as XPath, Xsl, and Schema namespaces. These separate namespaces within the enclosing Xml namespace are factored out to encapsulate and localize shared functionality within the general scope of XML. This arrangement makes it easier to identify the support, for example, that .NET provides for the World Wide Web Consortium (W3C) XPath recommendation. Other namespaces nested within the System namespace include IO, containing file and directory classes, Collections, Threading, Web, and so on.
In a directory structure, we indicate the relationship of contained and containing directories with the backslash (\), at least under Windowsfor example,
System\Xml\XPath
With namespaces, similar contained and containing relationships are indicated by the scope operator (.) in place of a backslashfor example,
System.Xml.XPath
In both cases we know that XPath is contained within Xml, which is contained within System.
Whenever we refer to a name in a C# program, the compiler must resolve that name to an actual declaration of something somewhere within our program. For example, when we write
Console.WriteLine( "Hello, World" );
the compiler must somehow discover that Console is a class name and that WriteLine() is a member function within the Console classthat is, within the scope of the Console class definition. Because we have defined only the Hello class in our file, without our help the compiler is unable to resolve what the name Console refers to. Whenever the compiler cannot resolve what a name refers to, it generates a compile-time error, which stops our program from building:
C:\C#Programs\hello\hello.cs(7): The type or namespace name 'Console' does not exist in the class or namespace
The using directive in our program,
using System;
directs the compiler to look in the System namespace for any names that it cannot immediately resolve within the file it is processingin this case, the file that contains the definition of our Hello class and its Main() member function.
Alternatively, we can explicitly tell the compiler where to look:
System.Console.WriteLine( "Hello, World" );
Some peopleactually some very smart and otherwise quite decent peoplebelieve that explicit listing of the fully qualified namethat is, the one that identifies the full set of namespaces in which a class is containedis always preferable to a using directive. They point out that the fully qualified name clearly identifies where the class is found, and they believe that is useful information (even if it is repeated 14 times within 20 adjacent lines). I don't share that belief (and I really don't like all that typing). In my textand this is one of the reasons we authors write booksthe fully qualified name of a class is never used,1 except to disambiguate the use of a type name (see Section 1.2 for an illustration of situations in which this is necessary).
Earlier I wrote that classes come mostly either from other programmers or from libraries provided by the development system. Where else do they come from? The C# language itself. C# predefines several heavily used data types, such as integers, single- and double-precision floating-point types, and strings. Each has an associated type specifier that identifies the type within C#: int represents the primitive integer type; float, the primitive single-precision type; double, the double-precision type; and string, the string type. (See Tables 1.2 and 1.3 in Section 1.18.2 for a list of the predefined numeric types.)
For example, an alternative implementation of our simple program defines a string object initialized with the "Hello, World!" string literal:
public static void Main() { string greeting = "Hello, World!" Console.WriteLine( greeting ); }
string is a C# keyword that is, a word reserved by the C# language and invested with special meaning. public, static, and void are also keywords in the language. greeting is referred to as an identifier. It provides a name for an object of type string. Identifiers in C# must begin with either an underscore (_) or an alphabet character. The names are case sensitive, so greeting, Greeting, and Greeting1 each represent a unique identifier.
A common flash point among programmers centers on whether compound names should be separated by an underscore, as in xml_text_reader, or by capitalization of the first letter of each internal word, as in xmlTextReader. By convention, identifiers that represent class names usually begin with a capital letter, as in XmlTextReader.
Within a unit of program visibility referred to as a declaration space, or scope, identifiers must be unique. At local scopethat is, within a function body, such as within our definition of Main()this is not a problem because we generally control the entire definition of any object within our function. As the extent of the scope widensthat is, as the number of programmers or organizations involved increasesthe problem of unique identifiers becomes more difficult. This is where namespaces come into the picture.