Вы находитесь на странице: 1из 30

Table of Contents

Introduction ........................................................................................................................... xxxv

Part I: .NET Framework 4.5 and Visual Studio 2012


Chapter 1: C#.NET Essentials..................................................................................................... 1
Section I Introduction to .NET ....................................................................................................................... 2
Evolution of .NET ................................................................................................................................................ 2
Benefits of .NET Framework............................................................................................................................... 2
Overview of .NET Framework 4.5 ...................................................................................................................... 3
CLR................................................................................................................................................................. 4
CTS ................................................................................................................................................................. 5
Metadata and Assemblies ............................................................................................................................. 6
.NET Framework Class Library .................................................................................................................... 8
BCL ................................................................................................................................................................. 9
Windows Forms........................................................................................................................................... 11
ASP.NET and ASP.NET AJAX.................................................................................................................... 11
ADO.NET..................................................................................................................................................... 12
WF ................................................................................................................................................................ 13
WPF .............................................................................................................................................................. 13
WCF.............................................................................................................................................................. 14
Windows CardSpace ................................................................................................................................... 16
LINQ ............................................................................................................................................................ 17
Tools in the .NET Framework 4.5 ..................................................................................................................... 17
New Features of .NET Framework 4.5 ............................................................................................................. 20
.NET for Windows Store Apps ................................................................................................................... 20
Portable Class Libraries............................................................................................................................... 20
Core New Features and Improvements ..................................................................................................... 21
MEF .............................................................................................................................................................. 21
Asynchronous File Operations ................................................................................................................... 21
Tools ............................................................................................................................................................. 22
Parallel Computing ..................................................................................................................................... 22
Improvements in Web Application Development ..................................................................................... 22
Networking .................................................................................................................................................. 22

Table of Contents

Improvements in WPF ................................................................................................................................ 22


Improvements in WCF ................................................................................................................................ 23
Improvements in WF................................................................................................................................... 23
Section II Introduction to Visual Studio 2012 IDE ..................................................................................... 24
New Features of Visual Studio 2012 ................................................................................................................. 25
Project Compatibility................................................................................................................................... 25
Configuration Changes in ASP.NET Website Templates .......................................................................... 26
Native Support in IIS 7.5 for ASP.NET Routing ........................................................................................ 26
HTML Editor ............................................................................................................................................... 26
JavaScript Editor .......................................................................................................................................... 31
CSS Editor .................................................................................................................................................... 31
Page Inspector.............................................................................................................................................. 35
Publishing .................................................................................................................................................... 36
Support for Developing Windows Store Apps .......................................................................................... 36
Enhanced Cloud Capability ........................................................................................................................ 37
New Tabs in Start Page ............................................................................................................................... 37
ALM and TFS............................................................................................................................................... 37
Managing the Application Lifecycle........................................................................................................... 37
Modeling Applications................................................................................................................................ 37
Developing Applications and Building Communication in a Team......................................................... 37
Automating and Debugging Builds............................................................................................................ 38
Microsoft Test Manager .............................................................................................................................. 38
Some Miscellaneous Features of Visual Studio 2012 ................................................................................. 38
Editions of Visual Studio 2012 .......................................................................................................................... 39
Installing Visual Studio 2012............................................................................................................................. 40
Exploring the Visual Studio 2012 IDE .............................................................................................................. 44
Start Page ..................................................................................................................................................... 45
Menu Bar...................................................................................................................................................... 45
Toolbar ......................................................................................................................................................... 45
Toolbox......................................................................................................................................................... 46
Code Editor .................................................................................................................................................. 46
IntelliSense ................................................................................................................................................... 47
Designers...................................................................................................................................................... 49
Solution Explorer ......................................................................................................................................... 51
Properties Window...................................................................................................................................... 52
Object Browser............................................................................................................................................. 53
Class View.................................................................................................................................................... 54
Server Explorer ............................................................................................................................................ 55
Component Tray.......................................................................................................................................... 56
Output Window .......................................................................................................................................... 56
Task List ....................................................................................................................................................... 57

vi

Table of Contents

Error List ...................................................................................................................................................... 57


Command Window ..................................................................................................................................... 58
Dynamic Help Window .............................................................................................................................. 58
Manage Styles Window............................................................................................................................... 59
Basic IDE Operations......................................................................................................................................... 60
Creating a New Windows Application ...................................................................................................... 60
Saving Code on the Toolbox ....................................................................................................................... 61
Docking the Windows ................................................................................................................................. 63
Switching between the Windows ............................................................................................................... 64
Specifying the .NET Framework Version for Projects ............................................................................... 66
Exporting and Importing IDE Settings ....................................................................................................... 66
Changing the IDE Font and Color .............................................................................................................. 72
Building and Debugging the Projects ......................................................................................................... 74
Section III C# 5.0 Overview .......................................................................................................................... 77
New Features of C# 5.0 ..................................................................................................................................... 77
Async and Await ......................................................................................................................................... 77
Caller Information ....................................................................................................................................... 78
Lambda Expressions.................................................................................................................................... 78
LINQ Expressions........................................................................................................................................ 78
Named Arguments ...................................................................................................................................... 78
Overload Resolution.................................................................................................................................... 78
Creating a Simple C# 5.0 Console Application ................................................................................................ 78
Section IV Snapshot of .NET Related Technologies .................................................................................. 80
WPF .................................................................................................................................................................... 80
LINQ .................................................................................................................................................................. 81
ADO.NET Entity Framework ........................................................................................................................... 81
ASP.NET AJAX.................................................................................................................................................. 81
Silverlight ........................................................................................................................................................... 81
ASP.NET Web Services ..................................................................................................................................... 81
WCF Services ..................................................................................................................................................... 81
Summary............................................................................................................................................................ 82

Part II: C# Programming Language


Chapter 2: C# Fundamentals .................................................................................................... 83
In Depth............................................................................................................................................................. 84
Identifiers and Keywords.................................................................................................................................. 84
Variables and Constants.................................................................................................................................... 85
Value Types ................................................................................................................................................. 87
Reference Types........................................................................................................................................... 89
Pointer Types ............................................................................................................................................... 91
Type Conversions ........................................................................................................................................ 92
Boxing and Unboxing.................................................................................................................................. 93

vii

Table of Contents

Expressions and Operators ............................................................................................................................... 94


Immediate Solutions ........................................................................................................................................ 98
Declaring Variables ........................................................................................................................................... 98
Initializing Variables ....................................................................................................................................... 100
Declaring Constants ........................................................................................................................................ 101
Declaring Nullable Type Variables................................................................................................................. 101
Using the ?? (Null Coalescing) Operator........................................................................................................ 102
Specifying the Scope of a Variable .................................................................................................................. 103
Using the Scope Resolution Operator (::) ....................................................................................................... 104
Declaring and Initializing Strings ................................................................................................................... 105
Manipulating Strings....................................................................................................................................... 105
Converting a Number Type into another Number Type .............................................................................. 107
Converting a String Type to a Number Type................................................................................................. 108
Converting Two Nullable Types .................................................................................................................... 109
Using the is and as Operators ......................................................................................................................... 110
Determining the Minimum and Maximum Values of Data Types ............................................................... 111
Creating an Enumeration ................................................................................................................................ 112
Declaring Single and Multi-dimensional Arrays ........................................................................................... 113
Iterating Through an Array............................................................................................................................. 115
Creating an Alias ............................................................................................................................................. 117
Summary.......................................................................................................................................................... 118

Chapter 3: Flow Control and Exception Handling in C# 5.0 Programs ................................. 119
In Depth........................................................................................................................................................... 120
Control Flow Statements ................................................................................................................................. 120
Selection Statements .................................................................................................................................. 120
Iteration Statements or Loops ................................................................................................................... 122
Jump Statements ........................................................................................................................................ 124
Exception Handling......................................................................................................................................... 126
The trycatchfinally Statement............................................................................................................ 127
The throw Statement ................................................................................................................................. 128
Checked and Unchecked Statements.............................................................................................................. 128
Immediate Solutions ...................................................................................................................................... 129
Using the if Statement ..................................................................................................................................... 129
Using Nested if Statements ............................................................................................................................. 129
Using the switch Statement............................................................................................................................. 130
Using Fall-through Switch Statement............................................................................................................. 132
Using the while Loop ...................................................................................................................................... 133
Using the dowhile Loop .............................................................................................................................. 134
Using the for Loop........................................................................................................................................... 135

viii

Table of Contents

Using the foreach Loop ................................................................................................................................... 137


Using Nested for Loops................................................................................................................................... 138
Creating an Infinite Loop ................................................................................................................................ 139
Using the goto Statement ................................................................................................................................ 140
Breaking a Loop............................................................................................................................................... 141
Using the continue Statement ......................................................................................................................... 143
Handling an Exception.................................................................................................................................... 143
Throwing an Exception ................................................................................................................................... 144
Using the checked and unchecked Statements .............................................................................................. 145
Summary.......................................................................................................................................................... 146

Chapter 4: Namespaces, Classes, Objects, and Structs ....................................................... 147


In Depth........................................................................................................................................................... 149
Exploring Namespaces.................................................................................................................................... 149
Exploring Classes and Objects ........................................................................................................................ 152
Syntax of a Class ........................................................................................................................................ 152
Methods as Class Members....................................................................................................................... 152
Access Modifiers........................................................................................................................................ 154
Objects ........................................................................................................................................................ 154
Constructors and Destructors ................................................................................................................... 155
Static Classes and Static Class Members .................................................................................................. 156
Properties ................................................................................................................................................... 157
Indexers...................................................................................................................................................... 159
Exploring Structs ............................................................................................................................................. 160
Syntax of a Struct ....................................................................................................................................... 160
Access Modifiers for Structs...................................................................................................................... 161
Immediate Solutions ...................................................................................................................................... 162
Creating a Namespace..................................................................................................................................... 162
Adding a Reference to the Namespace........................................................................................................... 163
Accessing a Predefined Namespace through the using Directive................................................................. 165
Creating a Class ............................................................................................................................................... 166
Declaring a Partial Class ................................................................................................................................. 167
Creating an Object ........................................................................................................................................... 167
Using this Keyword......................................................................................................................................... 169
Creating an Array of Objects........................................................................................................................... 170
Using a Method with Parameters ................................................................................................................... 172
Using a Method with Arguments ................................................................................................................... 173
Passing a Reference Argument to a Method .................................................................................................. 174
Passing an Object as an Argument to a Method ............................................................................................ 175
Returning a Value from a Method .................................................................................................................. 177
Creating a Partial Method ............................................................................................................................... 177

ix

Table of Contents

Using a Parameterized Constructor in a Class............................................................................................... 179


Creating a Copy Constructor .......................................................................................................................... 179
Calling the Destructor of a Class .................................................................................................................... 181
Creating a Static Class ..................................................................................................................................... 181
Using a Static Method in Classes .................................................................................................................... 182
Assigning and Retrieving Values Using a Property ...................................................................................... 183
Using Object Initializers .................................................................................................................................. 184
Retrieving Values using a Read-Only Property ............................................................................................. 185
Creating a Static Property ............................................................................................................................... 186
Creating an Anonymous Type........................................................................................................................ 187
Using an Indexer.............................................................................................................................................. 188
Using a String Indexer..................................................................................................................................... 189
Using Nested Classes ...................................................................................................................................... 190
Using a Struct................................................................................................................................................... 191
Summary.......................................................................................................................................................... 192

Chapter 5: Object-Oriented Programming in C# 5.0 .............................................................. 193


In Depth........................................................................................................................................................... 194
Encapsulation .................................................................................................................................................. 194
Inheritance ....................................................................................................................................................... 195
Inheritance and Constructors.................................................................................................................... 195
Sealed Classes and Sealed Methods ......................................................................................................... 196
Extension Methods .................................................................................................................................... 196
Polymorphism ................................................................................................................................................. 197
Compile Time Polymorphism/Overloading ........................................................................................... 197
Runtime Polymorphism/Overriding ....................................................................................................... 200
Abstraction....................................................................................................................................................... 200
Abstract Classes ......................................................................................................................................... 201
Abstract Methods ...................................................................................................................................... 201
Interfaces .......................................................................................................................................................... 202
Syntax of Interfaces ................................................................................................................................... 202
Implementation of Interfaces .................................................................................................................... 203
Interfaces and Inheritance ......................................................................................................................... 203
Immediate Solutions ...................................................................................................................................... 205
Inheriting a Class ............................................................................................................................................. 205
Creating a Sealed Class ................................................................................................................................... 206
Using an Extension Method ............................................................................................................................ 207
Implementing Encapsulation Using Accessors and Mutators ...................................................................... 208
Implementing Encapsulation Using Properties ............................................................................................. 209
Implementing Operator Overloading ............................................................................................................ 210
Overloading a Method .................................................................................................................................... 212

Table of Contents

Overloading an Indexer .................................................................................................................................. 213


Overloading the new Operator ....................................................................................................................... 214
Overriding a Base Class Method .................................................................................................................... 215
Implementing an Interface .............................................................................................................................. 216
Inheriting an Interface ..................................................................................................................................... 217
Summary.......................................................................................................................................................... 218

Chapter 6: Pointers, Delegates, and Events .......................................................................... 219


In Depth........................................................................................................................................................... 220
Pointers ............................................................................................................................................................ 220
Declaring Unsafe Code.............................................................................................................................. 220
Declaring Pointers ..................................................................................................................................... 221
Performing Arithmetic Operations on Pointers ....................................................................................... 221
Converting Pointers................................................................................................................................... 222
Delegates .......................................................................................................................................................... 223
Creating and Using Delegates .................................................................................................................. 223
Multicasting with Delegates ..................................................................................................................... 224
Exploring Covariance and Contravariance .............................................................................................. 224
Exploring Anonymous Methods with Delegates..................................................................................... 225
Events ............................................................................................................................................................... 226
Event Sources............................................................................................................................................. 226
Event Handlers .......................................................................................................................................... 227
Events and Delegates ................................................................................................................................ 227
Multiple Event Handlers........................................................................................................................... 229
Immediate Solutions ...................................................................................................................................... 230
Creating Pointer to int Data Type ................................................................................................................... 230
Dereferencing Pointers .................................................................................................................................... 231
Using Pointers to Arrays ................................................................................................................................. 232
Using Pointers to Structures ........................................................................................................................... 233
Using Pointers to a Pointer.............................................................................................................................. 234
Subtracting Pointers ........................................................................................................................................ 235
Converting a Pointer Type to another Pointer Type...................................................................................... 236
Using the fixed Statement ............................................................................................................................... 237
Creating a Delegate ......................................................................................................................................... 238
Declaring and Instantiating a Delegate .......................................................................................................... 240
Creating Multicast Delegates .......................................................................................................................... 242
Implementing Covariance............................................................................................................................... 244
Implementing Contravariance ........................................................................................................................ 245
Using Anonymous Methods to Instantiate Delegates ................................................................................... 246
Creating Events................................................................................................................................................ 247
Chaining Events............................................................................................................................................... 248

xi

Table of Contents

Working with an Event Source ....................................................................................................................... 249


Firing an Event ................................................................................................................................................ 252
Summary.......................................................................................................................................................... 254

Chapter 7: Dynamic Data Type and Dynamic Language Runtime (DLR) .............................. 255
In Depth........................................................................................................................................................... 256
Understanding the Dynamic Data Type ........................................................................................................ 256
Converting the Data Types ............................................................................................................................. 257
Exploring Dynamic Language Runtime ......................................................................................................... 258
Advantages of DLR ................................................................................................................................... 258
Architecture of DLR .................................................................................................................................. 259
Understanding the ExpandoObject and DynamicObject Classes ................................................................. 260
Immediate Solutions ...................................................................................................................................... 261
Using Dynamic Data Type .............................................................................................................................. 261
Converting Data Types ................................................................................................................................... 262
Using the ExpandoObject Class ...................................................................................................................... 263
Using the DynamicObject Class...................................................................................................................... 264
Summary.......................................................................................................................................................... 266

Part III: Windows Forms and Windows Presentation Foundation (WPF)


Chapter 8: Introducing Windows Presentation Foundation .................................................. 267
In Depth........................................................................................................................................................... 268
New Features of WPF 4.5 ................................................................................................................................ 268
The Ribbon Control ................................................................................................................................... 269
Support for Validating Data Synchronously and Asynchronously ........................................................ 269
Support for Binding to Types that Implement ICustomTypeProvider................................................... 269
Support for Fetching Binding Information............................................................................................... 269
Data Binding to Static Properties .............................................................................................................. 270
Support for Accessing Collections on a Non-UI Thread ......................................................................... 270
Support for Automatically Updating a Source of a Data Binding .......................................................... 270
New VirtualizingPanel Features............................................................................................................... 270
Support for Implementing Weak Reference to an Event ......................................................................... 270
New Methods Defined in the Dispatcher Class ....................................................................................... 271
Support for the Use of Markup Extensions for Events ............................................................................ 271
Support for Rearrangement of Data on Modification (Live Shaping)..................................................... 271
Improved Performance when Displaying Large Sets of Grouped Data ................................................. 271
Support for the Validation of DataContext Object................................................................................... 271
WPF 4.5 Architecture....................................................................................................................................... 271
PresentationFramework ............................................................................................................................ 272
PresentationCore ....................................................................................................................................... 272
WindowsBase ............................................................................................................................................ 272

