Step-by-step beginner’s guide to Visual C# 2012
Written for novice programmers who want to learn programming with C# and the .NET framework, this book offers programming basics such as variables, flow control, and object oriented programming. It then moves into web and Windows programming and data access (databases and XML). The authors focus on the tool that beginners use most often to program C#, the Visual C# 2012 development environment in Visual Studio 2012.
- Puts the spotlight on key beginning level topics with easy-to-follow instructions for Microsoft Visual C# 2012
- Explores how to program for variables, expressions, flow control, and functions
- Explains the debugging process and error handling as well as object oriented programming, and much more
Beginning Microsoft Visual C# 2012 Programming offers beginners a guide to writing effective programming code following simple step-by-step methods, each followed by the opportunity to try out newly acquired skills.
İçerik tablosu
INTRODUCTION xxxi
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 4
CIL and JIT 4
Assemblies 5
Managed Code 5
Garbage Collection 6
Fitting It Together 6
Linking 7
What Is C#? 7
Applications You Can Write with C# 8
C# in this Book 8
Visual Studio 2012 8
Visual Studio Express 2012 Products 9
Solutions 9
Summary 10
CHAPTER 2: WRITING A C# PROGRAM 13
The Visual Studio 2012 Development Environment 14
Console Applications 16
The Solution Explorer 18
The Properties Window 19
The Error List Window 19
Desktop Applications 20
Summary 23
CHAPTER 3: VARIABLES AND EXPRESSIONS 25
Basic C# Syntax 26
Basic C# Console Application Structure 28
Variables 29
Simple Types 29
Variable Naming 33
Naming Conventions 34
Literal Values 34
Variable Declaration and Assignment 36
Expressions 37
Mathematical Operators 37
Assignment Operators 41
Operator Precedence 42
Namespaces 42
Summary 45
CHAPTER 4: FLOW CONTROL 49
Boolean Logic 49
Boolean Assignment Operators 52
Bitwise Operators 53
Operator Precedence Updated 57
The goto Statement 58
Branching 59
The Ternary Operator 59
The if Statement 59
Checking More Conditions Using if Statements 62
The switch Statement 63
Looping 65
do Loops 66
while Loops 68
for Loops 70
Interrupting Loops 74
Infi nite Loops 75
Summary 75
CHAPTER 5: MORE ABOUT VARIABLES 79
Type Conversion 80
Implicit Conversions 80
Explicit Conversions 81
Explicit Conversions Using the Convert Commands 84
Complex Variable Types 86
Enumerations 87
Defi ning Enumerations 87
Structs 90
Defi ning Structs 90
Arrays 92
Declaring Arrays 93
foreach Loops 95
Multidimensional Arrays 96
Arrays of Arrays 97
String Manipulation 98
Summary 102
CHAPTER 6: FUNCTIONS 105
Defi ning and Using Functions 106
Return Values 108
Parameters 109
Parameter Matching 111
Parameter Arrays 111
Reference and Value Parameters 113
Out Parameters 115
Variable Scope 116
Variable Scope in Other Structures 118
Parameters and Return Values versus Global Data 120
The Main( ) Function 121
Struct Functions 123
Overloading Functions 124
Using Delegates 125
Summary 128
CHAPTER 7: DEBUGGING AND ERROR HANDLING 131
Debugging in Visual Studio 132
Debugging in Nonbreak (Normal) Mode 132
Outputting Debugging Information 133
Tracepoints 137
Diagnostics Output Versus Tracepoints 139
Debugging in Break Mode 140
Entering Break Mode 140
Monitoring Variable Content 142
Stepping Through Code 144
Immediate and Command Windows 146
The Call Stack Window 146
Error Handling 147
try…catch…finally 147
Listing and Configuring Exceptions 152
Notes on Exception Handling 152
Summary 153
CHAPTER 8: INTRODUCTION TO OBJECT-ORIENTED
PROGRAMMING 157
What Is Object-Oriented Programming? 158
What Is an Object? 158
Properties and Fields 159
Methods 160
Everything’s an Object 161
The Life Cycle of an Object 161
Constructors 161
Destructors 162
Static and Instance Class Members 162
Static Constructors 162
Static Classes 163
OOP Techniques 163
Interfaces 163
Disposable Objects 164
Inheritance 165
Polymorphism 167
Interface Polymorphism 168
Relationships Between Objects 168
Containment 168
Collections 169
Operator Overloading 169
Events 170
Reference Types Versus Value Types 170
OOP in Desktop Applications 171
Summary 174
CHAPTER 9: DEFINING CLASSES 177
Class Defi nitions in C# 177
Interface Defi nitions 180
System.Object 182
Constructors and Destructors 184
Constructor Execution Sequence 185
OOP Tools in Visual Studio 188
The Class View Window 188
The Object Browser 190
Adding Classes 191
Class Diagrams 192
Class Library Projects 193
Interfaces Versus Abstract Classes 196
Struct Types 198
Shallow Copying Versus Deep Copying 200
Summary 200
CHAPTER 10: DEFINING CLASS MEMBERS 203
Member Defi nitions 203
Defi ning Fields 204
Defi ning Methods 204
Defi ning Properties 205
Adding Members from a Class Diagram 210
Adding Methods 210
Adding Properties 212
Adding Fields 212
Refactoring Members 212
Automatic Properties 213
Additional Class Member Topics 213
Hiding Base Class Methods 213
Calling Overridden or Hidden Base Class Methods 215
The this Keyword 216
Using Nested Type Defi nitions 216
Interface Implementation 218
Implementing Interfaces in Classes 219
Explicit Interface Member Implementation 220
Additional Property Accessors 220
Partial Class Defi nitions 221
Partial Method Defi nitions 222
Example Application 224
Planning the Application 224
The Card Class 224
The Deck Class 224
Writing the Class Library 224
Adding the Suit and Rank Enumerations 225
Adding the Card Class 226
Adding the Deck Class 227
A Client Application for the Class Library 230
The Call Hierarchy Window 231
Summary 232
CHAPTER 11: COLLECTIONS, COMPARISONS, AND CONVERSIONS 235
Collections 236
Using Collections 236
Defi ning Collections 241
Indexers 242
Adding a Cards Collection to Card Lib 244
Keyed Collections and IDictionary 247
Iterators 248
Iterators and Collections 252
Deep Copying 253
Adding Deep Copying to Card Lib 255
Comparisons 256
Type Comparisons 256
Boxing and Unboxing 257
The is Operator 258
Value Comparisons 260
Operator Overloading 261
Adding Operator Overloads to Card Lib 265
The IComparable and IComparer Interfaces 269
Sorting Collections 271
Conversions 274
Overloading Conversion Operators 274
The as Operator 275
Summary 276
CHAPTER 12: GENERICS 279
What Are Generics? 280
Using Generics 281
Nullable Types 281
Operators and Nullable Types 282
The ?? Operator 283
The System.Collections.Generic Namespace 287
List 287
Sorting and Searching Generic Lists 289
Dictionary 294
Modifying Card Lib to Use a Generic Collection Class 295
Defi ning Generic Types 296
Defi ning Generic Classes 296
The default Keyword 298
Constraining Types 298
Inheriting from Generic Classes 304
Generic Operators 305
Generic Structs 306
Defi ning Generic Interfaces 306
Defi ning Generic Methods 306
Defi ning Generic Delegates 308
Variance 308
Covariance 309
Contravariance 310
Summary 310
CHAPTER 13: ADDITIONAL OOP TECHNIQUES 313
The :: Operator and the Global Namespace Qualifi er 313
Custom Exceptions 315
Adding Custom Exceptions to Card Lib 315
Events 316
What Is an Event? 316
Handling Events 317
Defi ning Events 319
Multipurpose Event Handlers 323
The Event Handler and Generic Event Handler Types 325
Return Values and Event Handlers 326
Anonymous Methods 326
Expanding and Using Card Lib 327
A Card Game Client for Card Lib 327
Attributes 334
Reading Attributes 334
Creating Attributes 335
Summary 336
CHAPTER 14: C# LANGUAGE ENHANCEMENTS 339
Initializers 340
Object Initializers 340
Collection Initializers 342
Type Inference 344
Anonymous Types 346
Dynamic Lookup 350
The dynamic Type 350
IDynamic Meta Object Provider 354
Advanced Method Parameters 354
Optional Parameters 354
Optional Parameter Values 355
The Optional Attribute Attribute 356
Optional Parameter Order 356
Named Parameters 356
Named and Optional Parameter Guidelines 360
Extension Methods 360
Lambda Expressions 364
Anonymous Methods Recap 364
Lambda Expressions for Anonymous Methods 365
Lambda Expression Parameters 368
Lambda Expression Statement Bodies 368
Lambda Expressions as Delegates and Expression Trees 369
Lambda Expressions and Collections 370
Caller Information Attributes 372
Summary 375
PART II: WINDOWS PROGRAMMING
CHAPTER 15: BASIC DESKTOP PROGRAMMING 381
XAML 382
Separation of Concerns 382
XAML in Action 383
Namespaces 383
Code-Behind Files 384
The Playground 384
WPF Controls 385
Properties 386
Dependency Properties 387
Attached Properties 388
Events 388
Handling Events 389
Routed Events 390
Routed Commands 390
Control Types 392
Control Layout 393
Stack Order 393
Alignment, Margins, Padding, and Dimensions 393
Border 394
Canvas 394
Dock Panel 395
Stack Panel 397
Wrap Panel 398
Grid 398
The Game Client 401
The About Window 401
Designing the User Interface 401
The Image Control 401
The Label Control 402
The Text Block Control 402
The Button Control 402
The Options Window 405
The Text Box Control 406
The Check Box Control 406
The Radio Button Control 407
The Combo Box Control 408
The Tab Control 409
Handling Events in the Options Window 411
Data Binding 413
The Data Context 414
Binding to Local Objects 414
Static Binding to External Objects 414
Dynamic Binding to External Objects 415
Starting a Game 418
The List Box Control 418
Summary 421
CHAPTER 16: ADVANCED DESKTOP PROGRAMMING 425
The Main Window 425
The Menu Control 426
Routed Commands with Menus 426
Creating and Styling Controls 429
Styles 430
Templates 430
Value Converters 434
The IValue Converter Interface 434
Value Conversion Attribute 434
Triggers 435
Animations 436
Timelines without Key Frames 436
Timelines with Key Frames 437
WPF User Controls 438
Implementing Dependency Properties 439
Putting It All Together 447
Refactoring the Domain Model 447
The View Models 451
Completing the Game 459
Summary 466
CHAPTER 17: WINDOWS STORE APPS 469
Getting Started 469
Windows 8 Developer License 470
How to Get the License 470
Windows Store Apps versus Desktop Applications 471
Developing Windows Store Apps 472
View Modes 472
Full-Screen Mode 473
Snapped Mode 473
Fill Mode 473
Screen Orientation 473
The Missing Caption Bar 473
Menus and Toolbars 473
The App Bar 474
Charms 475
Tiles and Badges 476
App Lifetime 476
Lock Screen Apps 476
App Development 476
WPF and Windows Store App XAML Diff erences 476
Namespace Changes 477
Eff ects 477
Opacity Mask 477
Style Triggers 477
Commands 478
Templates and Pages 478
Blank Page 478
Basic Page 478
Grouped Items and Group Details Pages 478
Item Details Page 479
Items and Split Pages 479
Sandboxed Apps 479
Disk Access 479
Serialization, Streams, and Async Programming 480
Navigation Between Pages 483
Managing State 485
Converting Karli Cards, Part 1 485
Creating the Card Lib Project 486
Converting the View Models 489
Visual Changes 493
Drop Shadow and Opacity Masks 493
Style Triggers 493
Converting User Controls 493
Common Elements of Windows Store Apps 499
The App Bar Control 500
The Settings Panel 501
Tiles, Badges, and Splash Screens 503
Converting Karli Cards, Part 2 504
The Windows Store 509
Checking the Store Requirements 509
Summary 510
CHAPTER 18: DEPLOYING DESKTOP APPLICATIONS 513
Deployment Overview 514
Click Once Deployment 514
Implementing Click Once Deployment 515
Installing the Application with Click Once 522
Creating and Using Updates of the Application 524
Install Shield Limited Edition 524
The Project Assistant 524
Step 1: Application Information 525
Step 2: Installation Requirements 525
Step 3: Installation Architecture 526
Step 4: Application Files 526
Step 5: Application Shortcuts 527
Step 6: Application Registry 528
Step 7: Installation Interview 528
Summary 530
PART III: WEB PROGRAMMING
CHAPTER 19: ASP.NET WEB PROGRAMMING 535
Overview of Web Applications 536
ASP.NET Runtime 536
Creating a Simple Page 536
Server Controls 544
ASP.NET Postback 545
ASP.NET AJAX Postback 550
Input Validation 553
State Management 557
Client-Side State Management 558
View State 558
Cookie 559
Server-Side State Management 560
Session 560
Application 561
Cache 562
Styles 562
Master Pages 566
Site Navigation 571
Authentication and Authorization 573
Authentication Confi guration 574
Using Security Controls 577
Reading from and Writing to a SQL Server Database 580
Summary 589
CHAPTER 20: DEPLOYING WEB APPLICATIONS 591
Internet Information Services 591
IIS Confi guration 592
Copying a Website 594
Publishing a Web Application 597
Summary 600
PART IV: DATA ACCESS
CHAPTER 21: FILE SYSTEM DATA 605
Streams 605
The Classes for Input and Output 606
The File and Directory Classes 607
The File Info Class 608
The Directory Info Class 610
Path Names and Relative Paths 611
The File Stream Object 611
File Position 612
Reading Data 613
Writing Data 615
The Stream Writer Object 617
The Stream Reader Object 618
Reading Data 620
Delimited Files 621
Asynchronous File Access 625
Reading and Writing Compressed Files 625
Serialized Objects 628
Monitoring the File System 632
Summary 636
CHAPTER 22: XML 639
XML Documents 640
XML Elements 640
Attributes 641
The XML Declaration 641
The Structure of an XML Document 642
XML Namespaces 642
Well-Formed and Valid XML 643
Validating XML Documents 644
Schemas 644
The XSD dialog box shown in the Xml Document Class 645
Using XML in Your Application 647
XML Document Object Model 647
The Xml Document Class 648
The Xml Element Class 648
Changing the Values of Nodes 652
Selecting Nodes 656
XPath 657
Summary 661
CHAPTER 23: INTRODUCTION TO LINQ 663
First LINQ Query 664
Declaring a Variable for Results Using the var Keyword 665
Specifying the Data Source: from Clause 666
Specify Condition: where Clause 666
Selecting Items: select Clause 666
Finishing Up: Using the foreach Loop 667
Deferred Query Execution 667
Using the LINQ Method Syntax 667
LINQ Extension Methods 667
Query Syntax versus Method Syntax 668
Ordering Query Results 669
Understanding the orderby Clause 670
Ordering Using Method Syntax 671
Querying a Large Data Set 672
Using Aggregate Operators 674
Querying Complex Objects 678
Projection: Creating New Objects in Queries 681
Projection: Method Syntax 682
Using the Select Distinct Query 683
Using the Any and All Methods 684
Ordering by Multiple Levels 685
Multi-Level Ordering Method Syntax: Then By 687
Using Group Queries 687
Using Take and Skip 689
Using First and First Or Default 691
Using the LINQ Set Operators 692
Using Joins 694
Summary 696
CHAPTER 24: APPLYING LINQ 699
LINQ Varieties 699
Using LINQ with Databases 700
Installing SQL Server and the Northwind Sample Data 700
Installing SQL Server Express 701
Installing the Northwind Sample Database 701
First LINQ to Database Query 701
Navigating Database Relationships 704
Using LINQ with XML 706
LINQ to XML Functional Constructors 707
Constructing XML Element Text with Strings 710
Saving and Loading an XML Document 710
Loading XML from a String 712
Contents of a Saved XML Document 713
Working with XML Fragments 713
Generating XML from Databases 715
How to Query an XML Document 717
Using LINQ to XML Query Members 718
Elements( ) 718
Descendants( ) 719
Attributes( ) 721
Summary 723
PART V: ADDITIONAL TECHNIQUES
CHAPTER 25: WINDOWS COMMUNICATION FOUNDATION 727
What Is WCF? 728
WCF Concepts 728
WCF Communication Protocols 729
Addresses, Endpoints, and Bindings 729
Contracts 731
Message Patterns 732
Behaviors 732
Hosting 732
WCF Programming 733
The WCF Test Client 740
Defi ning WCF Service Contracts 742
Data Contracts 743
Service Contracts 743
Operation Contracts 744
Message Contracts 745
Fault Contracts 745
Self-Hosted WCF Services 750
Summary 756
CHAPTER 26: WINDOWS WORKFLOW FOUNDATION 759
Hello World 759
Workfl ows and Activities 761
If Activity 761
While Activity 762
Sequence Activity 762
Arguments and Variables 763
Custom Activities 767
Workfl ow Extensions 769
Activity Validation 773
Activity Designers 774
Summary 776
APPENDIX A: EXERCISE SOLUTIONS 779
INDEX 827
Yazar hakkında
Karli Watson is an author and IT contractor in London.
Jacob Vibe Hammer is a software architect and developer at Kamstrup A/S, Denmark. He has coauthored several books.
Jon D. Reid is the director of systems engineering at Indigo Biosystems, Inc.
Morgan Skinner joined Microsoft in 2001.
Daniel Kemper is a software engineer specializing in reporting and rich client technologies.
Christian Nagel is a Microsoft Regional Director and Microsoft MVP, an associate of thinktecture, and founder of CN innovation.
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.