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# programmer—and not a trivial one at that—is 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 Windows—for example,

SystemXmlXPath

With namespaces, similar contained and containing relationships are indicated by the scope operator (.) in place of a backslash—for 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 class—that 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#Programshellohello.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 processing—in 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 people—actually some very smart and otherwise quite decent people—believe that explicit listing of the fully qualified name—that is, the one that identifies the full set of namespaces in which a class is contained—is 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 text—and this is one of the reasons we authors write books—the 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).

[1] The Visual Studio wizards, such as Windows Forms and Web Forms, generate fully qualified names. However, because the names are machine generated, this does not really qualify as a counterexample.

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 scope—that 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 widens—that is, as the number of programmers or organizations involved increases—the problem of unique identifiers becomes more difficult. This is where namespaces come into the picture.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.120.136