CYAN YELLOW MAGENTA BLACK PANTONE 123 C BOOKS FOR PROFESSIONALS BY PROFESSIONALS® THE EXPERT’S VOICE® IN .NET Companion eBook Available Introducing Visual C# 2010 I n Dear Reader, t If you are new to C#, this book is written for you! I guide you through the C# lan- r guage and the .NET Framework, carefully building your knowledge from the fun- o damental concepts to advanced features. Learning a new language can be a daunting task, especially learning a language d Introducing as rich and expressive as C#. Introducing Visual C# 2010 is a comprehensive primer. You can have confidence in the fact that you'll be able to build well-constructed u Adam Freeman , Author of web and Windows applications of your own by the end of this book. Pro ASP.NET 4 in C# 2010 I build your knowledge gradually, using the concepts from earlier chapters as a c Pro .NET 4 Parallel foundation to learn new and more sophisticated techniques. I start with the funda- i Programming in C# mental knowledge that any C# programmer needs to know and show you how to Visual C# 2010 n Pro LINQ: Language apply that knowledge to unlock more advanced features. Integrated Query in C# g 2010 In this book, you’ll learn how to: Visual C# 2010 Recipes • Understand the fundamentals of the C# language’s syntax and grammar and use V it to create Web and Windows applications. Programming .NET Security • Use the C# object oriented programming features to save you time and increase Microsoft .NET XML Web i your program’s flexibility. s Services Step by Step • Connect to a wide range of data sources and incorporate their information into C# for Java Developers u your programs. Programming the Internet • Use the key time and effort saving features of C# such as generics, lambda a with Java expressions, and delegates. Active Java • Use the huge array of supporting technologies that .NET 4 provides such as the l Windows Presentation Foundation, Windows Forms, Windows Communication Foundation, ASP.NET and LINQ. C # Get started on your C# journey with an expert This book shows you how to get things done – I focus on the practice, rather than the theory and show you how to use each major language and library feature. by your side leading by example 2 Companion eBook Adam Freeman 0 THE APRESS ROADMAP 1 Introducing Pro C# 2010 See last page for details Visual C# 2010 and the 0 on $10 eBook version .NET 4 Platform Beginning Pro ASP.NET ASP.NET 4.0 4.0 in C# 2010 in C# 2010 F SOURCE CODE ONLINE re Adam Freeman e www.apress.com ISBN 978-1-4302-3171-4 m 53999 a n US $39.99 Shelve in: Programming Languages / C# User level: Beginning 9 781430 231714 this print for content only—size & color not accurate 7.5 x 9.25 spine = 1.90625" 1320 page count (PPI = 692) Introducing Visual C# 2010 Adam Freeman Introducing Visual C# 2010 Copyright © 2010 by Adam Freeman All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-3171-4 ISBN-13 (electronic): 978-1-4302-3172-1 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. President and Publisher: Paul Manning Lead Editor: Ewan Buckingham Technical Reviewer: Damien Foggon Editorial Board: Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Anne Collett and Kelly Moritz Copy Editor: Kim Wimpsett and Mary Behr Compositor: Bytheway Publishing Services Indexer: BIM Indexing & Proofreading Services Artist: April Milne Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer Science+Business Media, LLC., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders- [email protected], or visit www.springeronline.com. For information on translations, please e-mail [email protected], or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at www.apress.com. Dedicated to my wife, Jacqui Griffyth. CONTENTS AT A GLANCE Contents at a Glance About the Author...............................................................................................xxxii About the Technical Reviewer.........................................................................xxxiii Acknowledgments...........................................................................................xxxiv PART 1 Getting Started..................................................................................1 Chapter 1: Introduction...........................................................................................3 Chapter 2: Getting Ready........................................................................................7 Chapter 3: Understanding C# and the .NET Framework........................................17 PART 2 The C# Language.............................................................................31 Chapter 4: C# Fundamentals and Keyword Reference..........................................33 Chapter 5: Numeric and Boolean Types................................................................91 Chapter 6: Classes and Objects..........................................................................131 Chapter 7: Fields.................................................................................................183 Chapter 8: Properties, Indexers, and Operators.................................................203 Chapter 9: Methods.............................................................................................229 Chapter 10: Delegates, Events, and Anonymous Methods..................................305 Chapter 11: Namespaces....................................................................................337 Chapter 12: Interfaces, Structs, and Enums.......................................................355 Chapter 13: Arrays..............................................................................................385 Chapter 14: Exceptions.......................................................................................419 Chapter 15: Generic and Anonymous Types.......................................................453 Chapter 16: Strings and Characters ...................................................................481 iv CONTENTS AT A GLANCE Chapter 17: Attributes........................................................................................519 Chapter 18: Garbage Collection..........................................................................539 PART 3 The .NET Class Library..................................................................549 Chapter 19: Collections.......................................................................................551 Chapter 20: Files, Streams, and IO.....................................................................621 Chapter 21: Networking & WCF..........................................................................675 Chapter 22: Time & Dates...................................................................................711 Chapter 23: Serialization....................................................................................735 Chapter 24: Serialization....................................................................................735 Chapter 25: Asynchronous Methods and Parallel Loops....................................815 Chapter 26: Other Useful Features and Classes..................................................833 PART 4 Data and Databases......................................................................861 Chapter 27: LINQ to Objects................................................................................863 Chapter 28: Parallel LINQ....................................................................................915 Chapter 29: LINQ to XML.....................................................................................925 Chapter 30: LINQ to Entities................................................................................965 Chapter 31: LINQ to DataSet.............................................................................1001 PART 5 User Interfaces............................................................................1031 Chapter 32: Windows Forms.............................................................................1033 Chapter 33: Windows Presentation Foundation................................................1069 Chapter 34: ASP.NET.........................................................................................1099 Chapter 35: Silverlight......................................................................................1125 PART 6 Advanced Topics.........................................................................1159 Chapter 36: Windows Integration.....................................................................1161 Chapter 37: Cryptography and Security............................................................1185 Chapter 38: Testing and Debugging..................................................................1201 Index.................................................................................................................1231 v CONTENTS Contents About the Authors.............................................................................................xxxii About the Technical Reviewers.......................................................................xxxiii Acknowledgments...........................................................................................xxxiv PART 1 Getting Started..................................................................................1 Chapter 1: Introduction...........................................................................................3 Who Should Read This Book?............................................................................................3 What Is Covered in This Book?..........................................................................................3 What Do You Need to Read This Book?.............................................................................4 What Is the Structure of This Book?..................................................................................4 What Is in Each Chapter?...................................................................................................4 Getting the Example Code.................................................................................................5 Finding More Information..................................................................................................6 The MSDN C# Programming Guide...........................................................................................................6 Online Forums..........................................................................................................................................6 Other Books..............................................................................................................................................6 Summary...........................................................................................................................6 Chapter 2: Getting Ready........................................................................................7 Installing the Software.......................................................................................................7 Installing Visual Studio 2010....................................................................................................................7 Installing SQL Server 2008.......................................................................................................................9 Installing the Silverlight Tools................................................................................................................10 Downloading the Sample Code and Data...............................................................................................10 vi CONTENTS Creating Your First C# Project..........................................................................................10 Creating the Project................................................................................................................................11 Editing the Code.....................................................................................................................................13 Compiling and Running the Program......................................................................................................14 Features That I Use Before Explaining Them...................................................................14 The using statements.............................................................................................................................14 The namespace statement.....................................................................................................................15 The class statement...............................................................................................................................15 The Main method....................................................................................................................................15 The Code Comments...............................................................................................................................16 The Console.WriteLine and Console.ReadLine methods.........................................................................16 Summary.........................................................................................................................16 Chapter 3: Understanding C# and the .NET Framework........................................17 C# and the .NET Framework at a Glance.........................................................................17 Introducing C#........................................................................................................................................17 Introducing the .NET Framework............................................................................................................18 The Relationship between C# and the .NET Framework........................................................................19 Understanding Assemblies.....................................................................................................................20 The Key Features of C# and the .NET Framework...........................................................22 Understanding Object-Orientation..........................................................................................................22 Understanding Type Safety....................................................................................................................22 Understanding Automatic Memory Management...................................................................................23 Understanding the Just-In-Time Compiler.............................................................................................23 Understanding Parallel Processing.........................................................................................................23 Understanding Unmanaged/Native Code Support..................................................................................24 Understanding the .NET Technology Spectrum......................................................................................24 Understanding .NET Cross-Platform Support..................................................................29 Summary.........................................................................................................................30 vii CONTENTS PART 2 The C# Language.............................................................................31 Chapter 4: C# Fundamentals and Keyword Reference..........................................33 Language Fundamentals.................................................................................................33 A Simple C# Program..............................................................................................................................33 C# Syntax................................................................................................................................................34 Types......................................................................................................................................................41 Common Programming Tasks.........................................................................................46 Assigning Values....................................................................................................................................46 Making Comparisons..............................................................................................................................47 Performing Selections............................................................................................................................49 Iterating Data Items................................................................................................................................53 Keyword and Operator Reference....................................................................................60 Type Keywords.......................................................................................................................................61 Modifiers.................................................................................................................................................69 Selection Keywords................................................................................................................................75 Iterator Keywords...................................................................................................................................75 Jump Keywords......................................................................................................................................76 Exception Handling Keywords................................................................................................................77 Arithmetic Overflow Keywords...............................................................................................................78 Synchronization Keywords.....................................................................................................................79 Parameter Keywords..............................................................................................................................79 Namespace Keywords............................................................................................................................81 Literal Keywords.....................................................................................................................................82 Object Keywords.....................................................................................................................................83 LINQ Keywords.......................................................................................................................................84 Other Keywords......................................................................................................................................84 Operators................................................................................................................................................87 Summary.........................................................................................................................89 viii
Description: