proc#2010 and the.net 4 platform - gbv · contents abouttheauthor xxv aboutthetechnicalreviewer...

18
Pro C# 2010 and the .NET 4 Platform Fifth Edition Andrew Troelsen Apress® TIB/UB Hannover 133 295 192 89

Upload: others

Post on 10-Aug-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ProC#2010 and the.NET 4 Platform - GBV · Contents AbouttheAuthor xxv AbouttheTechnicalReviewer xxvi Acknowledgments xxvii Introduction xxviii Part 1: Introducing C#andthe.NETPlatform

Pro C# 2010 and

the .NET 4 Platform

Fifth Edition

Andrew Troelsen

Apress®

TIB/UB Hannover133 295 192

89

Page 2: ProC#2010 and the.NET 4 Platform - GBV · Contents AbouttheAuthor xxv AbouttheTechnicalReviewer xxvi Acknowledgments xxvii Introduction xxviii Part 1: Introducing C#andthe.NETPlatform

Contents

About the Author xxv

About the Technical Reviewer xxvi

Acknowledgments xxvii

Introduction xxviii

Part 1: Introducing C# and the .NET Platform 1

Chapter 1: The Philosophy of .NET 3

Understanding the Previous State of Affairs 3

The .NET Solution 6

Introducing the Building Blocks of the .NET Platform (the CLR, CTS, and CLS) 7

Additional .NET-Aware Programming Languages 10

An Overview of .NET Assemblies 12

Understanding the Common Type System 19

Understanding the Common Language Specification 23

Understanding the Common Language Runtime 25

The Assembly/Namespace/Type Distinction 27

Exploring an Assembly Using ildasm.exe 33

Exploring an Assembly Using Reflector 35

Deploying the .NET Runtime 36

Page 3: ProC#2010 and the.NET 4 Platform - GBV · Contents AbouttheAuthor xxv AbouttheTechnicalReviewer xxvi Acknowledgments xxvii Introduction xxviii Part 1: Introducing C#andthe.NETPlatform

m CONTENTS

The Platform-Independent Nature of .NET 37

Summary 39

I Chapter 2: Building C# Applications 41

The Role of the .NET Framework 4.0 SDK 41

Building C# Applications Using csc.exe 42

Building .NET Applications Using Notepad++ 49

Building .NET Applications Using SharpDevelop 50

Building .NET Applications Using Visual C# 2010 Express 53

Building .NET Applications Using Visual Studio 2010 54

Summary 69

Part 2: Core C# Programming Constructs 71

tl Chapter 3: Core C# Programming Constructs, Part I .73

The Anatomy of a Simple C# Program 73

An Interesting Aside: Some Additional Members of the System.Environment Class 79

The System.Console Class 81

System Data Types and C# Shorthand Notation 86

Working with String Data 97

Narrowing and Widening Data Type Conversions 106

Understanding Implicitly Typed Local Variables 112

C# Iteration Constructs 117

Decision Constructs and the Relational/Equality Operators 119

Summary 122

Chapter 4: Core C# Programming Constructs, Part II 125

Methods and Parameter Modifiers 125

Understanding C# Arrays 137

Understanding the Enum Type 144

ix

Page 4: ProC#2010 and the.NET 4 Platform - GBV · Contents AbouttheAuthor xxv AbouttheTechnicalReviewer xxvi Acknowledgments xxvii Introduction xxviii Part 1: Introducing C#andthe.NETPlatform

IB CONTENTS

Understanding the Structure Type 151

Understanding Value Types and Reference Types 154

Understanding C# Nullable Types 162

Summary 165

if Chapter 5: Defining Encapsulated Glass Types 167

Introducing the C# Class Type 167

Understanding Constructors 170

The Role of the this Keyword 174

Understanding the static Keyword 181

Defining the Pillars of OOP 188

C# Access Modifiers 192

The First Pillar: C#'s Encapsulation Services 194

Understanding Automatic Properties 206

Understanding Object Initializer Syntax 210

Working with Constant Field Data 214

Understanding Partial Types 217

Summary 218

II Chapter 6: Understanding Inheritance and Polymorphism 219

The Basic Mechanics of Inheritance 219

Revising Visual Studio Class Diagrams 224

The Second Pillar of OOP: The Details of Inheritance 226

Programming for Containment/Delegation 232

The Third Pillar of OOP: C#'s Polymorphic Support 235

Understanding Base Class/Derived Class Casting Rules 247

The Master Parent Class: System.Object 250

Summary 258

X

Page 5: ProC#2010 and the.NET 4 Platform - GBV · Contents AbouttheAuthor xxv AbouttheTechnicalReviewer xxvi Acknowledgments xxvii Introduction xxviii Part 1: Introducing C#andthe.NETPlatform

CONTENTS

Chapter 7: Understanding Structured Exception Handling 259

Ode to Errors, Bugs, and Exceptions 259

The Role of .NET Exception Handling 260

The Simplest Possible Example 263

Configuring the State of an Exception 267

System-Level Exceptions (System.SystemException) ,272

Application-Level Exceptions (System.ApplicationException) 273

Processing Multiple Exceptions 277

Who Is Throwing What? 282

The Result of Unhandled Exceptions 283

Debugging Unhandled Exceptions Using Visual Studio 284

A Brief Word Regarding Corrupted State Exceptions (CSE) 285

Summary 287

Chapter 8: Understanding Object Lifetime 289

Classes, Objects, and References 289

The Basics of Object Lifetime -291

The Role of Application Roots 294

Understanding Object Generations 296

Concurrent Garbage Collection under .NET 1.0 - 3.5 297

Background Garbage Collection under .NET 4.0 297

The System.GC Type 298

Building Finalizable Objects 302

Building Disposable Objects 305

Building Finalizable and Disposable Types ....309

Understanding Lazy Object Instantiation 313

Summary 316

xi

Page 6: ProC#2010 and the.NET 4 Platform - GBV · Contents AbouttheAuthor xxv AbouttheTechnicalReviewer xxvi Acknowledgments xxvii Introduction xxviii Part 1: Introducing C#andthe.NETPlatform

CONTENTS

Part 3: Advanced C# Programming Constructs ........319

I Chapter 9: Working with interfaces 321

Understanding Interface Types 321

Defining Custom Interfaces 325

Implementing an Interface 327

Invoking Interface Members at the Object Level 329

Interfaces As Parameters 331

Interfaces As Return Values 333

Arrays of Interface Types 334

Implementing Interfaces Using Visual Studio 2010 335

Resolving Name Clashes via Explicit Interface Implementation 336

Designing Interface Hierarchies 339

Building Enumerable Types (Enumerable and Enumerator) 343

Building Cloneable Objects (ICIoneable) 349

Building Comparable Objects (IComparable) 354

Summary 359

B Chapter 10: Understanding Generics 361

The Issues with Non-Generic Collections 361

The Role of Generic Type Parameters 371

The System.Collections.Generic Namespace 376

Creating Custom Generic Methods 385

Creating Custom Generic Structures and Classes 388

Constraining Type Parameters 392

Summary 395

Chapter 11: Delegates, Events, and Lambdas.. 397

Understanding the .NET Delegate Type 397

Page 7: ProC#2010 and the.NET 4 Platform - GBV · Contents AbouttheAuthor xxv AbouttheTechnicalReviewer xxvi Acknowledgments xxvii Introduction xxviii Part 1: Introducing C#andthe.NETPlatform

CONTENTS

Defining a Delegate Type in C# 398

The System.MulticastDelegate and System.Delegate Base Classes 400

The Simplest Possible Delegate Example 402

Sending Object State Notifications using Delegates 405

Method Group Conversion Syntax 411

Understanding Delegate Covariance 413

Understanding Generic Delegates 415

Understanding C# Events 418

