C# 2.0: Practical Guide for Programmers

C# 2.0: Practical Guide for Programmers

NOOK Book(eBook)

$27.49 $31.95 Save 14% Current price is $27.49, Original price is $31.95. You Save 14%.
View All Available Formats & Editions
Available on Compatible NOOK Devices and the free NOOK Apps.
Want a NOOK ? Explore Now


C# 2.0: Practical Guide for Programmers by Michel de Champlain, Brian G. Patrick

You don’t need coddling; you don’t need to be told what you already know. What you need is a book that uses your experience as a Java or C++ programmer to give you a leg up into the challenges and rewards of C#. And this Practical Guide is precisely what you’re after.

Written by a team that boasts extensive experience teaching C# to professionals, this book provides a practical, efficient explanation of the language itself, covering basic to advanced features and calling out all that’s new in 2.0. Its instruction is always firmly situated within the context of the .NET framework and bolstered by code examples, key lessons in object-oriented programming, and installments of a realistic application programming tutorial. Concise and incisive, this is the best way to master the world’s fastest-growing and most marketable programming language.

* Provides a carefully focused explanation of every aspect of the C# language, including entire chapters on the unified type system, advanced types, collections, generics, reflection and attributes.
* Highlights all features new to the latest version of C# and organizes its presentation of C# according to the key principles of object-oriented programming and the .NET framework.
* Using end-of-chapter exercises, incrementally develops a cohesive application programming tutorial.
* Includes an easy-to-use appendix on the grammatical rules of C# and provides downloadable example code via the companion website.

* Provides a carefully focused explanation of every aspect of the C# language, including entire chapters on the unified type system, advanced types, collections, generics, reflection and attributes.
* Highlights all features new to the latest version of C# and organizes its presentation of C# according to the key principles of object-oriented programming and the .NET framework.
* Using end-of-chapter exercises, incrementally develops a cohesive application programming tutorial.
* Includes an easy-to-use appendix on the grammatical rules of C# and provides downloadable example code via the companion website.

Product Details

ISBN-13: 9780080488431
Publisher: Elsevier Science
Publication date: 03/30/2005
Series: Practical Guides
Sold by: Barnes & Noble
Format: NOOK Book
Pages: 272
File size: 9 MB

Read an Excerpt

C# 2.0: Practical Guide for Programmers

By Michel de Champlain Brian G. Patrick


Copyright © 2005 Elsevier Inc.
All right reserved.

ISBN: 978-0-08-048843-1

Chapter One

Introducing C# and .NET

In the late 1990s, Microsoft created Visual J++ in an attempt to use Java in a Windows context and to improve the interface of its Component Object Model (COM). Unable to extend Java due to proprietary rights held by Sun, Microsoft embarked on a project to replace and improve Visual J++, its compiler, and its virtual machine with a general-purpose, object-oriented language. To head up this project, Microsoft engaged the talents of Anders Hejlsberg, formerly of Borland and the principal author of Windows Foundation Classes (WFC), Turbo Pascal, and Delphi. As a result of this effort, C# was first introduced in July 2000 as a thoroughly modern object-oriented language that would ultimately serve as the main development language of the Microsoft .NET platform.

In this short introductory chapter, we lay out the fundamental features of the C# programming language and the .NET Framework. We also outline the requirements of a small project that will serve as an ongoing exercise throughout the text. The chapter ends with a few words on syntactic notation.

1.1 What Is C#?

As part of the lineage of C-based languages, C# has incorporated and exploited programming language features with a proven record of success and familiarity. To that end, most syntactic features of C# are borrowed from C/C++, and most of its object-oriented concepts, such as garbage collection, reflection, the root class, and the multiple inheritance of interfaces, are inspired by Java. Improvements in C# over Java, often with syntax simplification, have been applied to iteration, properties, events, metadata, versioning, and the conversion between simple types and objects.

In addition to being syntactically familiar, C# is strongly typed, architecturally neutral, portable, safe, and multi-threaded. Type security in C# is supported in a number of ways, including initializing variables before their use, eliminating dangerous explicit type conversions, controlling the limits in arrays, and checking the overflow of type limits during arithmetic operations. Its architecturally neutral intermediate format, implemented as the Common Intermediate Language (CIL) and executed on a virtual machine, makes

