Update to Wrox’s leading C# book for beginners
Get ready for the next release of Microsoft’s C# programming language with this essential Wrox beginner’s guide. Beginning Microsoft Visual C# 2010 starts with the basics and brings you thoroughly up to speed. You’ll first cover the fundamentals such as variables, flow control, and object-oriented programming and gradually build your skills for Web and Windows programming, Windows forms, and data access.
Step-by-step directions walk you through processes and invite you to ‚Try it Out, ‚ at every stage. By the end, you’ll be able to write useful programming code following the steps you’ve learned in this thorough, practical book.
- The C# 4 programming language version will be synonymous with writing code with in C# 2010 in Visual Studio 2010, and you can use it to write Windows applications, Web apps with ASP.NET, and Windows Mobile and Embedded CE apps
- Provides step-by-step instructions for mastering topics such as variables, flow controls, and object-oriented programming before moving to Web and Windows programming and data access
- Addresses expressions, functions, debugging, error handling, classes, collections, comparisons, conversions, and more
If you’ve always wanted to master Visual C# programming, this book is the perfect one-stop resource.
Note: CD-ROM/DVD and other supplementary materials are not included as part of e Book file.
Inhaltsverzeichnis
Introduction xxxiii
Part I: The C# Language
Chapter 1: Introducing C# 3
What is the .NET Framework? 3
What’s in the .NET Framework? 4
Writing Applications Using the .NET Framework 5
CIL and JIT 5
Assemblies 5
Managed Code 6
Garbage Collection 6
Fitting It Together 7
Linking 8
What is C#? 8
Applications You Can Write with C# 9
C# in This Book 10
Visual Studio 2010 10
Visual Studio 2010 Express Products 11
Solutions 11
Summary 11
Chapter 2: Writing A C# Program 13
The Development Environments 14
Visual Studio 2010 14
Visual C# 2010 Express Edition 17
Console Applications 18
The Solution Explorer 22
The Properties Window 23
The Error List Window 23
Windows Forms Applications 24
Summary 28
Chapter 3: Variables and Expressions 31
Basic C# Syntax 32
Basic C# Console Application Structure 34
Variables 35
Simple Types 36
Variable Naming 40
Naming Conventions 41
Literal Values 42
String Literals 43
Variable Declaration and Assignment 44
Expressions 45
Mathematical Operators 45
Assignment Operators 50
Operator Precedence 51
Namespaces 51
Summary 55
Chapter 4: Flow Control 59
Boolean Logic 59
Boolean Assignment Operators 62
Bitwise Operators 64
Operator Precedence Updated 68
The goto Statement 68
Branching 69
The Ternary Operator 70
The if Statement 70
Checking More Conditions Using if Statements 73
The switch Statement 74
Looping 77
do Loops 78
while Loops 80
for Loops 83
Interrupting Loops 87
Infinite Loops 88
Summary 89
Chapter 5: More About Variables 93
Type Conversion 94
Implicit Conversions 94
Explicit Conversions 96
Explicit Conversions Using the Convert Commands 99
Complex Variable Types 102
Enumerations 102
Defining Enumerations 103
Structs 107
Defining Structs 107
Arrays 110
Declaring Arrays 110
foreach Loops 113
Multidimensional Arrays 113
Arrays of Arrays 115
String Manipulation 116
Summary 121
Chapter 6: Functions 125
Defining and Using Functions 126
Return Values 128
Parameters 130
Parameter Matching 132
Parameter Arrays 132
Reference and Value Parameters 134
Out Parameters 136
Variable Scope 137
Variable Scope in Other Structures 140
Parameters and Return Values versus Global Data 142
The Main() Function 143
Struct Functions 146
Overloading Functions 147
Delegates 149
Summary 152
Chapter 7: Debugging and Error Handling 155
Debugging in VS and VCE 156
Debugging in Nonbreak (Normal) Mode 157
Outputting Debugging Information 158
Tracepoints 163
Diagnostics Output Versus Tracepoints 164
Debugging in Break Mode 166
Entering Break Mode 166
Monitoring Variable Content 170
Stepping Through Code 172
Immediate and Command Windows 173
The Call Stack Window 174
Error Handling 175
try . . . catch . . . finally 176
Listing and Configuring Exceptions 181
Notes on Exception Handling 182
Summary 183
Chapter 8: Introduction To Object-Oriented Programming 185
What Is Object-Oriented Programming? 186
What Is an Object? 187
Properties and Fields 188
Methods 189
Everything’s an Object 189
The Life Cycle of an Object 190
Constructors 190
Destructors 191
Static and Instance Class Members 191
Static Constructors 191
Static Classes 192
OOP Techniques 192
Interfaces 193
Disposable Objects 194
Inheritance 194
Polymorphism 196
Interface Polymorphism 197
Relationships Between Objects 198
Containment 198
Collections 199
Operator Overloading 200
Events 200
Reference Types Versus Value Types 201
OOP in Windows Applications 201
Summary 204
Chapter 9: Defining Classes 209
Class Definitions in C# 209
Interface Definitions 212
System.Object 215
Constructors and Destructors 217
Constructor Execution Sequence 218
OOP Tools in VS and VCE 222
The Class View Window 222
The Object Browser 224
Adding Classes 226
Class Diagrams 227
Class Library Projects 229
Interfaces Versus Abstract Classes 232
Struct Types 235
Shallow Copying Versus Deep Copying 237
Summary 237
Chapter 10: Defining Class Members 241
Member Definitions 241
Defining Fields 242
Defining Methods 242
Defining Properties 244
Adding Members from a Class Diagram 249
Adding Methods 250
Adding Properties 251
Adding Fields 252
Refactoring Members 252
Automatic Properties 253
Additional Class Member Topics 253
Hiding Base Class Methods 254
Calling Overridden or Hidden Base Class Methods 255
The this Keyword 256
Nested Type Definitions 257
Interface Implementation 257
Implementing Interfaces in Classes 258
Explicit Interface Member Implementation 260
Adding Property Accessors with Nonpublic Accessibility 260
Partial Class Definitions 261
Partial Method Definitions 262
Example Application 264
Planning the Application 264
The Card Class 264
The Deck Class 265
Writing the Class Library 265
Adding the Suit and Rank Enumerations 266
Adding the Card Class 268
Adding the Deck Class 269
A Client Application for the Class Library 272
The Call Hierarchy Window 274
Summary 275
Chapter 11: Collections, Comparisons, and Conversions 277
Collections 278
Using Collections 278
Defining Collections 284
Indexers 286
Adding a Cards Collection to Card Lib 288
Keyed Collections and IDictionary 291
Iterators 293
Iterators and Collections 297
Deep Copying 299
Adding Deep Copying to Card Lib 301
Comparisons 303
Type Comparisons 303
Boxing and Unboxing 303
The is Operator 305
Value Comparisons 308
Operator Overloading 308
Adding Operator Overloads to Card Lib 313
The IComparable and IComparer Interfaces 318
Sorting Collections Using the IComparable and IComparer Interfaces 320
Conversions 324
Overloading Conversion Operators 324
The as Operator 326
Summary 327
Chapter 12: Generics 331
What Are Generics? 332
Using Generics 333
Nullable Types 333
Operators and Nullable Types 334
The ?? Operator 336
The System.Collections.Generics Namespace 340
List < T > 341
Sorting and Searching Generic Lists 343
Dictionary < K, V > 349
Modifying Card Lib to Use a Generic Collection Class 350
Defining Generic Types 351
Defining Generic Classes 351
The default Keyword 354
Constraining Types 354
Inheriting from Generic Classes 361
Generic Operators 362
Generic Structs 363
Defining Generic Interfaces 364
Defining Generic Methods 364
Defining Generic Delegates 366
Variance 366
Covariance 367
Contravariance 368
Summary 369
Chapter 13: Additional OOP Techniques 373
The :: Operator and the Global Namespace Qualifier 373
Custom Exceptions 375
Adding Custom Exceptions to Card Lib 375
Events 377
What Is an Event? 377
Handling Events 378
Defining Events 380
Multipurpose Event Handlers 385
The Event Handler and Generic Event Handler < T > Types 388
Return Values and Event Handlers 388
Anonymous Methods 389
Expanding and Using Card Lib 389
A Card Game Client for Card Lib 390
Summary 398
Chapter 14: C# Language Enhancements 401
Initializers 402
Object Initializers 402
Collection Initializers 404
Type Inference 407
Anonymous Types 409
Dynamic Lookup 413
The dynamic Type 414
IDynamic Meta Object Provider 417
Advanced Method Parameters 418
Optional Parameters 418
Optional Parameter Values 419
Optional Parameter Order 420
Named Parameters 420
Named and Optional Parameter Guidelines 424
Extension Methods 424
Lambda Expressions 429
Anonymous Methods Recap 429
Lambda Expressions for Anonymous Methods 430
Lambda Expression Parameters 434
Lambda Expression Statement Bodies 434
Lambda Expressions As Delegates and Expression Trees 435
Lambda Expressions and Collections 436
Summary 439
Part II: Windows Programming
Chapter 15: Basic Windows Programming 447
Controls 448
Properties 448
Anchoring, Docking, and Snapping Controls 449
Anchor and Dock Properties 450
Events 451
The Button Control 453
Button Properties 453
Button Events 453
Adding the Event Handlers 455
The Label and Link Label Controls 456
The Text Box Control 457
Text Box Properties 457
Text Box Events 458
Adding the Event Handlers 460
The Radio Button and Check Box Controls 464
Radio Button Properties 465
Radio Button Events 465
Check Box Properties 466
Check Box Events 466
The Group Box Control 466
The Rich Text Box Control 470
Rich Text Box Properties 470
Rich Text Box Events 472
The List Box and Checked List Box Controls 477
List Box Properties 477
List Box Methods 478
List Box Events 478
The List View Control 481
List View Properties 481
List View Methods 481
List View Events 481
List View Item 484
Column Header 484
The Image List Control 484
The Tab Control Control 491
Tab Control Properties 491
Working with the Tab Control 492
Summary 494
Chapter 16: Advanced Windows Forms Features 497
Menus and Toolbars 498
Two Is One 498
Using the Menu Strip Control 498
Creating Menus Manually 499
Properties of the Tool Strip Menu Item 501
Adding Functionality to Menus 501
Toolbars 503
Tool Strip Properties 504
Tool Strip Items 504
Adding Event Handlers 507
Status Strip 509
Status Strip Status Label Properties 510
SDI and MDI Applications 512
Building MDI Applications 513
Creating Controls 522
Adding Properties 524
Adding the Event Handlers 525
Debugging User Controls 527
Extending the Label Textbox Control 527
Adding More Properties 528
Adding More Event Handlers 529
Adding a Custom Event Handler 529
Summary 530
Chapter 17: Deploying Windows Applications 533
Deployment Overview 533
Click Once Deployment 534
Creating the Click Once Deployment 534
Installing the Application with Click Once 543
Creating and Using Updates of the Application 545
Visual Studio Setup and Deployment Project Types 546
Microsoft Windows Installer Architecture 547
Windows Installer Terms 548
Advantages of the Windows Installer 549
Creating an Installation Package for the MDI Editor 550
Planning the Installation 550
Creating the Project 552
Project Properties 553
Packaging 553
Prerequisites 554
Setup Editors 556
File System Editor 556
Adding Items to Special Folders 557
File Properties 557
File Types Editor 559
Create Actions 560
Launch Condition Editor 561
User Interface Editor 561
Additional Dialogs 563
Building the Project 565
Installation 566
Welcome 566
Read Me 566
License Agreement 567
Optional Files 568
Select Installation Folder 568
Disk Cost 568
Confirm Installation 569
Progress 570
Installation Complete 571
Running the Application 571
Uninstall 571
Summary 571
Part III: Web Programming
Chapter 18: ASP.NET Web Programming 577
Overview of Web Applications 578
ASP.NET Runtime 578
Creating a Simple Page 578
Server Controls 587
ASP.NET Postback 588
ASP.NET AJAX Postback 593
Input Validation 597
State Management 600
Client-Side State Management 601
View State 601
Cookies 602
Server-Side State Management 603
Session 603
Application 605
Cache 605
Styles 606
Master Pages 611
Site Navigation 616
Authentication and Authorization 619
Authentication Configuration 619
Using Security Controls 623
Reading from and Writing to a SQL Server Database 626
Summary 634
Chapter 19: Web Services 637
Where to Use Web Services 637
A Hotel Travel Agency Application Scenario 638
A Book Distributor Application Scenario 638
Client Application Types 639
Application Architecture 639
Web Services Architecture 640
Calling Methods and the Web Services Description Language 640
Calling a Method 641
WS-I Basic Profile 642
Web Services and the .NET Framework 642
Creating a Web Service 643
Web Service Attribute 643
Web Method Attribute 643
Web Service Binding Attribute 644
Client 645
Soap Http Client Protocol 645
Alternative Client Protocols 645
Creating a Simple ASP.NET Web Service 645
Adding a Web Method 648
Testing the Web Service 649
Implementing a Windows Client 649
Calling the Service Asynchronously 655
Implementing an ASP.NET Client 658
Passing Data 659
Summary 662
Chapter 20: Deploying Web Applications 665
Internet Information Services 665
IIS Configuration 666
Copying a Website 669
Publishing a Web Application 672
Windows Installer 675
Creating a Setup Program 675
Installing the Web Application 677
Summary 678
Part IV: Data Access
Chapter 21: File System Data 683
Streams 683
The Classes for Input and Output 684
The File and Directory Classes 686
The File Info Class 687
The Directory Info Class 689
Path Names and Relative Paths 690
The File Stream Object 690
File Position 691
Reading Data 692
Writing Data 695
The Stream Writer Object 697
The Stream Reader Object 699
Reading Data 701
Delimited Files 702
Reading and Writing Compressed Files 706
Serialized Objects 710
Monitoring the File System 715
Summary 722
Chapter 22: XML 725
XML Documents 726
XML Elements 726
Attributes 727
The XML Declaration 728
Structure of an XML Document 728
XML Namespaces 729
Well-Formed and Valid XML 730
Validating XML Documents 730
DTDs 730
Schemas 731
Using XML in Your Application 734
XML Document Object Model 734
Xml Document Class 735
Xml Element Class 735
Changing the Values of Nodes 739
Selecting Nodes 744
XPath 745
Summary 749
Chapter 23: Introduction To LINQ 753
First LINQ Query 754
Declaring a Variable for Results Using the var Keyword 756
Specify Data Source: from Clause 756
Specify Condition: where Clause 757
Select Items: select Clause 757
Finishing Up: Using the foreach Loop 757
Deferred Query Execution 757
Using the LINQ Method Syntax 758
LINQ Extension Methods 758
Query Syntax versus Method Syntax 758
Ordering Query Results 760
orderby Clause 761
Ordering Using Method Syntax 762
Querying a Large Data Set 764
Aggregate Operators 766
Querying Complex Objects 770
Projection: Creating New Objects in Queries 774
Projection: Method Syntax 776
Select Distinct Query 776
Any and All 777
Ordering by Multiple Levels 779
Multi-Level Ordering Method Syntax: Then By 781
Group Queries 781
Take and Skip 783
First and First Or Default 785
Set Operators 787
Joins 790
Summary 791
Chapter 24: Applying LINQ 795
LINQ Varieties 795
Using LINQ with Databases 796
Installing SQL Server and the Northwind Sample Data 797
Installing SQL Server Express 2008 797
Installing the Northwind Sample Database 797
First LINQ to Database Query 798
Navigating Database Relationships 801
Using LINQ with XML 804
LINQ to XML Functional Constructors 804
Constructing XML Element Text with Strings 808
Saving and Loading an XML Document 808
Loading XML from a String 811
Contents of a Saved XML Document 811
Working with XML Fragments 812
Generating XML from Databases 814
How to Query an XML Document 817
Using LINQ to XML Query Members 818
Elements() 818
Descendants() 819
Attributes() 821
Summary 823
Part V: Additional Techniques
Chapter 25: Windows Presentation Foundation 829
What Is WPF? 830
WPF for Designers 830
WPF for C# Developers 833
Anatomy of a Basic WPF Application 834
WPF Fundamentals 845
XAML Syntax 845
Object Element Syntax 845
Attribute Syntax 846
Property Element Syntax 846
Content Syntax 847
Mixing Property Element Syntax and Content Syntax 847
Markup Extensions 848
Desktop and Web Applications 848
The Application Object 849
Control Basics 849
Dependency Properties 850
Attached Properties 852
Routed Events 852
Attached Events 858
Control Layout 858
Stack Order 859
Alignment, Margins, Padding, and Dimensions 859
Border 860
Canvas 860
Dock Panel 861
Grid 863
Stack Panel 866
Wrap Panel 868
Control Styling 868
Styles 869
Templates 869
Triggers 874
Animation 875
Timelines without Key Frames 876
Timelines with Key Frames 877
Static and Dynamic Resources 878
Static Resources 878
Dynamic Resources 878
Referencing Style Resources 879
Programming with WPF 884
WPF User Controls 884
Implementing Dependency Properties 884
Summary 895
Chapter 26: Windows Communication Foundation 899
What Is WCF? 900
WCF Concepts 901
WCF Communication Protocols 901
Addresses, Endpoints, and Bindings 902
Contracts 904
Message Patterns 905
Behaviors 905
Hosting 906
WCF Programming 906
The WCF Test Client 914
Defining WCF Service Contracts 917
Data Contracts 918
Service Contracts 918
Operation Contracts 919
Message Contracts 920
Fault Contracts 920
Self-Hosted WCF Services 925
Summary 933
Chapter 27: Windows Workflow Foundation 935
Hello World 936
Workflows and Activities 937
If Activity 938
While Activity 938
Sequence Activity 938
Arguments and Variables 939
Custom Activities 944
Workflow Extensions 946
Activity Validation 952
Activity Designers 953
Summary 955
Appendix A: Exercise Solutions 957
Index 1009
Über den Autor
Karli Watson is an author and a consultant for Infusion.
Christian Nagel is an author, Microsoft Regional Director, and software architect with more than 20 years of experience.
Jacob Hammer Pedersen is an author and senior application developer at Elbek & Vejrup, Denmark.
Jon D. Reid is Software Engineering Manager at Metrix LLC.
Morgan Skinner is a consultant for Microsoft UK.
Wrox Beginning guides are crafted to make learning programming languages and technologies easier than you think, providing a structured, tutorial format that will guide you through all the techniques involved.