Understanding C# Anonymous Methods 427

Understanding Lambda Expressions 430

Summary 437

ti Chapter 12: Advanced C# Language Features 439

Understanding Indexer Methods 439

Understanding Operator Overloading 444

Understanding Custom Type Conversions 454

Understanding Extension Methods 460

Understanding Partial Methods 470

Understanding Anonymous Types 473

Working with Pointer Types 479

Summary 487

11 Chapter 13: LINQ to Objects... 489

UNO. Specific Programming Constructs 489

Understanding the Role of LINQ 493

Applying LINQ Queries to Primitive Arrays 496

Returning the Result of a LINQ Query 503

Applying LINQ Queries to Collection Objects 505

xiii

Page 8: ProC#2010 and the.NET 4 Platform - GBV · Contents AbouttheAuthor xxv AbouttheTechnicalReviewer xxvi Acknowledgments xxvii Introduction xxviii Part 1: Introducing C#andthe.NETPlatform

CONTENTS

Investigating the C# LINQ Query Operators 508

The Internal Representation of LINQ Query Statements 517

Summary 522

a Part 4: Programming with .NET Assemblies 523

Chapter 14: Configuring .NET Assemblies 525

Defining Custom Namespaces 525

The Role of .NET Assemblies 532

Understanding the Format of a .NET Assembly 533

Building and Consuming a Single-File Assembly 538

Building and Consuming a Multifile Assembly 550

Understanding Private Assemblies 553

Understanding Shared Assemblies 558

Consuming a Shared Assembly 567

Configuring Shared Assemblies 569

Understanding Publisher Policy Assemblies 574

Understanding the <codeBase> Element 576

The System.Configuration Namespace 577

Summary 579

Chapter 15: Type Reflection, Late Binding, and Attribute-Based Programming...581

The Necessity of Type Metadata 581

Understanding Reflection 586

Building a Custom Metadata Viewer 590

Dynamically Loading Assemblies 596

Reflecting on Shared Assemblies 598

Understanding Late Binding 600

Understanding the Role of .NET Attributes 604

xiv

Page 9: ProC#2010 and the.NET 4 Platform - GBV · Contents AbouttheAuthor xxv AbouttheTechnicalReviewer xxvi Acknowledgments xxvii Introduction xxviii Part 1: Introducing C#andthe.NETPlatform

CONTENTS

Building Custom Attributes 609

Assembly-Level (and Module-Level) Attributes 612

Reflecting on Attributes Using Early Binding 614

Reflecting on Attributes Using Late Binding 615

Putting Reflection, Late Binding, and Custom Attributes in Perspective 617

Building an Extendable Application 618

Summary 624

II Chapter 16: Processes, AppDomains, and Object Contexts 625

The Role of a Windows Process 625

Interacting with Processes Under the .NET Platform 627

Understanding .NET Application Domains 637

Interacting with the Default Application Domain .640

Creating New Application Domains 643

Understanding Object Context Boundaries 648

Summarizing Processes, AppDomains, and Context 652

Summary 652

Chapter 17: Understanding CIL and the Role of Dynamic Assemblies 653

Reasons for Learning the Grammar of CIL 653

Examining CIL Directives, Attributes, and Opcodes 654

Pushing and Popping: The Stack-Based Nature of CIL 656

Understanding Round-Trip Engineering 658

Understanding CIL Directives and Attributes 666

.NET Base Class Library, C#, and CIL Data Type Mappings 673

Defining Type Members in CIL 674

Examining CIL Opcodes 677

Page 10: ProC#2010 and the.NET 4 Platform - GBV · Contents AbouttheAuthor xxv AbouttheTechnicalReviewer xxvi Acknowledgments xxvii Introduction xxviii Part 1: Introducing C#andthe.NETPlatform

* CONTENTS

Building a .NET Assembly with CIL 683

Understanding Dynamic Assemblies 688

Summary 698

Chapter 18: Dynamic Types and the Dynamic Language Runtime 701