C# portable and independent of its running environment. C# is also safe. It controls access to hardware and memory resources, checks classes at runtime, and does not allow the implicit usage and manipulation of pointers (as C/C++ do). The explicit use of pointers, on the other hand, is restricted to sections of code that have been designated as unsafe. With the support of a garbage collector, frustrating memory leaks and dangling pointers are a non-issue. The C# language also supports multi-threading in order to promote efficient interactive applications such as graphics, input/output, and so on. Other modern features in C# include Just-in-Time (JIT) compilation from bytecode to native code, exceptions for error handling, namespaces for preventing type collisions, and documentation comments.

In order to promote the widespread use and acceptance of C#, Microsoft relinquished its proprietary rights. With the support of Hewlett-Packard and Intel, Microsoft quickly pushed for a standardized version of C#. In December 2001, the first standard was accepted by the European Computer Manufacturer Association (ECMA). The following December, a second standard was adopted by the ECMA, and it was accepted 3 months later by the International Organization for Standardization (ISO). The standardization of C# has three principal benefits:

1. To support the portability of C# applications across different hardware architectures,

2. To foster the development of C# compilers among different manufacturers, and

3. To encourage the emergence of high-quality software tools to support the development of C# applications.

In this text, C# 2.0 is used as the final arbiter of the language.

1.2 What Is the .NET Framework?

The .NET Framework provides a new platform for building applications that are easily deployed and executed across multiple architectures and operating systems. This portability is achievable only because of ongoing standardization through the ECMA and ISO organizations. In this way, the framework offers independence to languages by supplying an international standard called the Common Language Infrastructure (CLI).

The framework was designed to be installed on top of an operating system and is divided into two main layers, as shown in Figure 1.1: a runtime environment called the Common Language Runtime (CLR), similar to the Java Virtual Machine, and a large library of classes called the Framework Class Library (FCL), which provides the required services for modern applications.

The bottom layer of the .NET Framework contains the CLR. The CLR provides the runtime services to execute C# programs that have been translated into the CIL. The top layer encapsulates all services in the FCL for user interface, control, security, data access, Extensible Markup Language (XML), input/output, threading, and so on. User interface (UI) services—both Window and Web Forms—support graphic interfaces and server-side controls, respectively. ASP.NET provides control, security, sessioning, and configuration for dynamic web pages. Data access by ADO.NET adds XML as an intermediate format for data and supports connections to datasets using XML caches. The FCL also contains system classes to manage I/O, execution threads, serialization, reflection, networking, collections, diagnostics, debugging, and so on.

Applications and development tools are typically layered on top of the .NET Framework. Visual Studio .NET, in particular, is a good example. It provides an integrated development environment (IDE) that standardizes support for many programming languages, including C#, J#, C++, and Visual Basic.

After the standardization of the C# and CLI specifications in December 2001, Microsoft released the CLR as both a commercial implementation of the CLI runtime virtual machine and a subset of the FCL. Since then, C# has become the programming language of choice for developing applications in the .NET Framework. CLR, FCL, and the C# compiler are all released as part of the .NET Framework Software Development Kit (SDK), which is freely available from Microsoft at http://msdn.microsoft.com. At the time of this writing, there are other .NET implementations in progress, such as the open-source Mono and DotGNU projects. All these implementations include a C# compiler that extends language availability to platforms other than Windows.

The C# code executed on this framework follows object-oriented development practices defined by the Common Language Specification (CLS). The CLS defines a collaboration standard between languages and object development practices. Obviously, some older traditional programming languages, such as COBOL and Fortran, cannot exploit the full characteristics offered by the CLS. The Common Type System (CTS) of the .NET Framework represents a standardized set of basic data types that permit language interoperability. In other words, the CTS defines the rules implemented in the CLR. The CLS supports a (common) subset of the CTS in order to allow cross-language integration. Therefore, a CLS-compliant component can be used by applications written in other languages.

The following subsections highlight the relationships between a number of important features of the .NET Framework and the C# programming language, including the .NET virtual machine, .NET virtual code, and .NET assemblies.

1.2.1 The .NET Virtual Machine: Common Language Runtime