xii

Table of Contents

MIL or Milcore ........................................................................................................................................... 272


WPF 4.5 Class Hierarchy ................................................................................................................................. 273
The System.Windows.Threading.DispatcherObject Class ...................................................................... 273
The System.Windows.DependencyObject Class...................................................................................... 274
The System.Windows.Freezable Class ..................................................................................................... 274
The System.Windows.Media.Visual Class ............................................................................................... 274
The System.Windows.UIElement Class ................................................................................................... 275
The System.Windows.FrameworkElement Class .................................................................................... 275
The System.Windows.Controls.Control Class ......................................................................................... 275
The System.Windows.Controls.Panel Class............................................................................................. 275
The System.Windows.Shapes.Shape Class .............................................................................................. 276
The System.Windows.ContentElement Class .......................................................................................... 276
WPF 4.5 Application Model ............................................................................................................................ 276
Types of WPF Applications....................................................................................................................... 276
The Application Class ............................................................................................................................... 283
Exploring the WPF 4.5 Designer Interface ..................................................................................................... 285
Design View ............................................................................................................................................... 286
XAML View ............................................................................................................................................... 289
Split View Bar ............................................................................................................................................ 289
Solution Explorer ....................................................................................................................................... 291
Properties Window.................................................................................................................................... 291
Toolbox....................................................................................................................................................... 292
Document Outline Window...................................................................................................................... 293
Using XAML in WPF 4.5 Applications ........................................................................................................... 294
XAML Elements......................................................................................................................................... 294
Namespaces and XAML............................................................................................................................ 295
XAML Property Syntax ............................................................................................................................. 296
Markup Extensions.................................................................................................................................... 297
WPF Properties ................................................................................................................................................ 298
Dependency Properties ............................................................................................................................. 298
Attached Properties ................................................................................................................................... 306
WPF Events ...................................................................................................................................................... 308
Routed Events ............................................................................................................................................ 308
Attached Events......................................................................................................................................... 312
Lifetime of Events ...................................................................................................................................... 313
Working with Dialog Boxes in WPF Applications......................................................................................... 315
The MessageBox Class............................................................................................................................... 315
The OpenFileDialog Class......................................................................................................................... 316
The SaveFileDialog Class .......................................................................................................................... 316
The PrintDialog Class................................................................................................................................ 316
Compiling and Running WPF 4.5 Applications............................................................................................. 317

xiii

Table of Contents

Immediate Solutions ...................................................................................................................................... 320


Working with WPF Windows......................................................................................................................... 320
Adding Rows and Columns to a Grid Through XAML ................................................................................ 324
Creating a Dialog Box in a WPF 4.5 Application ........................................................................................... 326
Using a Message Box ................................................................................................................................. 326
Using the Print Dialog Box........................................................................................................................ 327
Working with Custom Routed Events............................................................................................................ 330
Summary.......................................................................................................................................................... 332

Chapter 9: Working with WPF 4.5 Controls, Resources, Styles, Templates,


and Commands ..................................................................................................... 333
In Depth........................................................................................................................................................... 334
WPF 4.5 Controls ............................................................................................................................................. 334
Container Controls .................................................................................................................................... 334
Simple Controls ......................................................................................................................................... 342
Content Controls ....................................................................................................................................... 349
Headered Content Controls ...................................................................................................................... 360
ItemsControl Control ................................................................................................................................ 363
HeaderedItems Controls ........................................................................................................................... 370
Miscellaneous Controls ............................................................................................................................. 372
Resources ......................................................................................................................................................... 380
Static Resources ......................................................................................................................................... 380
Dynamic Resources ................................................................................................................................... 381
Styles and Templates ....................................................................................................................................... 381
Styles in WPF 4.5 ....................................................................................................................................... 381
Templates ................................................................................................................................................... 382
Triggers ............................................................................................................................................................ 383
Property Triggers....................................................................................................................................... 383
Event Triggers............................................................................................................................................ 384
WPF Commands.............................................................................................................................................. 384
Command Sources..................................................................................................................................... 384
Command Targets ..................................................................................................................................... 385
Command Binding .................................................................................................................................... 385
Interoperability between Windows Forms and a WPF 4.5 Application ....................................................... 385
Immediate Solutions ...................................................................................................................................... 386
Using WPF Controls ........................................................................................................................................ 386
Using the Calendar and DatePicker Controls .......................................................................................... 386
Adding Controls through the XAML Code ............................................................................................. 387
Adding Controls through the Code-Behind File...................................................................................... 388
Using Dynamic Layout of Controls .......................................................................................................... 389
Setting the Absolute Position of Controls ................................................................................................ 391

xiv

Table of Contents

Adding Borders to Controls ...................................................................................................................... 392


Using the PasswordBox Control ............................................................................................................... 393
Creating a Scrollable Area in a WPF Application .................................................................................... 393
Using the Slider Control............................................................................................................................ 394
Using the Viewbox Control....................................................................................................................... 395
Assigning the Access Key for Controls .................................................................................................... 395
Using the Expander Control ..................................................................................................................... 397
Using the NavigationWindow and Frames Controls .............................................................................. 398
Using the Resources, Styles, and Templates .................................................................................................. 399
Accessing Resources from Code ............................................................................................................... 399
Using Dynamic Resources ........................................................................................................................ 400
Setting Styles through Resources.............................................................................................................. 401
Extending an Existing Style ...................................................................................................................... 402
Using DataTemplate to Change the Appearance of the Content of Control .......................................... 403
Using ControlTemplate ............................................................................................................................. 404
Using the DataTemplate Template ........................................................................................................... 405
Handling Events in Styles Using the EventSetter Class .......................................................................... 406
Using Triggers ................................................................................................................................................. 408
Setting the Value of Properties using Triggers ........................................................................................ 408
Performing Actions using Triggers .......................................................................................................... 408
Using WPF Commands with Controls ........................................................................................................... 411
Hosting a Windows Forms Controls in a WPF Application ......................................................................... 411
Hosting a WPF Control in a Windows Forms Application ........................................................................... 412
Summary.......................................................................................................................................................... 414

Part IV: ASP.NET 4.5 and Silverlight 5.0


Chapter 10: Introducing ASP.NET 4.5 and Web Forms ......................................................... 415
In Depth........................................................................................................................................................... 416
New Features in ASP.NET 4.5 ........................................................................................................................ 416
ASP.NET Core Services ............................................................................................................................. 416
ASP.NET Web Forms ................................................................................................................................ 418
ASP.NET Web Pages 2 .............................................................................................................................. 419
Describing the ASP.NET Technologies .......................................................................................................... 419
MVC Framework ....................................................................................................................................... 419
ADO.NET Entity Framework ................................................................................................................... 419
ADO.NET Data Services Framework ....................................................................................................... 420
The Silverlight Technology ....................................................................................................................... 420
Dynamic Data Framework ........................................................................................................................ 420
ASP.NET Web API .................................................................................................................................... 420
Describing the ASP.NET Life Cycle................................................................................................................ 420
Life Cycle of an ASP.NET Application on IIS 7.5..................................................................................... 421

xv

Table of Contents

Life Cycle of an ASP.NET Web Page ........................................................................................................ 422


Specifying a Location for a Web Application................................................................................................. 424
File System ................................................................................................................................................. 424
HTTP .......................................................................................................................................................... 425
FTP ............................................................................................................................................................. 429
Structure of an Application ............................................................................................................................. 430
The Application Domain ........................................................................................................................... 430
The Application Lifetime .......................................................................................................................... 431
The Application Directory Structure ........................................................................................................ 431
File Types in ASP.NET Web Sites ................................................................................................................... 432
Compilation in ASP.NET 4.5........................................................................................................................... 434
Dynamic Compilation in ASP.NET 4.5........................................................................................................... 435
Compiling on First Request ...................................................................................................................... 435
Recompiling on Change ............................................................................................................................ 435
Compilation Dependencies ....................................................................................................................... 435
Compilation Output .................................................................................................................................. 435
Multiple Language Support ...................................................................................................................... 436
Optimizing Dynamic Compilation ........................................................................................................... 436
Disadvantages of Dynamic Compilation ................................................................................................. 436
ASP.NET 4.5 Coding Models .......................................................................................................................... 436
Single-File Page Model .............................................................................................................................. 437
Code-Behind Page Model.......................................................................................................................... 438
ASP.NET 4.5 Page Directives .......................................................................................................................... 439
The @ Page Directive ................................................................................................................................. 440
The @ Control Directive ............................................................................................................................ 440
The @ Import Directive ............................................................................................................................. 440
The @ Implements Directive ..................................................................................................................... 440
The @ Register Directive ........................................................................................................................... 440
The @ Assembly Directive......................................................................................................................... 440
The @ Master Directive ............................................................................................................................. 441
The @ PreviousPageType Directive .......................................................................................................... 441
The @ MasterType Directive ..................................................................................................................... 441
The @ OutputCache Directive................................................................................................................... 441
The @ Reference Directive......................................................................................................................... 441
The @WebHandler Directive .................................................................................................................... 441
Immediate Solutions ...................................................................................................................................... 442
Creating a New Web Site ................................................................................................................................ 442
Using File System based Location ............................................................................................................ 442
Using HTTP-based Location ..................................................................................................................... 444
Using FTP-Based Location ........................................................................................................................ 446
Setting the Title Bar Text of a Web Form........................................................................................................ 448

xvi

Table of Contents

Adding a Background Color to a Web Form ................................................................................................. 449


Adding a Background Image to a Web Form ................................................................................................ 450
Adding Controls to a Web Form..................................................................................................................... 450
Hiding and Displaying Controls .............................................................................................................. 451
Disabling and Enabling Controls.............................................................................................................. 452
Setting the Captions and Fonts of Controls.............................................................................................. 453
Setting the Tab Order of a Control............................................................................................................ 454
Adding Tool Tips to Controls ................................................................................................................... 455
Specifying Access Characters for Controls............................................................................................... 455
Running a Web Application............................................................................................................................ 455
Creating a Multiform Web Application ......................................................................................................... 457
Displaying the Life Cycle of a Page ................................................................................................................ 458
Web Forms Navigation and Cross-Page Posting ........................................................................................... 460
Summary.......................................................................................................................................................... 464

Chapter 11: Standard Web Server Controls ........................................................................... 465


In Depth........................................................................................................................................................... 466
The Control Class ............................................................................................................................................ 466
The WebControl Class..................................................................................................................................... 468
The Button Control .......................................................................................................................................... 469
The TextBox Control........................................................................................................................................ 470
The Label Control ............................................................................................................................................ 471
The HyperLink Control................................................................................................................................... 472
The LinkButton Control .................................................................................................................................. 472
The Literal Control .......................................................................................................................................... 473
The PlaceHolder Control................................................................................................................................. 473
The HiddenField Control ................................................................................................................................ 474
The FileUpload Control................................................................................................................................... 474
The CheckBox Control..................................................................................................................................... 474
The RadioButton Control ................................................................................................................................ 475
The ListBox Control......................................................................................................................................... 475
The DropDownList Control ............................................................................................................................ 476
The BulletedList Control ................................................................................................................................. 476
The Image Control ........................................................................................................................................... 478
The ImageButton Control................................................................................................................................ 478
The ImageMap Control ................................................................................................................................... 479
The Table Control ............................................................................................................................................ 480
The Wizard Control ......................................................................................................................................... 480
User Controls and Custom Controls .............................................................................................................. 483
Using CSS in Web Applications...................................................................................................................... 483

xvii

Table of Contents

Immediate Solutions ...................................................................................................................................... 484


Using the Button Control ................................................................................................................................ 484
Using the TextBox Control .............................................................................................................................. 485
Using the Label Control .................................................................................................................................. 486
Using the HyperLink Control ......................................................................................................................... 488
Using the LinkButton Control......................................................................................................................... 489
Using the Literal Control................................................................................................................................. 490
Using the PlaceHolder Control ....................................................................................................................... 492
Using the HiddenField Control ...................................................................................................................... 493
Using the FileUpload Control ......................................................................................................................... 494
Using the CheckBox Control........................................................................................................................... 496
Using the RadioButton Control ...................................................................................................................... 498
Using the ListBox Control ............................................................................................................................... 501
Using the DropDownList Control .................................................................................................................. 505
Using the BulletedList Control ....................................................................................................................... 506
Using the Image Control ................................................................................................................................. 508
Using the ImageButton Control ...................................................................................................................... 509
Using the ImageMap Control ......................................................................................................................... 511
Using the Table Control .................................................................................................................................. 513
Using the Wizard Control ............................................................................................................................... 516
User Controls and Custom Controls .............................................................................................................. 520
Creating User Controls.............................................................................................................................. 520
Creating Custom Controls ........................................................................................................................ 521
Using CSS in Web Applications...................................................................................................................... 523
Summary.......................................................................................................................................................... 528

Chapter 12: Navigation Controls in ASP.NET 4.5 .................................................................. 529


In Depth........................................................................................................................................................... 530
Using the TreeView Class ............................................................................................................................... 530
The TreeView Control ..................................................................................................................................... 532
Adding Nodes to a TreeView Control Dynamically................................................................................ 533
Adding Nodes to a TreeView Control Using the DataSource Property ................................................. 533
Adding Nodes to a TreeView Control at Design Time............................................................................ 534
Using the Menu Class...................................................................................................................................... 535
The Menu Control ........................................................................................................................................... 537
Menu Display Properties .......................................................................................................................... 538
Menu Styles................................................................................................................................................ 538
Using the SiteMapPath Class .......................................................................................................................... 538
The SiteMapPath Control ................................................................................................................................ 540
Data Retrieval Using the SiteMapPath Control ....................................................................................... 540
SiteMapPath Behaviors ............................................................................................................................. 540

xviii

Table of Contents

SiteMapPath Style...................................................................................................................................... 541


SiteMapPath Templates............................................................................................................................. 541
Immediate Solutions ...................................................................................................................................... 542
Creating the TreeView Control ....................................................................................................................... 542
Generating TreeView from a Database .......................................................................................................... 550
Creating Static Menus ..................................................................................................................................... 554
Creating Dynamic Menus ............................................................................................................................... 556
Creating SiteMapPath ..................................................................................................................................... 558
Summary.......................................................................................................................................................... 560

Chapter 13: Login and Web Parts Controls in ASP.NET 4.5.................................................. 561
In Depth........................................................................................................................................................... 562
Introducing Login Controls ............................................................................................................................ 562
The Login Control ..................................................................................................................................... 563
The LoginView Control............................................................................................................................. 565
The LoginName Control ........................................................................................................................... 567
The LoginStatus Control ........................................................................................................................... 567
The PasswordRecovery Control ............................................................................................................... 569
The ChangePassword Control .................................................................................................................. 571
The CreateUserWizard Control ................................................................................................................ 575
Introducing Web Parts Controls ..................................................................................................................... 579
The WebPartManager Control .................................................................................................................. 579
The ProxyWebPartManager Control ........................................................................................................ 588
The WebPartZone Control ........................................................................................................................ 589
The CatalogZone Control.......................................................................................................................... 592
The DeclarativeCatalogPart Control......................................................................................................... 593
The PageCatalogPart Control ................................................................................................................... 595
The ImportCatalogPart Control ................................................................................................................ 596
The EditorZone Control ............................................................................................................................ 598
The LayoutEditorPart Control .................................................................................................................. 600
The AppearanceEditorPart Control .......................................................................................................... 602
The PropertyGridEditorPart Control........................................................................................................ 603
The BehaviorEditorPart Control ............................................................................................................... 604
The ConnectionsZone Control .................................................................................................................. 606
Immediate Solutions ...................................................................................................................................... 609
Using the Login and LoginView Controls...................................................................................................... 609
Using the LoginName and LoginStatus Controls .......................................................................................... 615
Using the CreateUserWizard Control ............................................................................................................ 617
Using the PasswordRecovery and ChangePassword Controls ..................................................................... 619
Creating the WebPartManager Control in Code............................................................................................ 625
Creating the WebPartZone Control in Code .................................................................................................. 627

xix

Table of Contents

Creating the CatalogZone Control in Code.................................................................................................... 628


Creating the EditorZone Control in Code ...................................................................................................... 630
Creating the PropertyGridEditorPart Control in Code ................................................................................. 632
Creating the ConnectionsZone Control in Code............................................................................................ 634
Summary.......................................................................................................................................................... 636

Chapter 14: Enhancing Web Applications with Silverlight 5.0 .............................................. 637
In Depth........................................................................................................................................................... 638
Main Features of Silverlight ............................................................................................................................ 638
Improved Programming Model................................................................................................................ 639
Comprehensive UI Framework ................................................................................................................ 639
Support for Deep Zoom Technology ........................................................................................................ 639
Support for 2-D Graphics, Animations, and Multimedia ........................................................................ 639
Support for Networking............................................................................................................................ 640
Support for Higher Quality Video and Audio ......................................................................................... 640
Empowering Richer Experiences .............................................................................................................. 640
Improving Rich Internet Application Productivity ................................................................................. 641
Enhanced Data Support ............................................................................................................................ 641
Improved Performance ............................................................................................................................. 641
Out of Browser Capabilities ...................................................................................................................... 642
New Features of Silverlight 5.0 ....................................................................................................................... 642
Enhancements in Controls ........................................................................................................................ 642
Enhancements in Media ............................................................................................................................ 642
Enhancements in Graphics........................................................................................................................ 643
Enhancements in Text ............................................................................................................................... 643
Enhancements in Trusted Applications ................................................................................................... 643
Enhancements in Printing ......................................................................................................................... 643
Enhancements in User Interface ............................................................................................................... 643
Enhancements in Data Binding................................................................................................................. 644
Enhancements in XAML ........................................................................................................................... 644
Improvements in Performance ................................................................................................................. 644
Architecture of Silverlight ............................................................................................................................... 644
The .NET Framework for Silverlight ........................................................................................................ 645
The Core Presentation Framework ........................................................................................................... 645
Silverlight Applications in Visual Studio 2012............................................................................................... 646
Assemblies and Files in Silverlight Applications..................................................................................... 648
Controls in Silverlight Applications ......................................................................................................... 656
Silverlight and ASP.NET ................................................................................................................................. 671
Data Binding in Silverlight.............................................................................................................................. 673
Types of Data Binding in Silverlight ........................................................................................................ 673
Binding Object ........................................................................................................................................... 674
Data Context .............................................................................................................................................. 674

xx

Table of Contents

Converters.................................................................................................................................................. 674
DataTemplate and ObservableCollection................................................................................................. 674
Data Binding Using Networking API....................................................................................................... 675
Immediate Solutions ...................................................................................................................................... 676
Creating a Simple Silverlight Application...................................................................................................... 676
Using Silverlight Controls............................................................................................................................... 678
Using the Calendar Control ...................................................................................................................... 678
Using the DatePicker Control ................................................................................................................... 685
Using the GridSplitter Control.................................................................................................................. 689
Using the HyperlinkButton Control ......................................................................................................... 691
Using the MediaElement Control ............................................................................................................. 693
Using the Popup Control .......................................................................................................................... 695
Using the TabControl Control .................................................................................................................. 699
Using the AutoCompleteBox Control....................................................................................................... 702
Using the DataPager Control .................................................................................................................... 705
Using the TreeView Control ..................................................................................................................... 707
Integrating Silverlight with ASP.NET Applications ...................................................................................... 709
Data Binding with Silverlight Application..................................................................................................... 710
Summary.......................................................................................................................................................... 716

Part V: Services and Deployment


Chapter 15: ASP.NET Web Services....................................................................................... 717
In Depth........................................................................................................................................................... 718
Introducing Web Services ............................................................................................................................... 718
Infrastructure of ASP.NET Web Services ....................................................................................................... 719
Web Services Directory ............................................................................................................................. 719
Web Services Discovery ............................................................................................................................ 720
Web Services Description.......................................................................................................................... 720
Web Services Wire Format ........................................................................................................................ 722
The Code Model of ASP.NET Web Services .................................................................................................. 725
The WebService Directive ......................................................................................................................... 725
The Code-Behind File ................................................................................................................................ 725
Web Service Properties.................................................................................................................................... 727
Application Property ................................................................................................................................. 727
Context Property ....................................................................................................................................... 728
Server Property .......................................................................................................................................... 728
Session Property ........................................................................................................................................ 729
ASP.NET AJAX and Web Services ................................................................................................................. 729
ASP.NET AJAX Client Architecture for Web Services ............................................................................ 729
ASP.NET AJAX Server Architecture for Web Services............................................................................ 731
Securing Web Services..................................................................................................................................... 731

xxi

Table of Contents

Encryption and Message-Based Security ................................................................................................. 732


Authentication and Access Controls for the Web Service ....................................................................... 732
Immediate Solutions ...................................................................................................................................... 733
Creating a Web Service ................................................................................................................................... 733
Creating a Web Service Project ................................................................................................................. 733
Declaring the Web Service ........................................................................................................................ 734
Setting the WebService Attribute.............................................................................................................. 735
Declaring Methods for a Web Service ...................................................................................................... 735
Deploying a Web Service ................................................................................................................................ 737
Using the WebService Class ............................................................................................................................ 744
Using the Web Service..................................................................................................................................... 746
Sharing Data Across Web Services ................................................................................................................. 751
Calling a Web Service from the Client............................................................................................................ 756
Caching with Web Services............................................................................................................................. 758
Working with ASP.NET AJAX Enabled Web Services .................................................................................. 760
Creating the ASP.NET AJAX-Enabled Web Service ................................................................................ 760
Calling the ASP.NET AJAX Enabled Web Service from Script ............................................................... 762
Using the UpdatePanel Control with a Web Service ..................................................................................... 763
Summary.......................................................................................................................................................... 766

Chapter 16: Working with Windows Communication Foundation ........................................ 767


In Depth........................................................................................................................................................... 768
What is New in WCF 4.5? ............................................................................................................................... 768
Instancing Model of WCF Services ................................................................................................................. 771
Per-call Instancing Service ........................................................................................................................ 771
Per-session Instancing Service .................................................................................................................. 773
Sharable Instancing Service....................................................................................................................... 773
Singleton Instancing Service ..................................................................................................................... 774
The WCF Architecture..................................................................................................................................... 774
The Contracts Layer .................................................................................................................................. 775
The Service Runtime Layer ....................................................................................................................... 776
The Messaging Layer................................................................................................................................. 776
The Activation and Hosting Layer ........................................................................................................... 777
Configuration of WCF Services ................................................................................................................ 777
WCF Clients ............................................................................................................................................... 778
WCF Service Identity................................................................................................................................. 779
WCF and ASP.NET AJAX ......................................................................................................................... 779
Immediate Solutions ...................................................................................................................................... 781
Creating WCF Services.................................................................................................................................... 781
Creating a Service Contract and Defining the Operations ...................................................................... 781
Implementing the Service Contract .......................................................................................................... 782

xxii

Table of Contents

Hosting WCF Services ............................................................................................................................... 783


Creating a WCF Client .............................................................................................................................. 795
Calling Operations of a WCF Service ....................................................................................................... 798
Creating an ASP.NET AJAX-Enabled WCF Service ................................................................................ 800
Calling WCF Services from Client-side Scripts ........................................................................................ 804
Summary.......................................................................................................................................................... 806

Chapter 17: Deploying Windows and Web Applications ....................................................... 807


In Depth........................................................................................................................................................... 808
InstallShield ..................................................................................................................................................... 808
InstallShield Limited Edition for Visual Studio ............................................................................................. 808
ClickOnce Deployment Technology ............................................................................................................... 815
Application Cache ..................................................................................................................................... 816
Security ...................................................................................................................................................... 816
Including Prerequisites for an Application Deployment .............................................................................. 816
XCopy Deployment ......................................................................................................................................... 818
Deploying Web Applications and Web Sites ................................................................................................. 818
Using the One-Click Publishing Feature .................................................................................................. 819
Transforming the Web.config File ............................................................................................................ 819
Deploying an SQL Server Database.......................................................................................................... 820
Using the Copy Web Site Tool .................................................................................................................. 821
Immediate Solutions ...................................................................................................................................... 822
Deploying a Windows Application Using Windows Installer...................................................................... 822
Creating an Installer File ........................................................................................................................... 822
Deploying an Application on a Target Computer ................................................................................... 828
Deploying a Windows-Based Application Using ClickOnce ........................................................................ 831
Building a Self-Updating Application ............................................................................................................ 833
XCopy Deployment ......................................................................................................................................... 835
Deploying a WPF Application ........................................................................................................................ 835
Deploying an XBAP Application Using ClickOnce Feature.......................................................................... 841
Summary.......................................................................................................................................................... 842

Part VI: Interacting with Database in C# 5.0


Chapter 18: ADO.NET Entity Framework................................................................................ 843
In Depth........................................................................................................................................................... 844
Exploring ADO.NET Entity Framework ........................................................................................................ 845
Exploring the Features of Entity Framework ................................................................................................. 847
Understanding the Entity Data Model ........................................................................................................... 849
Data Modeling ........................................................................................................................................... 849
Conceptual Schema Definition Language ................................................................................................ 850

xxiii

Table of Contents

Storage Schema Definition Language....................................................................................................... 851


Mapping Specification Language ............................................................................................................. 851
The EDM Types ......................................................................................................................................... 852
Type Constraints........................................................................................................................................ 853
Data Model Tools ...................................................................................................................................... 853
Understanding Object Services ....................................................................................................................... 854
Classes of Object Services.......................................................................................................................... 854
Features of Object Services........................................................................................................................ 855
Understanding Entity SQL Language ............................................................................................................ 855
Features of Entity SQL............................................................................................................................... 856
Operators for Entity SQL .......................................................................................................................... 858
Understanding the EntityClient Provider ...................................................................................................... 858
ADO.NET Metadata ........................................................................................................................................ 863
EDM Generator Tool ....................................................................................................................................... 865
Immediate Solutions ...................................................................................................................................... 868
Creating an Entity Data Model ....................................................................................................................... 868
Working with Entity Data Model Designer ................................................................................................... 872
Using the Entity Model Browser Window ............................................................................................... 872
Using the Mapping Details Window ........................................................................................................ 874
Working with Entity Framework Controls .................................................................................................... 877
Creating an Entity by Using the Entity Control ....................................................................................... 877
Creating an Association by Using the Association Control .................................................................... 879
Creating an Inheritance by Using the Inheritance Control...................................................................... 880
Creating an EDM-Based Application ............................................................................................................. 881
Summary.......................................................................................................................................................... 884

Chapter 19: Data Access with ADO.NET ................................................................................ 885


In Depth........................................................................................................................................................... 886
Understanding Databases ............................................................................................................................... 886
Understanding SQL......................................................................................................................................... 886
The SELECT Statement.............................................................................................................................. 887
The WHERE Clause................................................................................................................................... 887
The LIKE Clause ........................................................................................................................................ 887
The DISTINCT Clause ............................................................................................................................... 887
The Logical Operators ............................................................................................................................... 888
The ORDER BY Clause.............................................................................................................................. 888
The GROUP BY Clause ............................................................................................................................. 888
The DELETE Statement ............................................................................................................................. 888
The UPDATE Statement............................................................................................................................ 888
Joining Tables ............................................................................................................................................ 888
The DROP Statement................................................................................................................................. 889

xxiv

Table of Contents

Understanding ADO.NET............................................................................................................................... 889


Describing the Architecture of ADO.NET................................................................................................ 890
ADO.NET Entity Framework ................................................................................................................... 893
Immediate Solutions ...................................................................................................................................... 895
Creating Connection Strings ........................................................................................................................... 895
Creating a Connection to a Database.............................................................................................................. 899
SQL Server Database ................................................................................................................................. 899
OLEDB Database ....................................................................................................................................... 904
ODBC Data Source .................................................................................................................................... 910
Creating a Command Object........................................................................................................................... 916
Working with DataAdapters........................................................................................................................... 917
Creating Dataset from DataAdapter......................................................................................................... 918
Paging with DataAdapters........................................................................................................................ 918
Updating with DataAdapters ................................................................................................................... 919
Adding Multiple Tables to a Dataset ........................................................................................................ 920
Creating DataView .................................................................................................................................... 921
Using DataReader Work with Databases ....................................................................................................... 923
Summary.......................................................................................................................................................... 924

Chapter 20: .NET and SQL Server .......................................................................................... 925


In Depth........................................................................................................................................................... 926
Hosting of .NET Runtime................................................................................................................................ 926
Exploring the Microsoft.SqlServer.Server Namespace .................................................................................. 927
Exploring Stored Procedures .......................................................................................................................... 928
Exploring Triggers........................................................................................................................................... 928
Exploring User-Defined Aggregate Functions............................................................................................... 929
Exploring UDFs ............................................................................................................................................... 930
Exploring UDT................................................................................................................................................. 931
Using XML Data in SQL Server ...................................................................................................................... 931
Immediate Solutions ...................................................................................................................................... 932
Creating a SQL Server Project ......................................................................................................................... 932
Creating and Using Stored Procedures .......................................................................................................... 933
Creating and Using Triggers........................................................................................................................... 936
Creating and Using User-Defined Aggregate Functions............................................................................... 939
Creating and Using UDFs ............................................................................................................................... 941
Creating and Using UDTs ............................................................................................................................... 942
Handling XML Data in SQL Server and .NET Framework ........................................................................... 946
Summary.......................................................................................................................................................... 948

xxv

Table of Contents

Chapter 21: Data Binding in Windows Forms and WPF Applications .................................. 949
In Depth........................................................................................................................................................... 950
Introducing Data Binding ............................................................................................................................... 950
Data Binding to Windows Forms Controls .................................................................................................... 951
Data Binding to WPF Controls........................................................................................................................ 951
Data Flow Directions in WPF.................................................................................................................... 951
Sources of Data Binding in WPF ............................................................................................................... 952
Different Ways of Data Binding in WPF .................................................................................................. 952
Data Binding in WPF Using WCF Data Services ........................................................................................... 953
Immediate Solutions ...................................................................................................................................... 954
Implementing Simple Data Binding in Windows Forms .............................................................................. 954
Implementing Complex Data Binding in Windows Forms ........................................................................... 958
Binding Windows Forms Controls ................................................................................................................. 959
Text Boxes and Labels ............................................................................................................................... 959
Check Boxes ............................................................................................................................................... 961
Combo Boxes ............................................................................................................................................. 963
List Boxes ................................................................................................................................................... 964
Checked List Boxes .................................................................................................................................... 965
MenuStrip and StatusStrip Controls......................................................................................................... 966
Using the BindingContext Class ..................................................................................................................... 968
Moving to the First Record........................................................................................................................ 970
Moving to the Previous Record ................................................................................................................ 970
Displaying the Next or Current Record ................................................................................................... 970
Moving to the Last Record ........................................................................................................................ 971
Using Parameterized SQL Queries ................................................................................................................. 971
Binding Data in WPF....................................................................................................................................... 972
Binding a CLR Object in WPF ......................................................................................................................... 974
Binding an ADO.NET Object in WPF............................................................................................................. 977
Binding an XML Object in WPF ...................................................................................................................... 978
Using the DependencyObject Constructor in WPF........................................................................................ 980
Binding a Web Service in WPF ....................................................................................................................... 981
Creating a WCF Data Service.......................................................................................................................... 985
Summary.......................................................................................................................................................... 988

Chapter 22: Data Binding in ASP.NET Applications .............................................................. 989


In Depth........................................................................................................................................................... 990
The GridView Control..................................................................................................................................... 990
The DataList Control ....................................................................................................................................... 994
The DetailsView Control ................................................................................................................................. 996
The FormView Control.................................................................................................................................... 999
The ListView Control .................................................................................................................................... 1003

xxvi

Table of Contents

The Repeater Control .................................................................................................................................... 1006


The DataPager Control.................................................................................................................................. 1008
The Chart Control.......................................................................................................................................... 1010
The QueryExtender Control.......................................................................................................................... 1011
The SqlDataSource Control ........................................................................................................................... 1012
The LinqDataSource Control ........................................................................................................................ 1015
The ObjectDataSource Control ..................................................................................................................... 1018
The XmlDataSource Control ......................................................................................................................... 1022
The EntityDataSource Control ...................................................................................................................... 1024
The SiteMapDataSource Control .................................................................................................................. 1027
Immediate Solutions .................................................................................................................................... 1029
Using the SqlDataSource Control ................................................................................................................. 1029
Using the LinqDataSource Control............................................................................................................... 1035
Using the ObjectDataSource Control............................................................................................................ 1039
Using the XmlDataSource Control ............................................................................................................... 1042
The EntityDataSource Control ...................................................................................................................... 1045
Using the GridView Control ......................................................................................................................... 1050
Using the DataList Control ........................................................................................................................... 1054
Using the DetailsView Control ..................................................................................................................... 1057
Using the FormView Control........................................................................................................................ 1059
Using the Repeater Control........................................................................................................................... 1062
Using the ListView and DataPager Controls ............................................................................................... 1064
Using the Chart Control ................................................................................................................................ 1069
Using the QueryExtender Control ................................................................................................................ 1071
Summary........................................................................................................................................................ 1072

Chapter 23: LINQ Queries ..................................................................................................... 1073