The Role of the C# dynamic Keyword 701

The Role of the Dynamic Language Runtime (DLR) 707

Simplifying Late Bound Calls Using Dynamic Types 710

Simplifying COM Interoperability using Dynamic Data 714

COM Interop using C# 4.0 Language Features 718

Summary 723

Part 5: Introducing the .NET Base Class Libraries 725

Chapter 19: Multithreaded and Parallel Programming 727

The Process/AppDomain/Context/Thread Relationship 727

A Brief Review of the .NET Delegate 729

The Asynchronous Nature of Delegates 731

Invoking a Method Asynchronously 733

The System/Threading Namespace 739

The System.Threading.Thread Class 741

Programmatically Creating Secondary Threads 744

The Issue of Concurrency 750

Programming with Timer Callbacks 758

Understanding the CLR ThreadPool 760

Parallel Programming under the .NET Platform 761

Parallel LINQ Queries (PLINQ) 771

Summary , 774

XVI

Page 11: ProC#2010 and the.NET 4 Platform - GBV · Contents AbouttheAuthor xxv AbouttheTechnicalReviewer xxvi Acknowledgments xxvii Introduction xxviii Part 1: Introducing C#andthe.NETPlatform

CONTENTS

Chapter 20: File I/O and Object Serialization 775

Exploring the System.10 Namespace 775

The Directory(lnfo) and File(lnfo) Types 777

Working with the Directorylnfo Type 778

Working with the Directory Type 782

Working with the Drivelnfo Class Type 783

Working with the Filelnfo Class 785

Working with the File Type 789

The Abstract Stream Class 792

Working with StreamWriters and StreamReaders 794

Working with StringWriters and StringReaders 798

Working with BinaryWriters and BinaryReaders 799

Watching Files Programmatically 801

Understanding Object Serialization 804

Configuring Objects for Serialization 806

Choosing a Serialization Formatter , 808

Serializing Objects Using the BinaryFormatter 811

Serializing Objects Using the SoapFormatter 813

Serializing Objects Using the XmlSerializer 814

Serializing Collections of Objects 816

Customizing the Soap/Binary Serialization Process 818

Summary 824

Chapter 21: ADO.NET Part I: The Connected Layer 825

A High-Level Definition of AD0.NET 825

Understanding AD0.NET Data Providers 827

Additional AD0.NET Namespaces 831

xvii

Page 12: ProC#2010 and the.NET 4 Platform - GBV · Contents AbouttheAuthor xxv AbouttheTechnicalReviewer xxvi Acknowledgments xxvii Introduction xxviii Part 1: Introducing C#andthe.NETPlatform

CONTENTS

The Types of the System.Data Namespace 832

Abstracting Data Providers Using Interfaces 837

Creating the AutoLot Database 840

The ADO.NET Data Provider Factory Model 847

Understanding the Connected Layer of AD0.NET 853

Working with Data Readers 859

Building a Reusable Data Access Library 861

Creating a Console Ul-Based Front End 871

Understanding Database Transactions 877

Summary 883

a? Chapter 22: ADO.NET Part II: The Disconnected Layer 885

Understanding the Disconnected Layer of AD0.NET 885

Understanding the Role of the DataSet 886

Working with DataColumns 889

Working with DataRows 893

Working with DataTables 897

Binding DataTable Objects to Windows Forms GUIs 903

Working with Data Adapters 913

Adding Disconnection Functionality to AutoLotDAL.dll 917

Multitabled DataSet Objects and Data Relationships 921

The Windows Forms Database Designer Tools 927

Isolating Strongly Typed Database Code into a Class Library 938

Programming with LINO to DataSet 943

Summary 949

Chapter 23: ADO.NET Part III: The Entity Framework 951

Understanding the Role of Entity Framework 951

xviii

Page 13: ProC#2010 and the.NET 4 Platform - GBV · Contents AbouttheAuthor xxv AbouttheTechnicalReviewer xxvi Acknowledgments xxvii Introduction xxviii Part 1: Introducing C#andthe.NETPlatform

