9780201729559

C# Primer A Practical Approach

by
  • ISBN13:

    9780201729559

  • ISBN10:

    0201729555

  • Edition: 1st
  • Format: Paperback
  • Copyright: 2001-12-10
  • Publisher: Addison-Wesley Professional
  • Purchase Benefits
  • Free Shipping Icon Free Shipping On Orders Over $35!
    Your order must be $35 or more to qualify for free economy shipping. Bulk sales, PO's, Marketplace items, eBooks and apparel do not qualify for this offer.
  • eCampus.com Logo Get Rewarded for Ordering Your Textbooks! Enroll Now
List Price: $44.99

Summary

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 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 demonstrate how to use the language and class framework to solve problems rather than simply to list language features and the class framework API.

Learning C# is a two-step process: learning the details of the C# language and then becoming familiar with the .NET class framework. This two-step 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 versus value types, boxing, and so on. This step is covered in the 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 my company's Web site www.objectwrite.com.

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.

Chapter 1 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.

Chapter 2 covers the fundamentals of building classes. We look at access permission, distinguish between const and readonly members, and cover specialized 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 a function.

Chapters 3 and 4 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 frees resources. Classes holding database 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 database connections, and the use of XML.

Chapters 6 and 7 cover, in turn, drag-and-drop Windows Forms and Web Forms development. Chapter 7 focuses on ASP.NET, and the Web page life cycle. Both chapters provide lots of examples of using the prebuilt 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 assemblies, 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 assume that you have programmed in some language. So, for example, I don't assume that you know the exact syntax of the C# foreach loop statement, but I do assume that you know what a loop is. Although I will illustrate how to invoke a function in C#, I assume you know what I mean when I say we "invoke a function." This text 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 affects the design of our programs. The underlying assumption of this book is that C# is the preferred language for .NET programming.

The book is organized into eight relatively long chapters. The first set of four chapters focuses on the C# language, looking at the built-in language features, the class mechanism, class inheritance, and interface inheritance. The second set of four chapters explores 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.

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 as a local object within a function; and foreach, one of the predefined loop statements. Function names are 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. These conventions are intended to make the text more readable.

Resources

The richest documentation that you will be returning to time and again is 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 consists of the featured articles and columns in the MSDN Magazine. I'm always impressed by what I find in each issue. You can find it online at http://msdn.microsoft.com/msdnmag.

The DOTNET mailing list sponsored by DevelopMentor is a rich source of information. You can subscribe to it at http://discuss.develop.com.

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:

  • Active Server Pages+, by Richard Anderson, Alex Homer, Rob Howard, and Dave Sussman, Wrox Press, Birmingham, England, 2000.
  • C# Essentials, by Ben Albahari, Peter Drayton, and Brad Merrill, O'Reilly, Cambridge, MA, 2001.
  • C# Programming, by Burton Harvey, Si

Author Biography

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.

0

Table of Contents