In Depth......................................................................................................................................................... 1074
Introducing LINQ Queries ............................................................................................................................ 1074
Data Sources in LINQ Queries................................................................................................................ 1075
Deferred Query Execution and Immediate Execution........................................................................... 1075
LINQ and Generic Types ........................................................................................................................ 1075
Standard Query Operators............................................................................................................................ 1076
Filtering Operators .................................................................................................................................. 1076
Projection Operators................................................................................................................................ 1076
Sorting Operators .................................................................................................................................... 1077
Join Operators .......................................................................................................................................... 1077
Grouping Operators ................................................................................................................................ 1078
Quantifier Operators ............................................................................................................................... 1078
Partitioning Operators............................................................................................................................. 1078
Set Operators ........................................................................................................................................... 1079

xxvii

Table of Contents

Element Operators................................................................................................................................... 1079


Aggregate Operators ............................................................................................................................... 1080
Conversion Operators ............................................................................................................................. 1081
Generation Operators .............................................................................................................................. 1081
Zip Operator ............................................................................................................................................ 1081
Introducing LINQ to Objects ........................................................................................................................ 1082
Strings ...................................................................................................................................................... 1082
Reflections ................................................................................................................................................ 1082
File Directories ......................................................................................................................................... 1082
Introducing LINQ to ADO.NET ................................................................................................................... 1083
LINQ to DataSet ...................................................................................................................................... 1083
LINQ to SQL ............................................................................................................................................ 1084
LINQ to Entities ....................................................................................................................................... 1084
Introducing LINQ to XML ............................................................................................................................ 1085
Difference Between LINQ to XML and DOM Method .......................................................................... 1085
Working with XML Trees in LINQ ......................................................................................................... 1085
The LinqDataSource Control ........................................................................................................................ 1086
Extension Methods ........................................................................................................................................ 1086
Anonymous Methods .................................................................................................................................... 1087
Lambda Expressions...................................................................................................................................... 1087
Comparing Lambda Expressions with Anonymous Methods .................................................................... 1087
Introducing PLINQ ....................................................................................................................................... 1087
Immediate Solutions .................................................................................................................................... 1089
Creating a LINQ to SQL Application ........................................................................................................... 1089
Creating a LINQ to Object Application ........................................................................................................ 1091
Creating a LINQ to XML Application .......................................................................................................... 1093
Creating a Sample Extension Method .......................................................................................................... 1096
Using the Where() method ............................................................................................................................ 1098
Type Filtering................................................................................................................................................. 1099
Using the Projection Operators ..................................................................................................................... 1100
Using Sorting Operators................................................................................................................................ 1102
Using the Join() Method ................................................................................................................................ 1104
Using the Grouping Operator ....................................................................................................................... 1106
Using the Partitioning Operators.................................................................................................................. 1107
Using the Set Operators ................................................................................................................................ 1109
Using the Aggregate Operators .................................................................................................................... 1111
Using the Conversion Operators .................................................................................................................. 1112
Using the Generation Operators ................................................................................................................... 1114
Using the Zip() Operator............................................................................................................................... 1116
Creating a Simple PLINQ Query .................................................................................................................. 1118
Summary........................................................................................................................................................ 1120

xxviii

Table of Contents

Part VII: Advanced Topics


Chapter 24: File Handling in C# 2012 ................................................................................... 1121
In Depth......................................................................................................................................................... 1122
The System.IO Namespace............................................................................................................................ 1122
Working with Drives ..................................................................................................................................... 1123
Working with Directories.............................................................................................................................. 1124
Working with Files ........................................................................................................................................ 1126
The FileStream Class ............................................................................................................................... 1126
The StreamReader and StreamWriter Classes........................................................................................ 1127
The BinaryReader and BinaryWriter Classes ......................................................................................... 1128
The File Class ........................................................................................................................................... 1128
The FileInfo Class .................................................................................................................................... 1130
The Path Class.......................................................................................................................................... 1130
File Compression ........................................................................................................................................... 1131
Immediate Solutions .................................................................................................................................... 1133
Working with Drives ..................................................................................................................................... 1133
Working with Directories.............................................................................................................................. 1136
Checking Directory Existence ................................................................................................................. 1136
Getting the List of Directories ................................................................................................................. 1137
Getting the List of Subdirectories ........................................................................................................... 1139
Getting the List of Files in Subdirectories .............................................................................................. 1140
Creating, Renaming, and Deleting a Directory ...................................................................................... 1141
Working with Files ........................................................................................................................................ 1145
Checking File Existence and its Properties............................................................................................. 1146
Creating, Writing, and Reading Data from Text Files ........................................................................... 1149
Reading and Writing Binary Files........................................................................................................... 1151
Compressing Files in .NET Framework ....................................................................................................... 1153
Summary........................................................................................................................................................ 1158

Chapter 25: Working with XML and .NET ............................................................................. 1159


In Depth......................................................................................................................................................... 1160
The System.Xml Namespace ......................................................................................................................... 1160
Working with Streamed XML ....................................................................................................................... 1160
The XmlReader Class .............................................................................................................................. 1161
Validating XML Documents Using the XmlReader Class ..................................................................... 1161
The XmlWriter Class ............................................................................................................................... 1161
Implementing DOM in .NET Framework 4.5 .............................................................................................. 1161
XPath and XSLT in .NET Framework 4.5 ..................................................................................................... 1162
The System.Xml.XPath Namespace ........................................................................................................ 1163
The System.Xml.Xsl Namespace............................................................................................................. 1164

xxix

Table of Contents

Using XML with ADO.NET .......................................................................................................................... 1165


Object Serialization with XML ...................................................................................................................... 1165
Immediate Solutions .................................................................................................................................... 1166
Working with Streamed XML ....................................................................................................................... 1166
Reading Data ........................................................................................................................................... 1167
Validating Data........................................................................................................................................ 1168
Writing Data ............................................................................................................................................ 1171
Implementing DOM in .NET ........................................................................................................................ 1173
Finding Elements by Tag Name.............................................................................................................. 1173
Inserting Nodes ....................................................................................................................................... 1174
Navigating and Transforming Data ............................................................................................................. 1176
Navigating and Finding Elements by Attribute Values ........................................................................ 1176
Transforming XML .................................................................................................................................. 1177
Converting ADO.NET and XML Data ......................................................................................................... 1178
Converting ADO.NET Data into XML Data .......................................................................................... 1178
Writing Data into a DiffGram ................................................................................................................. 1180
Implementing Object Serialization with XML ............................................................................................. 1182
Serialization with Source Code Access................................................................................................... 1182
Serialization without Source Code Access ............................................................................................. 1184
Summary........................................................................................................................................................ 1186

Chapter 26: Threading in C# 5.0 ........................................................................................... 1187


In Depth......................................................................................................................................................... 1188
The Thread Class ........................................................................................................................................... 1188
Multithreading............................................................................................................................................... 1190
Thread Priorities ............................................................................................................................................ 1191
Thread States.................................................................................................................................................. 1192
Thread Synchronization ................................................................................................................................ 1193
Immediate Solutions .................................................................................................................................... 1194
Creating a Thread .......................................................................................................................................... 1194
Starting a Thread ........................................................................................................................................... 1194
Putting a Thread to Sleep .............................................................................................................................. 1195
Suspending a Thread..................................................................................................................................... 1196
Resuming a Thread........................................................................................................................................ 1197
Stopping a Thread ......................................................................................................................................... 1198
Creating a Multithreaded Application ......................................................................................................... 1200
Setting Priority of a Thread ........................................................................................................................... 1201
Determining State of a Thread ...................................................................................................................... 1203
Joining Threads.............................................................................................................................................. 1205
Synchronizing Threads ................................................................................................................................. 1207
Summary........................................................................................................................................................ 1210

xxx

Table of Contents

Chapter 27: Collections and Generics.................................................................................. 1211