CONTENTS

Building and Analyzing your First EDM 961

Programming Against the Conceptual Model 973

AutoLotDAL Version 4.0, Now with Entities 979

Data Binding Entities to Windows Forms GUIs 986

Summary 991

Chapter 24: Introducing UNO to XML 993

A Tale of Two XML APIs 993

Members of the System.Xml.Linq Namespace 997

Working with XEIement and XDocument 1002

Manipulating an in Memory XML Document 1006

Summary 1011

Chapter 25: Introducing Windows Communication Foundation ...1013

A Potpourri of Distributed Computing APIs 1013

The Role ofWCF 1019

Investigating the Core WCF Assemblies 1022

The Visual Studio WCF Project Templates 1023

The Basic Composition of a WCF Application 1025

The ABCs of WCF 1027

Building a WCF Service 1032

Hosting the WCF Service 1036

Building the WCF Client Application 1046

Simplifying Configuration Settings with WCF 4.0 1050

Using the WCF Service Library Project Template 1057

Hosting the WCF Service within a Windows Service 1061

Invoking a Service Asynchronously from the Client 1067

XIX

Page 14: ProC#2010 and the.NET 4 Platform - GBV · Contents AbouttheAuthor xxv AbouttheTechnicalReviewer xxvi Acknowledgments xxvii Introduction xxviii Part 1: Introducing C#andthe.NETPlatform

s* CONTENTS

Designing WCF Data Contracts 1070

Summary 1076

Chapter 26: introducing Windows Workflow Foundation 4.0 1077

Defining a Business Process 1078

Building a (Painfully) Simple Workflow 1079

The WF 4.0 Runtime 1083

Examining the Workflow 4.0 Activities 1088

Building a Flowchart Workflow 1093

Isolating Workflows into Dedicated Libraries 1103

Consuming the Workflow Library 1112

Summary 1114

§ Part 6: Building Desktop User Interfaces with WPF 1115

Chapter 27: Introducing Windows Presentation Foundation and XAML 1117

The Motivation Behind WPF 1117

The Various Flavors of WPF 1121

Investigating the WPF Assemblies 1126

Building a WPF Application without XAML 1135

Building a WPF Application using Only XAML 1144

Transforming Markup into a .NET Assembly 1150

Understanding The Syntax of WPF XAML 1154

Building a WPF Application using Code-Behind Files 1165

Building WPF Applications Using Visual Studio 2010 1167

Summary 1178

Chapter 28: Programming with WPF Controls 1179

A Survey of the Core WPF Controls 1179

Controlling Content Layout Using Panels 1184

XX

Page 15: ProC#2010 and the.NET 4 Platform - GBV · Contents AbouttheAuthor xxv AbouttheTechnicalReviewer xxvi Acknowledgments xxvii Introduction xxviii Part 1: Introducing C#andthe.NETPlatform

I CONTENTS

Building a Window's Frame Using Nested Panels 1195

Understanding WPF Control Commands,

1200

Building a WPF User Interface with Expression Blend 1207

Building the Ink API Tab 1216

Introducing the Documents API 1227

Building the Documents Tab 1228

Introducing the WPF Data-Binding Model 1235

Summary 1244

11 Chapter 29: WPF Graphics Rendering Services 1245

Understanding WPF's Graphical Rendering Services 1245

Rendering Graphical Data Using Shapes 1247

WPF Brushes and Pens 1257

Applying Graphical Transformations 1262

Working with Shapes using Expression Blend 1266

Rendering Graphical Data Using Drawings and Geometries 1271

Generating Complex Vector Graphics using Expression Design 1275

Rendering Graphical Data Using the Visual Layer 1277

Summary 1284

I Chapter 30: WPF Resources, Animations, and Styles 1285

Understanding the WPF Resource System 1285

Working with Object (Logical) Resources 1292

