Sams Teach Yourself C# 5.0 in 24 Hours
Scott J. Dorman
800 East 96th Street, Indianapolis, Indiana, 46240 USA
Sams Teach Yourself C# 5.0 in 24 Hours
Copyright 2013 by Pearson Education
All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission from the publisher. No patent liability is assumed with respect to the use of the information contained herein. Although every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions. Nor is any liability assumed for damages resulting from the use of the information contained herein.
This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/).
ISBN-13: 978-0-672-33684-3
ISBN-10: 0-672-33684-7
The Library of Congress Cataloging-in-Publication data is on file.
Printed in the United States of America
First Printing November 2012
Editor-in-Chief
Greg Wiegand
Executive Editor
Neil Rowe
Development Editor
Mark Renfrow
Managing Editor
Kristy Hart
Project Editor
Betsy Harris
Copy Editor
Karen Annett
Indexer
Christine Karpeles
Proofreader
Debbie Williams
Technical Editor
Christopher Wilcox
Publishing Coordinator
Cindy Teeters
Cover Designer
Anne Jones
Compositor
Nonie Ratcliff
Trademarks
All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized. Sams Publishing cannot attest to the accuracy of this information. Use of a term in this book should not be regarded as affecting the validity of any trademark or service mark.
Warning and Disclaimer
Every effort has been made to make this book as complete and as accurate as possible, but no warranty or fitness is implied. The information provided is on an as is basis. The author and the publisher shall have neither liability nor responsibility to any person or entity with respect to any loss or damages arising from the information contained in this book.
Bulk Sales
Sams Publishing offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales. For more information, please contact
U.S. Corporate and Government Sales
1-800-382-3419
For sales outside of the U.S., please contact
International Sales
Contents at a Glance
Table of Contents
Foreword
At the end of the previous millennium, a small team of designers met in a small conference room on the second floor of Building 41 at Microsoft to create C#, a then brand-new language. The designers wanted the language to be easy to understand but not simplistic, familiar to C++ and Java programmers but not a slavish copy of either, safe by default but not too restrictive, and practical but without abandoning a disciplined, consistent, and theoretically valid design.
After many months of thought, design, research, development, testing, and documentation, C# 1.0 was delivered to the public. It was a pretty straightforward object-oriented language. Many of the details of its design were carefully chosen to ensure that objects could be organized into independently versionable components, but the fundamental concepts of the language all came from ideas developed in object-oriented and procedural languages going back to the 1970s or earlier.
The design team continued to meet three times a week in that same second-floor conference room to build upon the solid base established by C# 1.0. By working with colleagues in Microsoft Research Cambridge and the CLR team across the street, the type system was extended to support parametric polymorphism on generic types and methods. They added iterator blocks to make it easier to build collection types. Generic types and iterators had been pioneered by earlier languages such as CLU and Ada in the 1970s and 1980s. They also added anonymous methods; the idea of embedding anonymous methods in an existing method goes all the way back to the foundations of modern computer science in the 1950s.
C# 2.0 was a huge step up from its predecessor, but still the design team was not content. They continued to meet in that same second-floor conference room three times a week. This time, they were thinking about fundamentals. They realized that programmers manipulate data by combining relatively simple operations in complex ways; those operations typically include sorting, filtering, grouping, joining, and projecting collections of data. The concept of a syntactic pattern for query comprehensions that concisely describe these operations was originally developed in functional languages such as Haskell but also works well in a more imperative language like C#. And thus LINQLanguage Integrated Querywas born.
After ten years of meeting for six hours a week in the same conference room, the need to teleconference with offsite team members motivated a change of venue to the fifth floor. The design team looked back on the last ten years to see what real-world problems were not solved well by the language, where there were rough edges, and so on. The increasing need to interoperate with both modern dynamic languages and legacy object models motivated the design of new language features like the dynamic type in C# 4.0. And they threw in typesafe covariance on certain generic interfaces for good measure.
Still the design team was not content; they were always looking at real-world developers to see what vexing problems they faced that could be solved with better language tools. They identified dealing with latency as a stumbling block for many modern application developers. In a world of multicore machines connected over networks, the milliseconds that elapse between requesting a result and processing it further ought not to be wasted, but it is very difficult to write clear, understandable programs that work well in high-latency environments. This motivated the addition of asynchronous methods in C# 5.0, which enable a program to do useful work while a method is waiting for a high-latency result to become available.
I figured it might be a good idea to do a quick look at the evolution of the C# language here, in the foreword, because this is certainly not the approach taken in this book. And that is a good thing! Authors of books for novices often choose to order the material in the order they learned it, which, as often as not, is the order in which the features were added to the language. What I particularly like about this book is that Scott chooses a sensible order to develop each concept, moving from the most basic arithmetical computations up to quite complex interrelated parts. Furthermore, his examples are actually realistic and motivating while still being clear enough and simple enough to be described in just a few paragraphs.
Ive concentrated here on the evolution of the language, but of course the evolution of one language is far from the whole story. The language is just the tool you use to access the power of the runtime and the framework libraries; they are large and complex topics in and of themselves. Another thing I like about this book is that it does not concentrate narrowly on the language, but rather builds upon the language concepts taught early on to explain how to make use of the power afforded by the most frequently used base class library types.