In Depth......................................................................................................................................................... 1212
Understanding Collections ........................................................................................................................... 1212
Collection Classes in .NET ............................................................................................................................ 1213
The ArrayList Class ................................................................................................................................. 1213
The Hashtable Class ................................................................................................................................ 1214
The SortedList Class ................................................................................................................................ 1215
The Stack Class ........................................................................................................................................ 1216
The Queue Class ...................................................................................................................................... 1217
The BitArray Class................................................................................................................................... 1218
Understanding Generics ............................................................................................................................... 1219
Generic Collection Classes in .NET .............................................................................................................. 1220
The List<T> Class ................................................................................................................................... 1220
The LinkedList<T> Class ....................................................................................................................... 1221
The SortedList<TKey, TValue> Class .................................................................................................... 1222
The Dictionary<TKey, TValue> Class ................................................................................................... 1223
The SortedDictionary<TKey, TValue> Class ........................................................................................ 1224
The Stack<T> Class................................................................................................................................. 1224
The Queue<T> Class .............................................................................................................................. 1225
The HashSet<T> Class............................................................................................................................ 1225
Covariance and Contravariance in Generics ................................................................................................ 1228
Immediate Solutions .................................................................................................................................... 1229
Working with Collection Classes .................................................................................................................. 1229
Using the ArrayList Class ....................................................................................................................... 1229
Using the Hashtable Class....................................................................................................................... 1232
Using the SortedList Class ...................................................................................................................... 1236
Using the Stack Class............................................................................................................................... 1239
Using the Queue Class ............................................................................................................................ 1242
Using the BitArray Class ......................................................................................................................... 1245
Working with Generic Collection Classes .................................................................................................... 1247
Using the List<T> Class ......................................................................................................................... 1247
Using the LinkedList<T> Class.............................................................................................................. 1249
Using the SortedList<TKey, TValue> Class .......................................................................................... 1253
Using the Dictionary<TKey, TValue> Class ......................................................................................... 1255
Using the SortedDictionary<TKey, TValue> Class............................................................................... 1257
Using the Stack<T> Class....................................................................................................................... 1259
Using the Queue<T> Class..................................................................................................................... 1261
Using the HashSet<T> Class .................................................................................................................. 1263
Creating Your Own Generic Collection Classes .......................................................................................... 1265
Implementing Covariance and Contravariance in Generics ....................................................................... 1269
Summary........................................................................................................................................................ 1270

xxxi

Table of Contents

Chapter 28: Reflection in C# 5.0 ........................................................................................... 1271


In Depth......................................................................................................................................................... 1272
Exploring the Reflection Process................................................................................................................... 1272
Need of Reflection ................................................................................................................................... 1272
Using Reflection....................................................................................................................................... 1273
Using Reflection for Dynamic Loading of Assembly .................................................................................. 1274
Immediate Solutions .................................................................................................................................... 1276
Obtaining Class and Type Information from an Assembly......................................................................... 1276
Obtaining Member Information from a Class .............................................................................................. 1280
Dynamically Invoking Methods from Classes in an Assembly .................................................................. 1282
Verifying that a Class Contains a Method.................................................................................................... 1285
Dumping the Methods and Their Parameters.............................................................................................. 1286
Summary........................................................................................................................................................ 1288

Chapter 29: .NET Remoting in C# 5.0 ................................................................................... 1289


In Depth......................................................................................................................................................... 1290
The .NET Remoting Architecture ................................................................................................................. 1290
Application Domains .............................................................................................................................. 1291
Remotable and Non-Remotable Objects................................................................................................. 1292
Setting Up Remoting Communication ................................................................................................... 1293
Channels .................................................................................................................................................. 1294
Message Sinks .......................................................................................................................................... 1297
Formatters ................................................................................................................................................ 1298
Proxies ...................................................................................................................................................... 1301
Hosting of Remote Objects............................................................................................................................ 1301
Setting the Lease Time of an Object .............................................................................................................. 1301
Immediate Solutions .................................................................................................................................... 1303
Creating and Using Application Domains ................................................................................................... 1303
Using Objects in another Application Domain ............................................................................................ 1305
Creating a Remotable Class .......................................................................................................................... 1306
Creating a Host Application ......................................................................................................................... 1307
Enumerating Registered Channels ......................................................................................................... 1311
Unregistering a Channel ......................................................................................................................... 1312
Registering Objects .................................................................................................................................. 1312
Specifying Formatters.............................................................................................................................. 1312
Creating a SingleCall Client Object .............................................................................................................. 1314
Creating a Class for SingleTon Client Object ............................................................................................... 1315
Creating a Client Activated Object ............................................................................................................... 1321
Summary........................................................................................................................................................ 1324

xxxii

Table of Contents

Chapter 30: Security in .NET 4.5 ........................................................................................... 1325


In Depth......................................................................................................................................................... 1326
Introducing Code Verification and Security in .NET Framework .............................................................. 1326
Type-Safe Code........................................................................................................................................ 1327
Verifiably Type-Safe MSIL Code ............................................................................................................ 1328
Security Features in the .NET Framework ................................................................................................... 1329
Security Policy Simplification ................................................................................................................. 1329
Security Transparency Level 2 ................................................................................................................ 1329
Obsolete Permission Requests ................................................................................................................ 1329
Conditional APTCA ................................................................................................................................ 1330
Evidence Objects...................................................................................................................................... 1330
Evidence Collections ............................................................................................................................... 1330
Security Changes in the .NET Framework 4.5 ............................................................................................. 1330
Introducing Authentication and Authorization........................................................................................... 1330
Role-Based Security ....................................................................................................................................... 1331
Principal Object........................................................................................................................................ 1332
Identity Object ......................................................................................................................................... 1333
Role-Based Security Checks .................................................................................................................... 1334
The Roles Class ........................................................................................................................................ 1338
Membership Feature ..................................................................................................................................... 1356
ActiveDirectoryMembershipProvider.................................................................................................... 1356
SqlMembershipProvider ......................................................................................................................... 1357
The Membership Class ............................................................................................................................ 1359
MembershipUser Class ................................................................................................................................. 1360
Immediate Solutions .................................................................................................................................... 1361
Using Role-Based Security to Authenticate and Authorize Users .............................................................. 1361
Performing Role-Based Security Checks ...................................................................................................... 1368
Managing Roles and Users............................................................................................................................ 1373
Using Anonymous Authentication in ASP.NET.......................................................................................... 1377
Using Windows Authentication in ASP.NET .............................................................................................. 1379
Using Forms Authentication in ASP.NET .................................................................................................... 1384
Using Impersonation in ASP.NET Applications.......................................................................................... 1388
Creating User Using the Membership Class ................................................................................................ 1390
Validating and Retrieving a User ................................................................................................................. 1393
Understanding Stack Walk ........................................................................................................................... 1396
Security Caveats ............................................................................................................................................ 1399
Summary........................................................................................................................................................ 1400

Chapter 31: Case Study: Sports Website ............................................................................. 1401


Background.................................................................................................................................................... 1402
Problems with the Existing System .............................................................................................................. 1402

xxxiii

Table of Contents

Envisioned System ........................................................................................................................................ 1402


Project Requirements..................................................................................................................................... 1402
SDLC of Sports Website Project .................................................................................................................... 1403
The Requirement Analysis Phase ........................................................................................................... 1403
The Software Design Phase ..................................................................................................................... 1403
The Development and Testing Phase ..................................................................................................... 1407
The Implementation and Maintenance Phase ........................................................................................ 1408
Summary........................................................................................................................................................ 1408

Glossary ................................................................................................................................ 1409


Index ...................................................................................................................................... 1425
Online Resource Available with the Book ............................................................................ 1435

Bonus chapters/Appendices on the Web


Chapter 1:
Chapter 2:
Chapter 3:
Chapter 4:
Chapter 5:
Chapter 6:
Chapter 7:
Chapter 8:
Chapter 9:
Chapter 10:
Chapter 11:
Chapter 12:
Chapter 13:
Chapter 14:
Chapter 15:
Chapter 16:
Appendix A:
Appendix B:
Appendix C:
Appendix D:

xxxiv

Windows Forms in C# 5.0


Windows Forms Controls: Button, Label, TextBox, RichTextBox, and
MaskedTextBox
Windows Forms Controls: RadioButton, CheckBox, ListBox, CheckedListBox,
and ComboBox
Windows Forms Controls: ListView, TreeView, ImageList, PictureBox, Panel,
GroupBox, and TabControl
Windows Forms Controls: SplitContainer, ScrollBar, TrackBar, ToolTip,
NotifyIcon, MonthCalendar, DateTimePicker, Timer, and ProgressBar
Windows Forms Controls: ToolStrip, MenuStrip, ContextMenuStrip,
StatusStrip, and Dialog Box Controls
Working with Typography and Documents in WPF
Using Graphics and Multimedia in Windows Forms and WPF
Validation Controls in ASP.NET
Developing ASP.NET AJAX Applications
Windows Services
.NET Assemblies
Working with Windows Workflow Foundation
Working with COM+ Applications in C# 5.0
Application Globalization
Cryptography in .NET 4.5
Introducing SOA
Introducing Cloud Computing
Syndication
Introducing MAF and MEF Frameworks

Вам также может понравиться