Preface xiii
Hello, C#
1(58)
A First C# Program
1(5)
Namespaces
6(4)
Alternative Forms of the Main () Function
10(1)
Making a Statement
11(6)
Opening a Text File for Reading and Writing
17(2)
Formatting Output
19(2)
The string Type
21(3)
Local Objects
24(4)
Value and Reference Types
28(1)
The C# Array
29(1)
The new Expression
30(2)
Garbage Collection
32(1)
Dynamic Arrays: The ArrayList Collection Class
33(2)
The Unified Type System
35(4)
Shadow Boxing
36(1)
Unboxing Leaves Us Downcast
37(2)
Jagged Arrays
39(2)
The Hashtable Container
41(3)
Exception Handling
44(3)
A Basic Language Handbook for C#
47(12)
Keywords
47(2)
Built-in Numeric Types
49(2)
Arithmetic, Relational, and Conditional Operators
51(3)
Operator Precedence
54(1)
Statements
55(4)
Class Design
59(58)
Our First Independent Class
59(4)
Opening a New Visual Studio Project
63(3)
Declaring Data Members
66(1)
Properties
67(2)
Indexers
69(3)
Member Initialization
72(1)
The Class Constructor
73(3)
The Implicit this Reference
76(3)
static Class Members
79(2)
const and readonly Data Members
81(2)
The enum Value Type
83(3)
The delegate Type
86(6)
Function Parameter Semantics
92(7)
Pass by Value
94(2)
Pass by Reference: The ref Parameter
96(1)
Pass by Reference: The out Parameter
97(2)
Function Overloading
99(4)
Resolving Overload Functions
100(1)
Determining a Best Match
101(2)
Variable-Length Parameter Lists
103(4)
Operator Overloading
107(3)
Conversion Operators
110(3)
The Class Destructor
113(1)
The struct Value Type
113(4)
Object-Oriented Programming
117(42)
Object-Oriented Programming Concepts
117(4)
Supporting a Polymorphic Query Language
121(3)
Designing a Class Hierarchy
124(4)
Object Lessons
128(4)
Designing an Abstract Base Class
132(1)
Declaring an Abstract Base Class
133(4)
Static Members of an Abstract Base Class
137(1)
A Hybrid Abstract Base Class
138(5)
The Single-Inheritance Object Model
140(1)
How Is a Hybrid Abstract Class Different?
141(2)
Defining a Derived Class
143(2)
Overriding the Inherited Virtual Interface
145(1)
Overriding the Virtual object Methods
146(1)
Member Access: The new and base Modifiers
147(6)
Accessibility versus Visibility
150(1)
Encapsulating Base-Class Access
151(2)
Sealing a Class
153(1)
The Exception Class Hierarchy
154(5)
Interface Inheritance
159(40)
Implementing a System Interface: I Comparable
160(3)
Accessing an Existing Interface
163(3)
Defining an Interface
166(12)
Implementing Our Interface: Proof of Concept
168(6)
Integrating Our Interface within the System Framework
174(4)
Explicit Interface Member Implementations
178(2)
Inherited Interface Members
180(3)
Overloaded, Hidden, or Ambiguous?
183(2)
Mastering Copy Semantics: I Cloneable
185(2)
Mastering Finalize Semantics: I Disposable
187(3)
BitVector: Extension through Composition
190(9)
Exploring the System Namespace
199(84)
Supporting the Fundamental Types
199(1)
The Array Is a system.Array
200(3)
Querying the Environment
203(6)
The Environment Class
204(1)
Accessing All the Environment Variables
205(2)
The Process Class
207(1)
Finding the Logical Drives
208(1)
System. IO
209(12)
Handling File Extensions: The Path Class
210(2)
Manipulating Directories
212(3)
Manipulating Files
215(1)
Reading and Writing Files
216(5)
A System Miscellany
221(7)
The System. Collections. Stack Container
221(2)
The System. Diagnostics. TraceListener Class
223(2)
System. Math
225(1)
The DateTime Class
226(2)
Regular Expressions
228(7)
System. Threading
235(6)
The Web Request/Response Model
241(4)
System. Net. Sockets
245(4)
The Server-Side TcpListener
246(2)
The Client-Side TcpClient
248(1)
System. Data
249(10)
The Database Tables
250(2)
Opening the Database: Selecting a Data Provider
252(2)
Navigating the DataTable
254(3)
Setting Up the DataRelation
257(1)
Selection and Expressions
258(1)
System. XML
259(24)
Getting XML Out of Our Programs
260(5)
Xml TextReader: The Firehose
265(7)
Document Object Model
272(5)
System. Xml. Xsl
277(2)
System. Xml. XPath
279(4)
Windows Froms Designer
283(32)
Our First Windows Forms Program
283(2)
Building the GUI
285(3)
Implementing the Event Callback Routines
288(7)
Implementing a TextBox Event
292(1)
Implementing the Button Events: OK
293(2)
Implementing the Button Events: Quit
295(1)
Inspecting and Generating Control Events
295(3)
Labels Are Programmable
296(2)
Implementing the MessageBox Pop-Up Dialog
298(1)
The List Box for Unformatted Output
299(3)
Exploring the File Dialog
302(2)
A Pocketful of Buttons
304(2)
Serving Up Menus
306(2)
The DataGrid Control
308(2)
Adding a PictureBox Control
310(5)
ASP.NET and Web Forms Designer
315(34)
Our First Web Forms Program
316(1)
Opening an ASP.NET Web Application Project
316(4)
Modifying the Document Properties
318(1)
Adding Controls to the Document: Label
319(1)
Adding Pages to a Project
320(1)
The HyperLink Control: Linking to Other Pages
321(1)
The DataGrid Control
321(2)
Understanding the Page Event Life Cycle
323(2)
The Data Provider
325(1)
Web State Management
326(5)
Adding a TextBox Control
328(1)
Adding an ImageButton Control
329(1)
Adding a ListBox Control
329(2)
Managing State: Class Members
331(1)
Managing State: The Session Object
332(1)
Managing State: The Application Object
333(1)
Validation Controls
334(1)
Adding a DropDownList Control
335(2)
Adding a Group of RadioButton Controls
337(1)
Adding a CheckBoxList Control
338(2)
Adding Validators to a Control
340(4)
Adding a Calendar Control
344(1)
Adding an Image Control
345(1)
Programming Web Server Controls
345(4)
The Common Language Runtime
349(36)
Assemblies
349(4)
Runtime Type Reflection
353(5)
Modifying the Retrieval through BindingFlags
358(4)
Invoking a Method during Runtime
362(2)
Delegating the Test to Reflection
364(3)
Attributes
367(5)
The Intrinsic Conditional Attribute
367(2)
The Intrinsic Serializable Attribute
369(1)
The Intrinsic Dllimport Attribute
370(2)
Implementing Our Own Attribute Class
372(4)
Positional and Named Parameters
375(1)
AttributeUsage
376(1)
Runtime Discovery of Attributes Using Reflection
376(2)
The Intermediate Language
378(7)
Examining the Intermediate Language
379(2)
The ildasm Tool
381(4)
Index 385

Supplemental Materials

What is included with this book?

The New copy of this book will include any supplemental materials advertised. Please check the title of the book to determine if it should include any access cards, study guides, lab manuals, CDs, etc.

The Used, Rental and eBook copies of this book are not guaranteed to include any supplemental materials. Typically, only the book itself is included. This is true even if the title states it includes any access cards, study guides, lab manuals, CDs, etc.

Excerpts

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 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 demonstrate how to use the language and class framework to solve problems rather than simply to list language features and the class framework API.Learning C# is a two-step process: learning the details of the C# language and then becoming familiar with the .NET class framework. This two-step 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 versus value types, boxing, and so on. This step is covered in the 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 my company's Web site www.objectwrite.com .Mail can be sent to me directly at slippman@objectwrite.com . Organization of the BookThe 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.Chapter 1covers 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.Chapter 2covers the fundamentals of building classes. We look at access permission, distinguish between const and readonly members, and cover specialized 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 a function.Chapters 3 and 4cover, 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 frees resources. Classes holding database connections or window handles are both likely to implement IDisposable, although they are otherwise unrelated.Chapter 5provides 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 database connections, and the use of XML.Chapters 6 and 7cover, in turn, drag-and-drop Windows Forms and Web Forms development. Chapter 7 focuses on AS

Rewards Program