C# Primer: A Practical Approach

Paperback (Print)
Used and New from Other Sellers
Used and New from Other Sellers
from $1.99
Usually ships in 1-2 business days
(Save 95%)
Other sellers (Paperback)
  • All (20) from $1.99   
  • New (6) from $5.28   
  • Used (14) from $1.99   

Overview

Now Stan Lippman brings you C# using his famed primer format. C# PRIMER is a comprehensive, example-driven introduction to this new object-oriented programming language.

C# is a cornerstone of Microsoft's new .NET platform. Inheriting many features from both Java and C++, C# is destined to become the high-level programming language of choice for building high-performance Windows and Web applications and components—from XML-based Web services to middle-tier business objects and system-level applications.

First, you will tour the language, looking at built-in features such as the class mechanism, class inheritance, and interface inheritance—all while you build small programs. Next, you will explore the various library domains supported within the .NET class framework. You will also learn how to use the language and class framework to solve problems and build quality programs.

Highlights include:
  • Covers fundamentals such as namespaces, exception handling, and unified type system
  • Presents a wide-ranging tour of the .NET class library, introducing ADO.NET and establishing database connections and the use of XML
  • Provides XML programming using the firehose and DOM parser models, XSLT, XPATH, and schemas
  • Focuses on ASP.NET Web Form Designer, walking through the page life-cycle and caching, and providing a large number of examples.
  • Introduces .NET Common Language Runtime (CLR)

Adding C# to your toolbox will not only improve your Web-based programming ability, but also increase your productivity. C# PRIMER provides a solid foundation to build upon and a refreshingly unbiased voicMicrosoft's vehicle to effective and efficient Web-based programming.



Read More Show Less

Product Details

  • ISBN-13: 9780201729559
  • Publisher: Addison-Wesley
  • Publication date: 3/28/2001
  • Series: Developmentor Series
  • Pages: 416
  • Product dimensions: 7.40 (w) x 9.20 (h) x 1.00 (d)

Meet the Author

Stanley B. Lippman is Architect with the Visual C++ development team at Microsoft. Previously, he served as a Distinguished Consultant at the Jet Propulsion Laboratories (JPL). Stan spent more than twelve years at Bell Laboratories, where he worked with Bjarne Stroustrup on the original C++ implementation and the Foundation research project. After Bell Laboratories, Stan worked at Disney Feature Animation, originally as principal software engineer, then as software technical director on Fantasia 2000.
Read More Show Less

Read an Excerpt

Chapter 1: Hello, C#

My daughter has cycled through a number of musical instruments. With each one she is anxious to begin playing the classics—no, 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 newline 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,

System\Xml\XPath

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#Programs\hello\hello.cs(7):

<blockquote>The type or namespace name 'Console' does
not exist in the class or namespace</blockquote>

The using directive in our program,
<blockquote>using System;</blockquote>

directs the compiler to look in the System namespace for any names that it cannot immediately resolve within the file it is process ng—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 peo-ple— 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 illustra-tion 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...


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.
Read More Show Less

Table of Contents

Preface
1: Hello, C<35>
2: Class Design
3: Object-Oriented Programming
4: Interface Inheritance
5: Exploring the System Namespace
6: Windows Forms Designer
7: ASP.NET and Web Forms Designer
8: The Common Language Runtime
Index
Read More Show Less

Preface

C# is a new language invented at Microsoft and introduced with Visual Studio.NET. More than a million lines of C# code already have gone into the implementation of the .NET class framework. This book covers both the C# language and its use in programming the .NET class framework, illustrating application domains such as ASP.NET and XML.

My general strategy in presenting the material is to introduce a programming task and then walk through one or two implementations, introducing language features or aspects of the class framework as they prove useful. The goal is to motivate how to use the language and class framework to solve problems rather than to simply list language features and the class framework API.Learning C# really is a two-step process, and that process is reflected in the organization of this text. In the first step, we walk through the language--both its mechanisms, such as class and interface inheritance and delegates, and its underlying concepts, such as its unified type system, reference vs. value types, boxing, and so on. This is covered in first four chapters. The second step is to become familiar with the .NET class framework, in particular with windows and web programming and the support for XML. This is the focus of the second half of the book. Working your way through the text should jump start your C# programming skills. In addition, you'll become familiar with a good swatch of the .NET class framework. All the program code is available for download at www.objectwrite.com , my company's Web site. Mail can be sent to me directly at slippman@objectwrite.com .

Organization of the Book

The book is organized into eight relatively long chapters. The first four chapters focus on the C# language, looking at the built-in language features, the class mechanism, class inheritance, and interface inheritance. The second four chapters explore the various library domains supported within the .NET class framework, such as regular expressions, threading, sockets, Windows Forms, ASP.NET, and the Common Language Runtime.

The first chapter covers the basic language, as well as some of the fundamental classes provided within the class framework. The discussion is driven by the design of a small program. Concepts such as namespaces, exception handling, and the unified type system are introduced.

The second chapter covers the fundamentals of building classes. We look at access permission, distinguish between const and readonly members, and cover access methods such as indexers and properties. We walk through the different strategies of member initialization, as well as the rules for operator overloading and conversion operators. We look at the delegate type, which serves as a kind of universal pointer to function.

The third and fourth chapters cover, in turn, class and interface inheritance. Class inheritance allows us to define a family of specialized types that override a generic interface, such as an abstract WebRequest base class and a protocol-specific HttpWebRequest subtype.

Interface inheritance, on the other hand, allows us to provide a common service or shared attribute for otherwise unrelated types. For example, the IDisposable interface provides a service to free resources. Classes holding data base connections or window handles are both likely to implement IDisposable although they are otherwise unrelated.

