HAPPY BOOKSGIVING
Use code BOOKSGIVING during checkout to save 40%-55% on books and eBooks. Shop now.
Register your product to gain access to bonus material or receive a coupon.
In the tradition of Core Java , a clear, concise introduction to C#, using real code to solve real world problems
° Written in the proven Core style
° Written on C# 2.0
° Explains all the basic principles of .NET as well as C#
° Provides best practice tips throughout
The Complete and Comprehensive Developer's Guide to C# 2.0 and .NET 2.0
Core C# and .NET is the no-nonsense, example-rich guide to achieving exceptional results with C# 2.0 and .NET 2.0. Writing for experienced programmers, Stephen Perry presents today's best practices for leveraging both C# 2.0 language features and Microsoft's .NET 2.0 infrastructure.
Like all books in the Core Series, Core C# and .NET focuses on solving real-world problems with serious, non-trivial code. Perry's broad, deep coverage ranges from new C# generics to Web services, from reflection to security. He systematically introduces the development of Windows Forms applications and the effective use of GDI+ graphics classes. He offers detailed guidance on data management with XML and ADO.NET, plus advanced coverage of threading, remoting, and code security. Finally, Perry presents an extensive section on Web development, covering ASP.NET, state management, HTTP requests, and much more.
With practical insights into everything from scalability to localization, this is the C# book you've been searching for: your definitive guide to building production-quality C# applications.
Core C# and .NET delivers
EVERY CORE SERIES BOOK:
DEMONSTRATES practical techniques used by professional developers
FEATURES robust, thoroughly tested sample code and realistic examples
FOCUSES on the cutting-edge technologies you need to master today
PROVIDES expert advice that will help you build superior software
© Copyright Pearson Education. All rights reserved.
A .NET Framework Text and Font Primer
Understanding MDI Applications in .NET
Using .NET Windows Forms Controls
Download the Sample
Chapter related to this title. Also, download a special bonus
chapter.
About the Author.
Foreword.
Preface.
Acknowledgments.
I. FUNDAMENTALS OF C# PROGRAMMING AND INTRODUCTION TO .NET.
1. Introduction to .NET and C#.
Overview of the .NET Framework
Microsoft .NET and the CLI Standards
Common Language Runtime
Compiling .NET Code
Common Type System
Assemblies
Framework Class Library
Working with the .NET Framework and SDK
Updating the .NET Framework
.NET Framework Tools
Ildasm.exe
wincv.exe
Framework Configuration Tool
Understanding the C# Compiler
Locating the Compiler
Compiling from the Command Line
Summary
Test Your Understanding
2. C# Language Fundamentals.
The Layout of a C# Program
General C# Programming Notes
Primitives
decimal
bool
char
byte, sbyte
short, int, long
single, double
Using Parse and TryParse to Convert a Numeric String
Operators: Arithmetic, Logical, and Conditional
Arithmetic Operators
Conditional and Relational Operators
Control Flow Statements
if-else
switch
Loops
while loop
do loop
for loop
foreach loop
Transferring Control Within a Loop
C# Preprocessing Directives
Conditional Compilation
Diagnostic Directives
Code Regions
Strings
String Literals
String Manipulation
Enumerated Types
Working with Enumerations
System.Enum Methods
Enums and Bit Flags
Arrays
Declaring and Creating an Array
Using System.Array Methods and Properties
Reference and Value Types
System.Object and System.ValueType
Memory Allocation for Reference and Value Types
Boxing
Summary of Value and Reference Type Differences
Summary
Test Your Understanding
3. Class Design in C#.
Introduction to a C# Class
Defining a Class
Attributes
Access Modifiers
Abstract, Sealed, and Static Modifiers
Class Identifier
Base Classes, Interfaces, and Inheritance
Overview of Class Members
Member Access Modifiers
Constants, Fields, and Properties
Constants
Fields
Properties
Indexers
Methods
Method Modifiers
Passing Parameters
Constructors
Instance Constructor
Private Constructor
Static Constructor
Delegates and Events
Delegates
Delegate-Based Event Handling
Operator Overloading
Interfaces
Creating and Using a Custom Interface
Working with Interfaces
Generics
Structures
Defining Structures
Using Methods and Properties with a Structure
Structure Versus Class
Structures Are Value Types and Classes Are Reference Types
Unlike a Class, a Structure Cannot Be Inherited
General Rules for Choosing Between a Structure and a Class
Summary
Test Your Understanding
4. Working with Objects in C#.
Object Creation
Example: Creating Objects with Multiple Factories
Exception Handling
System.Exception Class
Writing Code to Handle Exceptions
Example: Handling Common SystemException Exceptions
How to Create a Custom Exception Class
Unhandled Exceptions
Exception Handling Guidelines
Implementing System.Object Methods in a Custom Class
ToString() to Describe an Object
Equals() to Compare Objects
Cloning to Create a Copy of an Object
Working with .NET Collection Classes and Interfaces
Collection Interfaces
System.Collections Namespace
Stack and Queue
ArrayList
Hashtable
System.Collections.Generic Namespace
Object Serialization
Binary Serialization
Object Life Cycle Management
.NET Garbage Collection
Summary
Test Your Understanding
II. CREATING APPLICATIONS USING THE .NET FRAMEWORK CLASS LIBRARY.
5. C# Text Manipulation and File I/O.
Characters and Unicode
Unicode
Working with Characters
The String Class
Creating Strings
Overview of String Operations
Comparing Strings
Using String.Compare
Using String.CompareOrdinal
Searching, Modifying, and Encoding a String's Content
Searching the Contents of a String
Searching a String That Contains Surrogates
String Transformations
String Encoding
StringBuilder
StringBuilder Class Overview
StringBuilder Versus String Concatenation
Formatting Numeric and DateTime Values
Constructing a Format Item
Formatting Numeric Values
Formatting Dates and Time
Regular Expressions
The Regex Class
Creating Regular Expressions
A Pattern Matching Example
Working with Groups
Examples of Using Regular Expressions
System.IO: Classes to Read and Write Streams of Data
The Stream Class
FileStreams
MemoryStreams
BufferedStreams
Using StreamReader and StreamWriter to Read and Write Lines of Text
StringWriter and StringReader
Encryption with the CryptoStream Class
System.IO: Directories and Files
FileSystemInfo
Working with Directories Using the DirectoryInfo, Directory, and Path Classes
Working with Files Using the FileInfo and File Classes
Summary
Test Your Understanding
6. Building Windows Forms Applications.
Programming a Windows Form
Building a Windows Forms Application by Hand
Windows.Forms Control Classes
The Control Class
Working with Controls
Control Events
The Form Class
Setting a Form's Appearance
Setting Form Location and Size
Displaying Forms
The Life Cycle of a Modeless Form
Forms Interaction--A Sample Application
Owner and Owned Forms
Message and Dialog Boxes
Multiple Document Interface Forms
Working with Menus
MenuItem Properties
Context Menus
Adding Help to a Form
ToolTips
Responding to F1 and the Help Button
The HelpProvider Component
Forms Inheritance
Building and Using a Forms Library
Using the Inherited Form
Summary
Test Your Understanding
7. Windows Forms Controls.
A Survey of .NET Windows Forms Controls
Button Classes, Group Box, Panel, and Label
The Button Class
The CheckBox Class
The RadioButton Class
The GroupBox Class
The Panel Class
The Label Class
PictureBox and TextBox Controls
The PictureBox Class
The TextBox Class
ListBox, CheckedListBox, and ComboBox Classes
The ListBox Class
Other List Controls: the ComboBox and the CheckedListBox
The ListView and TreeView Classes
The ListView Class
The TreeView Class
The ProgressBar, Timer, and StatusStrip Classes
Building a StatusStrip
Building Custom Controls
Extending a Control
Building a Custom UserControl
A UserControl Example
Using the Custom User Control
Working with the User Control at Design Time
Using Drag and Drop with Controls
Overview of Drag and Drop
Using Resources
Working with Resource Files
Using Resource Files to Create Localized Forms
Summary
Test Your Understanding
8. .NET Graphics Using GDI+.
GDI+ Overview
The Graphics Class
The Paint Event
Using the Graphics Object
Basic 2-D Graphics
Pens
Brushes
Colors
A Sample Project: Building a Color Viewer
Images
Loading and Storing Images
Manipulating Images
Sample Project: Working with Images
A Note on GDI and BitBlt for the Microsoft Windows Platform
Summary
Test Your Understanding
9. Fonts, Text, and Printing.
Fonts
Font Families
The Font Class
Drawing Text Strings
Drawing Multi-Line Text
Formatting Strings with the StringFormat Class
Using Tab Stops
String Trimming, Alignment, and Wrapping
Printing
Overview
PrintDocument Class
Printer Settings
Page Settings
PrintDocument Events
PrintPage Event
Previewing a Printed Report
A Report Example
Creating a Custom PrintDocument Class
Summary
Test Your Understanding
10. Working with XML in .NET.
Working with XML
Using XML Serialization to Create XML Data
XML Schema Definition (XSD)
Using an XML Style Sheet
Techniques for Reading XML Data
XmlReader Class
XmlNodeReader Class
The XmlReaderSettings Class
Using an XML Schema to Validate XML Data
Options for Reading XML Data
Techniques for Writing XML Data
Using XPath to Search XML
Constructing XPath Queries
XmlDocument and Xpath
XPathDocument and Xpath
XmlDataDocument and Xpath
Summary
Test Your Understanding
11. ADO.NET.
Overview of the ADO.NET Architecture
OLE DB Data Provider in .NET
.NET Data Provider
Data Access Models: Connected and Disconnected
Connected Model
Disconnected Model
ADO.NET Connected Model
Connection Classes
The Command Object
DataReader Object
DataSets, DataTables, and the Disconnected Model
The DataSet Class
DataTables
Loading Data into a DataSet
Using the DataAdapter to Update a Database
Defining Relationships Between Tables in a DataSet
Choosing Between the Connected and Disconnected Model
XML and ADO.NET
Using a DataSet to Create XML Data and Schema Files
Creating a DataSet Schema from XML
Reading XML Data into a DataSet
Summary
Test Your Understanding
12. Data Binding with Windows Forms Controls.
Overview of Data Binding
Simple Data Binding
Complex Data Binding with List Controls
One-Way and Two-Way Data Binding
Using Binding Managers
Using Simple and Complex Data Binding in an Application
Binding to a DataTable
Binding Controls to an ArrayList
Adding an Item to the Data Source
Identifying Updates
Update Original Database with Changes
The DataGridView Class
Properties
Events
Setting Up Master-Detail DataGridViews
Virtual Mode
Summary
Test Your Understanding
III. ADVANCED USE OF C# AND THE .NET FRAMEWORK.
13. Asynchronous Programming and Multithreading.
What Is a Thread?
Multithreading
Asynchronous Programming
Asynchronous Delegates
Examples of Implementing Asynchronous Calls
Working Directly with Threads
Creating and Working with Threads
Multithreading in Action
Using the Thread Pool
Timers
Thread Synchronization
The Synchronization Attribute
The Monitor Class
The Mutex
The Semaphore
Avoiding Deadlock
Summary of Synchronization Techniques
Summary
Test Your Understanding
14. Creating Distributed Applications with Remoting.
Application Domains
Advantages of AppDomains
Application Domains and Assemblies
Working with the AppDomain Class
Remoting
Remoting Architecture
Types of Remoting
Client-Activated Objects
Server-Activated Objects
Type Registration
Remoting with a Server-Activated Object
Remoting with a Client-Activated Object (CAO)
Design Considerations in Creating a Distributed Application
Leasing and Sponsorship
Leasing
Sponsorship
Summary
Test Your Understanding
15. Code Refinement, Security, and Deployment.
Following .NET Code Design Guidelines
Using FxCop
Strongly Named Assemblies
Creating a Strongly Named Assembly
Delayed Signing
Global Assembly Cache (GAC)
Versioning
Security
Permissions and Permission Sets
Evidence
Security Policies
Configuring Security Policy
The .NET Framework Configuration Tool
Configuring Code Access Security with the Configuration Tool--An Example
Requesting Permissions for an Assembly
Programmatic Security
Application Deployment Considerations
Microsoft Windows Deployment: XCOPY Deployment Versus the Windows Installer
Deploying Assemblies in the Global Assembly Cache
Deploying Private Assemblies
Using CodeBase Configuration
Using a Configuration File to Manage Multiple Versions of an Assembly
Assembly Version and Product Information
Summary
Test Your Understanding
IV. PROGRAMMING FOR THE INTERNET.
16. ASP.NET Web Forms and Controls.
Client-Server Interaction over the Internet
Web Application Example: Implementing a BMI Calculator
Using ASP.NET to Implement a BMI Calculator
Inline Code Model
The Code-Behind Model
Code-Behind with Partial Classes
Page Class
Web Forms Controls
Web Controls Overview
Specifying the Appearance of a Web Control
Simple Controls
List Controls
The DataList Control
Data Binding and Data Source Controls
Binding to a DataReader
Binding to a DataSet
DataSource Controls
Validation Controls
Using Validation Controls
Master and Content Pages
Creating a Master Page
Creating a Content Page
Accessing the Master Page from a Content Page
Building and Using Custom Web Controls
A Custom Control Example
Using a Custom Control
Control State Management
Composite Controls
Selecting a Web Control to Display Data
Summary
Test Your Understanding
17. The ASP.NET Application Environment.
HTTP Request and Response Classes
HttpRequest Object
HttpResponse Object
ASP.NET and Configuration Files
A Look Inside web.config
Adding a Custom Configuration Section
ASP.NET Application Security
Forms Authentication
An Example of Forms Authentication
Maintaining State
Application State
Session State
Caching
Page Output Caching
Data Caching
Creating a Web Client with WebRequest and WebResponse
WebRequest and WebResponse Classes
Web Client Example
HTTP Pipeline
Processing a Request in the Pipeline
HttpApplication Class
HTTP Modules
HTTP Handlers
Summary
Test Your Understanding
18. XML Web Services.
Introduction to Web Services
Discovering and Using a Web Service
Building an XML Web Service
Creating a Web Service by Hand
Creating a Web Service Using VS.NET
Extending the Web Service with the WebService and WebMethod Attributes
Building an XML Web Service Client
Creating a Simple Client to Access the Web Service Class
Creating a Proxy with Visual Studio.NET
Understanding WSDL and SOAP
Web Services Description Language (WSDL)
Simple Object Access Protocol (SOAP)
Using Web Services with Complex Data Types
A Web Service to Return Images
Using Amazon Web Services
Creating a Proxy for the Amazon Web Services
Building a WinForms Web Service Client
Web Services Performance
Configuring the HTTP Connection
Working with Large Amounts of Data
Summary
Test Your Understanding
Appendix A. Features Specific to .NET 2.0 and C# 2.0.
Appendix B. DataGridView Events and Delegates.
Answers to Chapter Exercises.
Index.
Donald Knuth, preface to Fundamental Algorithms (1968)
Thirty-seven years later, programmers still experience the same creative satisfaction from a well-crafted program. It can be ten lines of recursive code that pops into one's head at midnight, or it can be an entire production management system whose design requires a year of midnights. Then, as now, good programs still convey an impression of logic and naturalnessparticularly to their users.
But the challenges have evolved. Software is required to be more malleableit may be run from a LAN, the Internet, or a cellular phone. Security is also a much bigger issue, since the code may be accessible all over the world. This, in turn, raises issues of scalability and how to synchronize code for hundreds of concurrent users. More users bring more cultures, and the concomitant need to customize programs to meet the language and culture characteristics of a worldwide client base.
. NETand the languages written for itaddresses these challenges as well as any unified development environment. This book is written for developers, software architects, and students who choose to work with the .NET Framework. All code in the book is written in C#, although only one chapter is specifically devoted to the syntactical structure of the C# language.
This book is not an introduction to programmingit assumes you are experienced in a computer language. This book is not an introduction to object oriented programming (OOP)although it will re-enforce the principles of encapsulation, polymorphism, and inheritance through numerous examples. Finally, this book is not an introduction to using Visual Studio .NET to develop C# programs. VS.NET is mentioned, but the emphasis is on developing and understanding C# and the .NET classesindependent of any IDE.
This book is intended for the experienced programmer who is moving to .NET and wants to get an overall feel for its capabilities. You may be a VB6 or C++ programmer seeking exposure to .NET; a VB.NET programmer expanding your repertoire into C#; orand yes it does happen occasionallya Java programmer investigating life on the far side. Here's what you'll find if you choose to journey through this book.
18 Chapters. The first four chapters should be read in order. They provide an introduction to C# and a familiarity with using the .NET Class libraries. The remaining chapters can be read selectively based on your interests. Chapters 6 and 7 describe how to develop Windows Forms applications. Chapter 8 and 9 deal with GDI+the .NET graphics classes. Chapters 10 through 12 are about working with data. Both XML and ADO.NET are discussed. Chapters 13, 14, and 15 tackle the more advanced topics of threading, remoting, and code security, respectively. The final chapters form a Web trilogy: Chapter 16 discusses ASP.NET Web page development; Chapter 17 looks behind the scenes at how to manage state information and manage HTTP requests; the book closes with a look at Web Services.
While some will disagree, if you really want to learn C# and .NET, shut down
your IDE, pull out your favorite text editor, and learn how to use the C# compiler
from the command line. Once you have mastered the fundamentals, you can switch
to VS.NET and any other IDE for production programming.
Finally, a word about .NET and Microsoft. This book was developed using Microsoft .NET 1.x and Whidbey betas. It includes topics such as ADO.NET and ASP.NET that are very much a Microsoft proprietary implementations. In fact Microsoft has applied to patent these methodologies. However all of C# and many of the .NET basic class libraries are based on a standard that enables them to be ported to other platforms. Now, and increasingly in the future, many of the techniques described in this book will be applicable to .NET like implementations (see Mono) on non-Windows platforms.
Download the Index
file related to this title.