Understanding WPF's Animation Services 1303

Authoring Animations in XAML 1309

Understanding the Role of WPF Styles 1312

Generating Styles with Expression Blend 1320

Summary 1324

xxi

Page 16: ProC#2010 and the.NET 4 Platform - GBV · Contents AbouttheAuthor xxv AbouttheTechnicalReviewer xxvi Acknowledgments xxvii Introduction xxviii Part 1: Introducing C#andthe.NETPlatform

II CONTENTS

it Chapter 31: WPF Control Templates and UserControls 1325

Understanding the Role of Dependency Properties 1325

Building a Custom Dependency Property 1331

Understanding Routed Events 1337

Logical Trees, Visual Trees and Default Templates 1341

Building a Custom Control Template with Visual Studio 2010 1348

Building Custom UserControls with Blend 1356

Creating the Jackpot Deluxe WPF Application 1364

Summary 1375

Part 7: Building Web Applications with ASP.NET 1377

i Chapter 32: Building ASP.NET Web Pages 1379

The Role of HTTP 1379

Understanding Web Applications and Web Servers 1380

The Role of HTML 1382

The Role of Client-Side Scripting 1388

Posting Back to the Web Server 1390

The Feature Set of the ASP.NET API 1391

Building a Single File ASP.NET Web Page .....1395

Building an ASP.NET Web Page using Code Files 1406

ASP.NET Web Sites and ASP.NET Web Applications 1412

The ASP.NET Web Site Directory Structure 1413

The Inheritance Chain of the Page Type 1416

Interacting with the Incoming HTTP Request 1417

Interacting with the Outgoing HTTP Response 1421

The Life Cycle of an ASP.NET Web Page 1423

xxii

Page 17: ProC#2010 and the.NET 4 Platform - GBV · Contents AbouttheAuthor xxv AbouttheTechnicalReviewer xxvi Acknowledgments xxvii Introduction xxviii Part 1: Introducing C#andthe.NETPlatform

CONTENTS

The Role of the Web.config File 1427

Summary 1428

Chapter 33: ASP.NET Web Controls, Master Pages and Themes 1429

Understanding the Nature of Web Controls 1429

The Control and WebControl Base Classes 1432

Major Categories of ASP.NET Web Controls 1438

Building the ASP.NET Cars Web Site 1441

The Role of the Validation Controls 1457

Working with Themes 1465

Summary 1471

Chapter 34: ASP.NET State Management Techniques 1473

The Issue of State 1473

ASP.NET State Management Techniques 1476

Understanding the Role of ASP.NET View State 1476

The Role of the Global.asax File 1479

Understanding the Application/Session Distinction 1483

Working with the Application Cache 1488

Maintaining Session Data 1493

Understanding Cookies 1497

The Role of the <sessionState> Element 1500

Understanding the ASP.NET Profile API 1502

Summary 1509

Appendix A: Programming with Windows Forms 1511

The Windows Forms Namespaces 1512

Building a Simple Windows Forms Application 1513

The Visual Studio Windows Forms Project Template 1518

xxiii

Page 18: ProC#2010 and the.NET 4 Platform - GBV · Contents AbouttheAuthor xxv AbouttheTechnicalReviewer xxvi Acknowledgments xxvii Introduction xxviii Part 1: Introducing C#andthe.NETPlatform

fc CONTENTS

The Anatomy of a Form 1525

Responding to Mouse and Keyboard Activity 1534

Designing Dialog Boxes 1538

Rendering Graphical Data Using GDI+ 1545

Building a Complete Windows Forms Application 1551

Summary 1560

m Appendix B: Platform-Independent .NET Development with Mono 1561

The Platform-Independent Nature of .NET 1561

Obtaining and Installing Mono 1565

The Mono Development Languages 1569

Microsoft-Compatible Mono Development Tools 1570

Building .NET Applications with Mono 1572

Who is Using Mono? 1581

Suggestions for Further Study 1581

Summary 1582

xxiv