Chapter 5 provides a wide-ranging tour of the .NET class library. We look at input and output, including file and directory manipulation, regular expressions, sockets and thread programming, the WebRequest and WebResponse class hierarchies, a brief introduction to ADO.NET and establishing data base connections, and the use of XML. The sixth and seventh chapters cover, in turn, drag and drop Windows Forms and Web Forms development. Chapter 7 focuses on ASP.NET, and the web page lifecycle. Both chapters provide lots of examples of using the pre-built controls and attaching event handlers for user interaction.

The final chapter provides a programmer's introduction to the .NET Common Language Runtime. It focuses on the assembly, type reflection, and attributes, and concludes with a brief look at the underlying intermediate language that is the compilation target of all .NET languages.

Written for Programmers

The book does not assume that you know C++, Visual Basic, or Java. But it does presume that you have programmed in some language. So, for example, I don't presume you know the exact syntax of the C# foreach loop statement, but I presume you know what a loop is. While I will illustrate how to invoke a function in C#, I presume you know what I mean when I say we "invoke a function." It does not require previous knowledge of object-oriented programming or of the earlier versions of ASP and ADO.

Some people--some very bright people--argue that, under .NET, the programming language is secondary to the underlying Common Language Runtime (CLR) upon which the languages float like the continents on tectonic plates. I don't agree. Language is how we express ourselves, and the choice of one's language impacts the design of our programs. The underlying assumption of this book is that C# is the preferred language for .NET programming.

Lexical Conventions

Type names, objects, and keywords are set off in Courier font, as in int, a predefined language type, Console, a class defined in the framework, maxCount, an object defined either as a data member or local object within a function, and foreach, one of the predefined loop statements. To indicate a function, a name is followed by an empty pair of parentheses, as in WriteLine(). The first introduction of a concept, such as garbage collection or data encapsulation, is highlighted in italics. Hopefully these conventions help in the readability of the text.

Resources

The richest documentation that you will be returning to time and again is that provided with The Visual Studio.NET Documentation. The .NET framework reference is essential to doing any sort of C#/.NET programming.

Another rich source of information about .NET are the featured articles and columns in the MSDN magazine. I'm always impressed by what I find in it each issue. You can find it online at The DOTNET mailing list sponsored by DevelopMentor is a rich source of information. You can subscribe to it at .

Anything Jeffrey Richter, Don Box, Aaron Skonnard, or Jeff Prosise writes about .NET (or XML in Aaron's case) should be considered essential reading. Currently, most of their writing has appeared only as articles in MSDN magazine.

Here is the collection of books that I have referenced or found helpful.

    Essential XML, Beyond Markup, by Don Box, Aaron Skonnard, and John Lam, Addison-Wesley, 2000.
    A Programmer's Introduction to C#, by Eric Gunnerson, Apress, 2000. Active Server Pages+, by Richard Anderson, Alex Homer, Rob Howard, and Dave Sussman, Wrox Press, 2000.
    Microsoft C# Language Specification, Microsoft Press, 2001.C# Essentials, by Ben Albahari, Peter Drayton, and Brad Merrill, O'Reilly & Associates, Inc., 2001.
    C# Programming, by Burton Harvey, Simon Robinson, Julian Templeman, and Karli Watson, Wrox Press, 2000.
Read More Show Less

Customer Reviews

Be the first to write a review
( 0 )
Rating Distribution

5 Star

(0)

4 Star

(0)

3 Star

(0)

2 Star

(0)

1 Star

(0)

Your Rating:

Your Name: Create a Pen Name or

Barnes & Noble.com Review Rules

Our reader reviews allow you to share your comments on titles you liked, or didn't, with others. By submitting an online review, you are representing to Barnes & Noble.com that all information contained in your review is original and accurate in all respects, and that the submission of such content by you and the posting of such content by Barnes & Noble.com does not and will not violate the rights of any third party. Please follow the rules below to help ensure that your review can be posted.

Reviews by Our Customers Under the Age of 13

We highly value and respect everyone's opinion concerning the titles we offer. However, we cannot allow persons under the age of 13 to have accounts at BN.com or to post customer reviews. Please see our Terms of Use for more details.

What to exclude from your review:

Please do not write about reviews, commentary, or information posted on the product page. If you see any errors in the information on the product page, please send us an email.

Reviews should not contain any of the following:

  • - HTML tags, profanity, obscenities, vulgarities, or comments that defame anyone
  • - Time-sensitive information such as tour dates, signings, lectures, etc.
  • - Single-word reviews. Other people will read your review to discover why you liked or didn't like the title. Be descriptive.
  • - Comments focusing on the author or that may ruin the ending for others
  • - Phone numbers, addresses, URLs
  • - Pricing and availability information or alternative ordering information
  • - Advertisements or commercial solicitation

Reminder:

  • - By submitting a review, you grant to Barnes & Noble.com and its sublicensees the royalty-free, perpetual, irrevocable right and license to use the review in accordance with the Barnes & Noble.com Terms of Use.
  • - Barnes & Noble.com reserves the right not to post any review -- particularly those that do not follow the terms and conditions of these Rules. Barnes & Noble.com also reserves the right to remove any review at any time without notice.
  • - See Terms of Use for other conditions and disclaimers.
Search for Products You'd Like to Recommend

Recommend other products that relate to your review. Just search for them below and share!

Create a Pen Name

Your Pen Name is your unique identity on BN.com. It will appear on the reviews you write and other website activities. Your Pen Name cannot be edited, changed or deleted once submitted.

 
Your Pen Name can be any combination of alphanumeric characters (plus - and _), and must be at least two characters long.

Continue Anonymously

    If you find inappropriate content, please report it to Barnes & Noble
    Why is this product inappropriate?
    Comments (optional)