Essential C# 5.0
Mark Michaelis
with Eric Lippert
Upper Saddle River, NJ Boston Indianapolis San Francisco
New York Toronto Montreal London Munich Paris Madrid
Capetown Sydney Tokyo Singapore Mexico City
The .NET logo is either a registered trademark or trademark of Microsoft Corporation in the United States and/or other countries and is used under license from Microsoft.
Microsoft, Windows, Visual Basic, Visual C#, and Visual C++ are either registered trademarks or trademarks of Microsoft Corporation in the U.S.A. and/or other countries/regions.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals.
The authors and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein.
The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales, which may include electronic versions and/or custom covers and content particular to your business, training goals, marketing focus, and branding interests. For more information, please contact:
U.S. Corporate and Government Sales
(800) 382-3419
For sales outside the United States, please contact:
International Sales
Visit us on the Web: informit.com/aw
Library of Congress Cataloging-in-Publication Data
Michaelis, Mark.
Essential C# 5.0 / Mark Michaelis with Eric Lippert.
pages cm
Includes index.
ISBN 0-321-87758-6 (pbk. : alk. paper)
1. C# (Computer program language) 2. Microsoft .NET Framework. I.
Lippert, Eric. II. Title.
QA76.73.C154M5238 2013
006.7882dc23 2012036148
Copyright 2013 Pearson Education, Inc.
All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. To obtain permission to use material from this work, please submit a written request to Pearson Education, Inc., Permissions Department, One Lake Street, Upper Saddle River, New Jersey 07458, or you may fax your request to (201) 236-3290.
ISBN-13: 978-0-321-87758-1
ISBN-10: 0-321-87758-6
Text printed in the United States on recycled paper at Edwards Brothers Malloy in Anne Arbor, Michigan.
First printing, November 2012
To my family: Elisabeth, Benjamin, Hanna, and Abigail,
You have sacrificed a husband and daddy for countless hours of writing, frequently at times when he was needed most.
Thanks!
Figures
Tables
Foreword
Welcome to one of the greatest collaborations you could dream of in the world of C# booksand probably far beyond! Mark Michaelis Essential C# series is already a classic, and teaming up with famous C# blogger Eric Lippert on the new edition is another masterstroke!
You may think of Eric as writing blogs and Mark as writing books, but that is not how I first got to know them.
In 2005 when LINQ (Language Integrated Query) was disclosed, I had only just joined Microsoft, and I got to tag along to the PDC conference for the big reveal. Despite my almost total lack of contribution to the technology, I thoroughly enjoyed the hype. The talks were overflowing, the printed leaflets were flying like hotcakes: It was a big day for C# and .NET, and I was having a great time.
It was pretty quiet in the hands-on labs area, though, where people could try out the technology preview themselves with nice scripted walkthroughs. Thats where I ran into Mark. Needless to say, he wasnt following the script. He was doing his own experiments, combing through the docs, talking to other folks, busily pulling together his own picture.
As a newcomer to the C# community, I think I may have met a lot of people for the first time at that conferencepeople that I have since formed great relationships with. But to be honest, I dont remember it. The only one I remember is Mark. Here is why: When I asked him if he was liking the new stuff, he didnt just join the rave. He was totally level-headed: I dont know yet. I havent made up my mind about it. He wanted to absorb and understand the full package, and until then he wasnt going to let anyone tell him what to think.
So instead of the quick sugar rush of affirmation I might have expected, I got to have a frank and wholesome conversation, the first of many over the years, about details, consequences, and concerns with this new technology. And so it remains: Mark is an incredibly valuable community member for us language designers to have, because he is super smart, insists on understanding everything to the core, and has phenomenal insight into how things affect real developers. But perhaps most of all because he is forthright and never afraid to speak his mind. If something passes the Mark Test then we know we can start feeling pretty good about it!
These are the same qualities that make Mark such a great writer. He goes right to the essence and communicates with great integrity, no sugarcoating, and a keen eye for practical value and real-world problems.
Eric is, of course, my colleague of seven years on the C# team. Hes been here much longer than I have, and the first I recall of him, he was explaining to the team how to untangle a bowl of spaghetti. More precisely, our C# compiler code base at the time was in need of some serious architectural TLC, and was exceedingly hard to add new features tosomething we desperately needed to be able to do with LINQ. Eric had been investigating what kind of architecture we ought to have (Phases! We didnt even really have those!), and more importantly, how to get from here to there, step by step. The remarkable thing was that as complex as this was, and as new as I was to the team and the code base, I immediately understood what he was saying!
You may recognize from his blogs the super-clear and well-structured untangling of the problem, the convincing clarity of enumerated solutions, and the occasional unmitigated hilarity. Well, you dont know the half of it! Every time Eric is grappling with a complex issue and is sharing his thoughts about it with the team, his emails about it are just as meticulous and every bit as hilarious. You fundamentally cant ignore an issue raised by Eric because you cant wait to read his prose about it. Theyre even purple, too! So I essentially get to enjoy a continuous supply of what amounts to unpublished installments of his blog, as well as, of course, his pleasant and insightful presence as a member of the C# compiler team and language design team.
In summary, I am truly grateful to get to work with these two amazing people on a regular basis: Eric to help keep my thinking straight and Mark to help keep me honest. They share a great gift of providing clarity and elucidation, and by combining their inside and outside perspective on C#, this book reaches a new level of completeness. No one will help you get C# 5.0 like these two gentlemen do.