Amazon.co.uk Review
It's no secret that many computer books are pretty much devoid of an authorial personality. This title is a winning exception. The author is able to weave in clever examples (using such topics as his own long experience in computing, his dog, Star Trek, etc.) without being coy or getting in the way of presenting real technical information. Liberty's wide experience in computers and general writing skill shows, as he is able to draw on a wealth of examples to move his text forward.
These are a couple of goals at work in Programming C#. First, it's an excellent language tutorial, certainly one of the smartest and best available guides to C# as a language. Early chapters explore basic and obscure language options using inheritance, delegation, interface and the conventions in C# used to implement these techniques. The middle part of the book turns toward the .NET Framework itself, with two useful (and somewhat introductory) chapters on both Windows Forms and Web Forms, for standalone and Web-based applications, respectively.
Later sections crank up the technical knowledge again with several advanced topics on understanding .NET assemblies and deployment in detail, as well as "reflection" APIs that allow .NET programs to essentially modify their code at run time. (One technique, reflection emit, that literally writes bytecodes, will definitely interest expert readers, though it's unlikely most programmers will need to do this.) Final sections look at the .NET stream classes (rivalled only by Java's for complexity). Liberty looks at basic file and network I/O as well as how objects get serialised and marshalled both for SOAP and Web services and "normal" .NET remoting.
The author's sure hand here in navigating the difficult waters of C# and .NET makes for a relatively concise text that is chock-full of useful information on C#. Filled with notably clever and inventive examples, this book is possibly the veteran computer author's best title to date, and it's sure to be a noteworthy resource as experienced developers tackle C# for the first time. --Richard Dragan --This text refers to an out of print or unavailable edition of this title.
www.techbookreport.com
Jeff Richards, Windowatch, June 2002
Salt Lake City ColdFusion User Group, June 2002
Thomas Paul, JavaRanch.com, July 2002
Product Description
The programming language C# was built with the future of application development in mind. Pursuing that vision, C#'s designers succeeded in creating a safe, simple, component-based, high-performance language that works effectively with Microsoft's .NET Framework. Now the favored language among those programming for the Microsoft platform, C# continues to grow in popularity as more developers discover its strength and flexibility. And, from the start, C# developers have relied on Programming C# both as an introduction to the language and a means of further building their skills.
The fourth edition of Programming C#--the top-selling C# book on the market--has been updated to the C# ISO standard as well as changes to Microsoft's implementation of the language. It also provides notes and warnings on C# 1.1 and C# 2.0.
Aimed at experienced programmers and web developers, Programming C#, 4th Edition, doesn't waste too much time on the basics. Rather, it focuses on the features and programming patterns unique to the C# language. New C# 2005 features covered in-depth include:
- Visual Studio 2005
- Generics
- Collection interfaces and iterators
- Anonymous methods
- New ADO.NET data controls
- Fundamentals of Object-Oriented Programming
Liberty also incorporates reader suggestions from previous editions to help create the most consumer-friendly guide possible.
From the Publisher
Adult birds stand about three feet tall and weigh six to nine pounds. Inside their long necks is a five-foot long windpipe-part of which is coiled inside their breastbone-giving voice to loud calls that can carry for miles. They live for about 22 years, spending most of their waking hours looking for the various plants, small animals, and insects they like to eat. (One crowned crane food-finding technique, perfected during the 38 to 54 million years these birds have been around, is to stamp their feet as they walk, flushing out tasty bugs.) They are the only type of crane to perch in trees, which they do at night when sleeping.
Social and talkative, African crowned cranes group together in pairs or families, and the smaller groups band together in flocks of more than 100 birds. Their elaborate mating dance has served as a model for some of the dances of local groups of people.
Darren Kelly was the production editor and Audrey Doyle was the proofreader for Programming C#. Mary Brady and Claire Cloutier provided quality control. Joe Wizda wrote the index. Interior composition was done by James Carter, Matthew Hutchinson, and Edith Shapiro.
Ellie Volckhausen designed the cover of this book, based on a series design by Edie Freedman. The cover image is an original antique engraving from the 19th century. Emma Colby produced the cover layout with QuarkXPress 4.1 using Adobe's ITC Garamond font.
David Futato designed the interior layout based on a series design by Nancy Priest. Neil Walls converted the files from Microsoft Word to FrameMaker 5.5.6 using tools created by Mike Sierra. The text and heading fonts are ITC Garamond Light and Garamond Book; the code font is Constant Willison. The illustrations that appear in this book were produced by Robert Romano and Jessamyn Read using Macromedia FreeHand 9 and Adobe Photoshop 6. This colophon was written by Leanne Soylemez.
Our look is the result of reader comments, our own experimentation, and feedback from distribution channels. Distinctive covers complement our distinctive approach to technical topics, breathing personality and life into potentially dry subjects. --This text refers to an out of print or unavailable edition of this title.
About the Author
Excerpted from Programming C# by Jesse Liberty. Copyright © 2005. Reprinted by permission. All rights reserved.
When a head of state dies, the president of the United States typically doesnt have time to attend the funeral personally. Instead, he dispatches a delegate. Often this delegate is the vice president, but sometimes the VP is unavailable and the president must send someone else, such as the secretary of state or even the first lady. He doesnt want to "hardwire" his delegated authority to a single person; he might delegate this responsibility to anyone who is able to execute the correct international protocol.
The president defines in advance what responsibility will be delegated (attend the funeral), what parameters will be passed (condolences, kind words), and what value he hopes to get back (good will). He then assigns a particular person to that delegated responsibility at "runtime" as the course of his presidency progresses.
In programming, you are often faced with situations where you need to execute a particular action, but you dont know in advance which method, or even which object, youll want to call upon to execute it. For example, a button might know that it must notify some object when it is pushed, but it might not know which object or objects need to be notified. Instead of wiring the button to a particular object, you will connect the button to a delegate and then resolve that delegate to a particular method when the program executes.
In the early, dark, and primitive days of computing, a program would begin execution and then proceed through its steps until it completed. If the user was involved, the interaction was strictly controlled and limited to filling in fields.
Todays GUI programming model requires a different approach, known as event-driven programming. A modern program presents the user interface and waits for the user to take an action. The user might take many different actions, such as choosing among menu selections, pushing buttons, updating text fields, clicking icons, and so forth. Each action causes an event to be raised. Other events can be raised without direct user action, such as events that correspond to timer ticks of the internal clock, email being received, file-copy operations completing, etc.
An event is the encapsulation of the idea that "something happened" to which the program must respond. Events and delegates are tightly coupled concepts because flexible event handling requires that the response to the event be dispatched to the appropriate event handler. An event handler is typically implemented in C# via a delegate.
Delegates are also used as callbacks so that one class can say to another "do this work and when youre done, let me know."
Delegates
In C#, delegates are first-class objects, fully supported by the language. Technically, a delegate is a reference type used to encapsulate a method with a specific signature and return type.* You can encapsulate any matching method in that delegate.
In C++ and many other languages, you can to some degree accomplish this requirement with function pointers and pointers to member functions.
A delegate is created with the delegate keyword, followed by a return type and the signature of the methods that can be delegated to it, as in the following:
public delegate int WhichIsFirst(object obj1, object obj2);
This declaration defines a delegate named WhichIsFirst, which will encapsulate any method that takes two objects as parameters and that returns an int.
Once the delegate is defined, you can encapsulate a member method with that delegate by instantiating the delegate, passing in a method that matches the return type and signature. As an alternative, you can use anonymous methods as described later. In either case, the delegate can then be used to invoke that encapsulated method.
Using Delegates to Specify Methods at Runtime
Delegates decouple the class that declares the delegate from the class that uses the delegate. For example, suppose that you want to create a simple generic container class called a Pair that can hold and sort any two objects passed to it. You cant know in advance what kind of objects a Pair will hold, but by creating methods within those objects to which the sorting task can be delegated, you can delegate responsibility for determining their order to the objects themselves.
* If the method is an instance method, the delegate encapsulates the target object as well.
Different objects will sort differently (for example, a Pair of Counter objects might sort in numeric order, while a Pair of Buttons might sort alphabetically by their name). As the author of the Pair class, you want the objects in the pair to have the responsibility of knowing which should be first and which should be second. To accomplish this, you will insist that the objects to be stored in the Pair must provide a method that tells you how to sort the objects.
You can define this requirement with interfaces, as well. Delegates are smaller and of finer granularity than interfaces. The Pair class doesnt need to implement an entire interface, it just needs to define the signature and return type of the method it wants to invoke. That is what delegates are for: they define the return type and signature of methods that can be invoked through the interface.
In this case, the Pair class will declare a delegate named WhichIsFirst. When the Pair needs to know how to order its objects, it will invoke the delegate passing in its two member objects as parameters. The responsibility for deciding which of the two objects comes first is delegated to the method encapsulated by the delegate:
public delegate Comparison
WhichIsFirst( T obj1, T obj2 )
In this definition, WhichIsFirst is defined to encapsulate a method that takes two objects as parameters, and that returns an object of type Comparison. Comparison turns out to be an enumeration you will define:
public enum Comparison$N