ebook img

C# in Depth PDF

528 Pages·2018·4.973 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview C# in Depth

IN DEPTH FOURTH EDITION Jon Skeet F E L OREWORD BY RIC IPPERT M A N N I N G Praise for the Third Edition “A must-have book that every .NET developer should read at least once.” —Dror Helper, Software Architect, Better Place “C# in Depth is the best source for learning C# language features.” —Andy Kirsch, Software Architect, Venga “Took my C# knowledge to the next level.” —Dustin Laine, Owner, Code Harvest “This book was quite an eye-opener to an interesting programming language that I have been unjustly ignoring until now.” —Ivan Todorovic´, Senior Software Developer AudatexGmbH, Switzerland “Easily the best C# reference I’ve found.” —Jon Parish, Software Engineer, Datasift “Highly recommend this book to C# developers who want to take their knowledge to pro status.” —D. Jay, Amazon reviewer Praise for the Second Edition “If you are looking to master C# then this book is a must-read.” —Tyson S. Maxwell, Sr. Software Engineer, Raytheon “We’re betting that this will be the best C# 4.0 book out there.” —Nikander Bruggeman and Margriet Bruggeman .NET consultants, Lois & Clark IT Services “A useful and engaging insight into the evolution of C# 4.” —Joe Albahari, Author of LINQPad and C# 4.0 in a Nutshell “This book should be required reading for all professional C# developers.” —Stuart Caborn, Senior Developer, BNP Paribas ii “A highly focused, master-level resource on language updates across all major C# releases. This book is a must-have for the expert developer wanting to stay current with new features of the C# language.” —Sean Reilly, Programmer/Analyst Point2 Technologies “Why read the basics over and over again? Jon focuses on the chewy, new stuff!” —Keith Hill, Software Architect, Agilent Technologies “Everything you didn’t realize you needed to know about C#.” —Jared Parsons, Senior Software Development Engineer, Microsoft Praise for the First Edition “Simply put, C# in Depth is perhaps the best computer book I’ve read.” —Craig Pelkie, Author, System iNetwork “I have been developing in C# from the very beginning and this book had some nice surprises even for me. I was especially impressed with the excellent coverage of delegates, anonymous methods, covariance and contravariance. Even if you are a seasoned developer, C# in Depth will teach you something new about the C# language.... This book truly has depth that no other C# language book can touch.” —Adam J. Wolf, Southeast Valley .NET User Group “This book wraps up the author’s great knowledge of the inner workings of C# and hands it over to readers in a well-written, concise, usable book.” —Jim Holmes, Author of Windows Developer Power Tools “Every term is used appropriately and in the right context, every example is spot-on and con- tains the least amount of code that shows the full extent of the feature...this is a rare treat.” —Franck Jeannin, Amazon UK reviewer “If you have developed using C# for several years now, and would like to know the internals, this book is absolutely right for you.” —Golo Roden Author, Speaker, and Trainer for .NET and related technologies “The best C# book I’ve ever read.” —Chris Mullins, C# MVP C# in Depth FOURTH EDITION JON SKEET FOREWORD BY ERIC LIPPERT MANNING SHELTER ISLAND For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 Email: [email protected] ©2019 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine. Manning Publications Co. Development editor: Richard Wattenberger 20 Baldwin Road Technical development editor: Dennis Sellinger PO Box 761 Review editor: Ivan Martinovic´ Shelter Island, NY 11964 Production editor: Lori Weidert Copy editor: Sharon Wilkey Technical proofreader: Eric Lippert Typesetter and cover designer: Marija Tudor ISBN 9781617294532 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – SP – 24 23 22 21 20 19 This book is dedicated to equality, which is significantly harder to achieve in the real world than overriding Equals() and GetHashCode(). vi contents foreword xvii preface xix acknowledgments xx about this book xxii about the author xxvi about the cover illustration xxvii PART 1 C# IN CONTEXT ............................................... 1 1 Survival of the sharpest 3 1.1 An evolving language 3 A helpful type system at large and small scales 4 ■ Ever more concise code 6 ■ Simple data access with LINQ 9 Asynchrony 10 ■ Balancing efficiency and complexity 11 Evolution at speed: Using minor versions 12 1.2 An evolving platform 13 1.3 An evolving community 14 1.4 An evolving book 15 Mixed-level coverage 16 ■ Examples using Noda Time 16 Terminology choices 17 vii viii CONTENTS PART 2 C# 2–5 .......................................................... 19 2 C# 2 21 2.1 Generics 22 Introduction by example: Collections before generics 22 Generics save the day 25 ■ What can be generic? 29 Type inference for type arguments to methods 30 ■ Type constraints 32 ■ The default and typeof operators 34 Generic type initialization and state 37 2.2 Nullable value types 38 Aim: Expressing an absence of information 39 ■ CLR and framework support: The Nullable<T> struct 40 ■ Language support 43 2.3 Simplified delegate creation 49 Method group conversions 50 ■ Anonymous methods 50 Delegate compatibility 52 2.4 Iterators 53 Introduction to iterators 54 ■ Lazy execution 55 ■ Evaluation of yield statements 56 ■ The importance of being lazy 57 Evaluation of finally blocks 58 ■ The importance of finally handling 61 ■ Implementation sketch 62 2.5 Minor features 66 Partial types 67 ■ Static classes 69 ■ Separate getter/setter access for properties 69 ■ Namespace aliases 70 Pragma directives 72 ■ Fixed-size buffers 73 InternalsVisibleTo 73 3 C# 3: LINQ and everything that comes with it 75 3.1 Automatically implemented properties 76 3.2 Implicit typing 77 Typing terminology 77 ■ Implicitly typed local variables (var) 78 ■ Implicitly typed arrays 79 3.3 Object and collection initializers 81 Introduction to object and collection initializers 81 Object initializers 83 ■ Collection initializers 84 The benefits of single expressions for initialization 86 3.4 Anonymous types 86 Syntax and basic behavior 86 ■ The compiler-generated type 89 ■ Limitations 90 CONTENTS ix 3.5 Lambda expressions 91 Lambda expression syntax 92 ■ Capturing variables 94 Expression trees 101 3.6 Extension methods 103 Declaring an extension method 103 ■ Invoking an extension method 104 ■ Chaining method calls 106 3.7 Query expressions 107 Query expressions translate from C# to C# 108 ■ Range variables and transparent identifiers 108 ■ Deciding when to use which syntax for LINQ 110 3.8 The end result: LINQ 111 4 C# 4: Improving interoperability 113 4.1 Dynamic typing 114 Introduction to dynamic typing 114 ■ Dynamic behavior beyond reflection 119 ■ A brief look behind the scenes 124 Limitations and surprises in dynamic typing 127 ■ Usage suggestions 131 4.2 Optional parameters and named arguments 133 Parameters with default values and arguments with names 134 Determining the meaning of a method call 135 ■ Impact on versioning 137 4.3 COM interoperability improvements 138 Linking primary interop assemblies 139 ■ Optional parameters in COM 140 ■ Named indexers 142 4.4 Generic variance 143 Simple examples of variance in action 143 ■ Syntax for variance in interface and delegate declarations 144 Restrictions on using variance 145 ■ Generic variance in practice 147 5 Writing asynchronous code 150 5.1 Introducing asynchronous functions 152 First encounters of the asynchronous kind 152 ■ Breaking down the first example 154 5.2 Thinking about asynchrony 155 Fundamentals of asynchronous execution 155 ■ Synchronization contexts 157 ■ Modeling asynchronous methods 158

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.