The CLR is the .NET virtual machine. It handles the compiling, loading, and execution of a C# application. The compiling process employs a JIT approach that translates the CIL into machine code as required. In addition to a traditional runtime system, it also provides debugging and profiling functionalities. The CLR implements the CTS, which defines types and data. Moreover, C# applications contain a complete description of their types, called metadata, providing code visibility to other applications or tools. With this metadata, the CLR uses reflection in order to resolve library references, link components, and resolve types at runtime. The garbage collector is a subsystem of the CLR that cleans up memory that is no longer needed. It frees developers of the tedious and error-prone responsibility of recovering (deleting or deallocating) memory space allocated during object creation.

1.2.2 The .NET Virtual Code: Intermediate Language

The applications written in C# are not traditional Windows programs compiled into machine code. Rather, the C# compiler generates CIL code, often referred to as managed code. This code is dedicated to run safely within the .NET environment. In fact, the CLR takes care of the back-end part of the compilation before execution, allowing the possibility of JIT translation from CIL code into native machine code without compromising security. On the other hand, unmanaged code, such as that generated by C/C++ compilers in the Windows environment, uses native and potentially dangerous instructions (for example, pointers). Like Java bytecode, CIL is also virtual machine code and is therefore completely independent of any underlying processor architecture. It is fully cross-language compatible on the .NET platform, offering at the time of this writing support for many different programming languages. Therefore, all programs implemented in any of these languages and compiled into CIL may share components without any extra effort.

1.2.3 The .NET Assemblies: Applications and/or Components

An assembly is the logical unit of deployment in .NET and encompasses two kinds of implementation units: applications (.exe) and components (.dll). Whereas applications represent fully executable C# programs, components represent core reusable objects that provide basic services to build up applications. Indeed, Microsoft prefers to call C# a component-oriented rather than an object-oriented programming language.

Each assembly is either private or public and contains a manifest (a set of metadata) that provides information about its implementation units, such as name, owner, version, security permissions, culture, processor, operating system, public key signature, and all other needed resources (such as bitmaps). Private assemblies are used only by the application that installed them, but public (shared) assemblies are stored in a repository maintained by the .NET Framework called the Global Assembly Cache (GAC). Finally, because every assembly contains version information, the CLR is able to handle multiple versions of the same component on the same platform.

1.3 Project Exercise

Throughout this text, the exercises at the end of most chapters are based on a small project. The project was chosen to offer a nice continuity among the exercises and to provide the reader with a practical application that can be used, reused, and modified. All the source code for the exercises is available and maintained on the web site of DeepObjectKnowledge (http://www.DeepObjectKnowledge.com).

The project consists of two distinct applications, each of which will be presented incrementally throughout the text. The first application allows a user to enter, modify, or delete an organization, its domain, and its e-mail format. Using the keywords First(F) and Last(L), e-mail formats can be represented in any number of ways, as shown below for the contact name John Smith.

Email Format Resulting Name

First.Last John.Smith Last.First Smith.John F.Last J.Smith First+Last JohnSmith Last+First SmithJohn ...

The second application allows a user to enter, modify, or delete a contact's name, organization, and e-mail address. However, using a property file generated by the first application, the e-mail address of the contact may also be deduced from the corresponding e-mail format of an existing organization. The latter approach generates contact information (in this case, the e-mail address) quickly and accurately.


Excerpted from C# 2.0: Practical Guide for Programmers by Michel de Champlain Brian G. Patrick Copyright © 2005 by Elsevier Inc.. Excerpted by permission of MORGAN KAUFMANN PUBLISHERS. All rights reserved. No part of this excerpt may be reproduced or reprinted without permission in writing from the publisher.
Excerpts are provided by Dial-A-Book Inc. solely for the personal use of visitors to this web site.

Table of Contents

1 Introducing C# and .NET
2 Classes, Objects, and Namespaces
3 Class Members and Class Reuse
4 Unified Type System
5 Operators, Assignments, and Expressions
6 Statements and Exceptions
7 Advanced Types, Polymorphism, and Accessors
8 Collections and Generics
9 Resource Disposal, Input/Output, and Threads
10 Reflection and Attributes
A C# 2.0 Grammar
B Predefined XML Tags for Documentation Comments

Customer Reviews

Most Helpful Customer Reviews

See All Customer Reviews