Programming C# 4.0

( 1 )


With its support for dynamic programming, C# 4.0 continues to evolve as a versatile language on its own. But when C# is used with .NET Framework 4, the combination is incredibly powerful. This bestselling tutorial shows you how to build web, desktop, and rich Internet applications using C# 4.0 with .NET's database capabilities, UI framework (WPF), extensive communication services (WCF), and more.

In this sixth edition, .NET experts Ian Griffiths, Matthew Adams, and Jesse Liberty...

See more details below
Paperback (6th ed.)
$39.56 price
(Save 28%)$54.99 List Price
Other sellers (Paperback)
  • All (26) from $1.99   
  • New (11) from $23.00   
  • Used (15) from $1.99   
Programming C# 4.0: Building Windows, Web, and RIA Applications for the .NET 4.0 Framework

Available on NOOK devices and apps  
  • NOOK Devices
  • Samsung Galaxy Tab 4 NOOK 7.0
  • Samsung Galaxy Tab 4 NOOK 10.1
  • NOOK HD Tablet
  • NOOK HD+ Tablet
  • NOOK eReaders
  • NOOK Color
  • NOOK Tablet
  • Tablet/Phone
  • NOOK for Windows 8 Tablet
  • NOOK for iOS
  • NOOK for Android
  • NOOK Kids for iPad
  • PC/Mac
  • NOOK for Windows 8
  • NOOK for PC
  • NOOK for Mac
  • NOOK for Web

Want a NOOK? Explore Now

NOOK Book (eBook)
$25.49 price
(Save 42%)$43.99 List Price


With its support for dynamic programming, C# 4.0 continues to evolve as a versatile language on its own. But when C# is used with .NET Framework 4, the combination is incredibly powerful. This bestselling tutorial shows you how to build web, desktop, and rich Internet applications using C# 4.0 with .NET's database capabilities, UI framework (WPF), extensive communication services (WCF), and more.

In this sixth edition, .NET experts Ian Griffiths, Matthew Adams, and Jesse Liberty cover the latest enhancements to C#, as well as the fundamentals of both the language and framework. You'll learn concurrent programming with C# 4.0, and how to use .NET tools such as the Entity Framework for easier data access, and the Silverlight platform for browser-based RIA development.

  • Learn C# fundamentals, such as variables, flow control, loops, and methods
  • Build complex programs with object-oriented and functional programming techniques
  • Process large collections of data with the native query features in LINQ
  • Communicate across networks with Windows Communication Foundation (WCF)
  • Learn the advantages of C# 4.0's dynamic language features
  • Build interactive Windows applications with Windows Presentation Foundation (WPF)
  • Create rich web applications with Silverlight and ASP.NET
Read More Show Less

Product Details

  • ISBN-13: 9780596159832
  • Publisher: O'Reilly Media, Incorporated
  • Publication date: 8/25/2010
  • Series: Animal Guide Series
  • Edition description: 6th ed.
  • Edition number: 1
  • Pages: 830
  • Sales rank: 679,961
  • Product dimensions: 6.90 (w) x 9.10 (h) x 1.60 (d)

Meet the Author

Ian Griffiths is an independent WPF consultant, developer, speaker and Pluralsight instructor and a widely recognized expert on the subject. He lives in London but can often be found on various developer mailing lists and newsgroups, where a popular sport is to see who can get him to write the longest email in reply to the shortest possible question. Ian maintains a popular blog at and is co-author of "Windows Forms in a Nutshell" and of "Mastering Visual Studio .NET".

Matthew is the Director of Development at Digital Healthcare Ltd. The last three years have kept him fully occupied in the development of a C#/.NET-based distributed imaging platform for healthcare applications. Before that, he studied Natural Sciences at Cambridge University, worked on banking and imaging applications in North America, became a fully-paid-up C++ junkie, and was the lead architect on software solutions for drug-discovery for a large US corporation. He thinks that .NET is a major philosophical stride forward for the computer industry: so much so that he almost doesn't miss his first love - generics - in C#. He has written articles and given papers on the subject to both technical and non-technical audiences, and looks forward to the day when he doesn't have to answer the question 'So, what is .NET?' any more!

Jesse Liberty, "Silverlight Geek", is a senior program manager for Microsoft Silverlight in the Silverlight Development Division where he is responsible for the creation of tutorials, videos and other content to facilitate the learning and use of Silverlight.

Even before joining Microsoft, Jesse is well known in the industry in part because of his many bestselling books, including O'Reilly Media's Programming .NET 3.5, Programming C# 3.0, Learning ASP.NET with AJAX and the soon to be published Programming Silverlight. He has over two decades experience writing software, consulting and training, with stints as at AT&T as a Distinguished Software Engineer and at Citibank as a Vice President in the Information Division.

Read More Show Less

Table of Contents


1 Introducing C# 1

Why C#? Why .NET? 1

The .NET Framework Class Library 2

Language Style 3

Composability 4

Managed Code 5

Continuity and the Windows Ecosystem 6

C# 4.0, .NET 4, and Visual Studio 2010 7

Summary 9

2 Basic Programming Techniques 11

Getting Started 11

Namespaces and Types 14

Projects and Solutions 19

Comments, Regions, and Readability 24

Bad Comments 26

XML Documentation Comments 26

Variables 28

Variable Types 28

Expressions and Statements 35

Assignment Statements 38

Increment and Decrement Operators 38

Flow Control with Selection Statements 39

if Statements 40

switch and case Statements 45

Iteration Statements 47

foreach Statements 48

for Statements 50

while and do Statements 52

Breaking Out of a Loop 53

Methods 55

Summary 58

3 Abstracting Ideas with Classes and Structs 59

Divide and Conquer 59

Abstracting Ideas with Methods 59

Abstracting Ideas with Objects and Classes 62

Defining Classes 64

Representing State with Properties 64

Protection Levels 66

Initializing with a Constructor 68

Fields: A Place to Put Data 72

Fields Can Be Fickle, but const Is Forever 75

Read-only Fields and Properties 76

Related Constants with enum 79

Value Types and Reference Types 82

Too Many Constructors, Mr. Mozart 88

Overloading 88

Overloaded Methods and Default Named Parameters 89

Object Initializers 92

Defining Methods 95

Declaring Static Methods 98

Static Fields and Properties 99

Static Constructors 101

Summary 102

4 Extensibility and Polymorphism 103

Association Through Composition and Aggregation 104

Inheritance and Polymorphism 106

Replacing Methods in Derived Classes 109

Hiding Base Members with new 109

Replacing Methods with virtual and override 112

Inheritance and Protection 114

Calling Base Class Methods 116

Thus Far and No Farther: sealed 118

Requiring Overrides with abstract 121

All Types Are Derived from Object 127

Boxing and Unboxing Value Types 127

C# Does Not Support Multiple Inheritance of Implementation 132

C# Supports Multiple Inheritance of Interface 132

Deriving Interfaces from Other Interfaces 135

Explicit Interface Implementation 136

The Last Resort: Checking Types at Runtime 141

Summary 142

5 Composability and Extensibility with Delegates 143

Functional Composition with delegate 150

Generic Actions with Action <T> 156

Generic Predicates with Predicate <T> 160

Using Anonymous Methods 162

Creating Delegates with Lambda Expressions 163

Delegates in Properties 165

Generic Delegates for Functions 167

Notifying Clients with Events 171

Exposing Large Numbers of Events 180

Summary 183

6 Dealing with Errors 185

When and How to Fail 191

Returning Error Values 194

Debugging with Return Values 200

Exceptions 201

Handling Exceptions 207

When Do finally Blocks Run? 214

Deciding What to Catch 215

Custom Exceptions 218

Summary 220

7 Arrays and Lists 221

Arrays 221

Construction and Initialization 222

Custom Types in Arrays 225

Array Members 230

Array Size 236

List<T< 243

Custom Indexers 247

Finding and Sorting 253

Collections and Polymorphism 254

Creating Your Own IEnumerable<T< 258

Summary 264

8 LINQ 265

Query Expressions 265

Query Expressions Versus Method Calls 267

Extension Methods and LING 268

let Clauses 271

LINQ Concepts and Techniques 271

Delegates and Lambdas 271

Functional Style and Composition 273

Deferred Execution 274

LINQ Operators 275

Filtering 275

Ordering 276

Concatenation 279

Grouping 280

Projections 282

Zipping 288

Getting Selective 289

Testing the Whole Collection 291

Aggregation 292

Set Operations 294

Joining 295

Conversions 296

Summary 297

9 Collection Classes 299

Dictionaries 299

Common Dictionary Uses 301

IDictionary<TKey, TValue> 308

Dictionaries and LINQ 309

HashSet and SortedSet 310

Queues 311

Linked Lists 312

Stacks 313

Summary 314

10 Strings 315

What Is a String? 316

The String and Char Types 317

Literal Strings and Chars 318

Escaping Special Characters 319

Formatting Data for Output 322

Standard Numeric Format Strings 323

Custom Numeric Format Strings 329

Dates and Times 332

Going the Other Way: Converting Strings to Other Types 336

Composite Formatting with String.Format 337

Culture Sensitivity 338

Exploring Formatting Rules 340

Accessing Characters by Index 341

Strings Are Immutable 341

Getting a Range of Characters 343

Composing Strings 344

Splitting It Up Again 346

Upper- and Lowercase 347

Manipulating Text 348

Mutable Strings with StringBuilder 349

Finding and Replacing Content 353

All Sorts of "Empty" Strings 355

Trimming Whitespace 357

Checking Character Types 360

Encoding Characters 360

Why Encodings Matter 362

Encoding and Decoding 363

Why Represent Strings As Byte Sequences? 370

Summary 370

11 Files and Streams 371

Inspecting Directories and Files 371

Examining Directories 374

Manipulating File Paths 375

Path and the Current Working Directory 376

Examining File Information 377

Creating Temporary Files 381

Deleting Files 381

Well-Known Folders 383

Concatenating Path Elements Safely 387

Creating and Securing Directory Hierarchies 388

Deleting a Directory 394

Writing Text Files 396

Writing a Whole Text File at Once 396

Writing Text with a StreamWriter 397

When Files Go Bad: Dealing with Exceptions 400

Finding and Modifying Permissions 404

Reading Files into Memory 409

Streams 413

Moving Around in a Stream 419

Writing Data with Streams 421

Reading, Writing, and Locking Files 422

FileStream Constructors 423

Stream Buffers 423

Setting Permissions During Construction 424

Setting Advanced Options 425

Asynchronous File Operations 425

Isolated Storage 428

Stores 429

Reading and Writing Text 430

Defining "Isolated" 431

Managing User Storage with Quotas 436

Managing Isolated Storage 436

Streams That Aren't Files 439

An Adapting Stream: CryptoStream 443

In Memory Alone: The MemoryStream 444

Representing Binary As Text with Base64 Encoding 444

Summary 447

12 XML 449

XML Basics (A Quick Review) 449

Elements 450


X Stands for eXtensible 452

Creating XML Documents 452

XML Elements 455

XML Attributes 456

Putting the LINQ in LINQ to XML 459

Searching in XML with LINQ 461

Searching for a Single Node 465

Search Axes 466

Where Clauses 466

XML Serialization 467

Customizing XML Serialization Using Attributes 469

Summary 471

13 Networking 473

Choosing a Networking Technology 473

Web Application with Client-Side Code 474

.NET Client and .NET Server 477

.NET Client and External Party Web Service 479

External Client and .NET Web Service 480

WCF 481

Creating a WCF Project 481

WCF Contracts 482

WCF Test Client and Host 483

Hosting a WCF Service 486

Writing a WCF Client 493

Bidirectional Communication with Duplex Contracts 501

HTTP 511

WebClient 512

WebRequest and WebResponse 516

Sockets 522

IP, IPv6, and TCP 523

Connecting to Services with the Socket Class 528

Implementing Services with the Socket Class 531

Other Networking Features 536

Summary 537

14 Databases 539

The .NET Data Access Landscape 539

Classic ADO.NET 540

LINQ and Databases 544

Non-Microsoft Data Access Technologies 545

WCF Data Services 546

Silverlight and Data Access 546

Databases 547

The Entity Data Model 548

Generated Code 551

Changing the Mapping 554

Relationships 555

Inheritance 562

Queries 563

LINQ to Entities 563

Entity SQL 568

Mixing ESQL and LINQ 570

The EntityClient ADO.NET Provider 571

Object Context 571

Connection Handling 571

Creating, Updating, and Deleting 574

Transactions 576

Optimistic Concurrency 581

Context and Entity Lifetime 583

WCF Data Services 584

Summary 588

15 Assemblies 589

.NET Components: Assemblies 589

References 590

Writing Libraries 593

Protection 595

Naming 598

Signing and Strong Names 599

Loading 601

Loading from the Application Folder 602

Loading from the GAC 603

Loading from a Silverlight .xap File 603

Explicit Loading 604

Summary 605

16 Threads and Asynchronous Code 607

Threads 609

Threads and the OS Scheduler 611

The Stack 613

The Thread Pool 620

Thread Affinity and Context 622

Common Thread Misconceptions 623

Multithreaded Coding Is Hard 629

Multithreading Survival Strategies 632

Synchronization Primitives 634

Monitor 634

Other Lock Types 645

Other Coordination Mechanisms 649

Events 649

Countdown 650

BlockingCollection 650

Asynchronous Programming 651

The Asynchronous Programming Model 652

The Event-Based Asynchronous Pattern 655

Ad Hoc Asynchrony 656

The Task Parallel Library 656

Tasks 657

Cancellation 663

Error Handling 665

Data Parallelism 666

Parallel For and ForEach 667

PLINQ: Parallel LINQ 669

Summary 670

17 Attributes and Reflection 671

Attributes 671

Types of Attributes 672

Custom Attributes 673

Reflection 677

Inspecting Metadata 678

Type Discovery 679

Reflecting on a Specific Type 681

Late Binding 683

Summary 686

18 Dynamic 687

Static Versus Dynamic 687

The Dynamic Style and COM Automation 689

The dynamic Type 690

Object Types and dynamic 693

dynamic in Noninterop Scenarios? 703

Summary 706

19 Interop with COM and Win32 707

Importing ActiveX Controls 707

Importing a Control in .NET 708

Interop Assemblies 711

No PIA 712

64-bit Versus 32-bit 713

P/Invoke 716

Pointers 720

C# 4.0 Interop Syntax Enhancements 725

Indexed Properties 725

Optional ref 726

Summary 727

20 WPF and Silverlight 729

Xaml and Code Behind 731

Xaml and Objects 735

Elements and Controls 738

Layout Panels 739

Graphical Elements 748

Controls 755

User Controls 760

Control Templates 761

Styles 764

The Visual State Manager 766

Data Binding 767

Data Templates 769

Summary 773

21 Programming ASP.NET Applications 775

Web Forms Fundamentals 775

Web Forms Events 776

Web Forms Life Cycle 778

Creating a Web Application 779

Code-Behind Files 780

Adding Controls 781

Server Controls 783

Data Binding 784

Examining the Code 789

Adding Controls and Events 790

Summary 794

22 Windows Forms 795

Creating the Application 796

Read More Show Less

Customer Reviews

Average Rating 5
( 1 )
Rating Distribution

5 Star


4 Star


3 Star


2 Star


1 Star


Your Rating:

Your Name: Create a Pen Name or

Barnes & Review Rules

Our reader reviews allow you to share your comments on titles you liked, or didn't, with others. By submitting an online review, you are representing to Barnes & that all information contained in your review is original and accurate in all respects, and that the submission of such content by you and the posting of such content by Barnes & does not and will not violate the rights of any third party. Please follow the rules below to help ensure that your review can be posted.

Reviews by Our Customers Under the Age of 13

We highly value and respect everyone's opinion concerning the titles we offer. However, we cannot allow persons under the age of 13 to have accounts at or to post customer reviews. Please see our Terms of Use for more details.

What to exclude from your review:

Please do not write about reviews, commentary, or information posted on the product page. If you see any errors in the information on the product page, please send us an email.

Reviews should not contain any of the following:

  • - HTML tags, profanity, obscenities, vulgarities, or comments that defame anyone
  • - Time-sensitive information such as tour dates, signings, lectures, etc.
  • - Single-word reviews. Other people will read your review to discover why you liked or didn't like the title. Be descriptive.
  • - Comments focusing on the author or that may ruin the ending for others
  • - Phone numbers, addresses, URLs
  • - Pricing and availability information or alternative ordering information
  • - Advertisements or commercial solicitation


  • - By submitting a review, you grant to Barnes & and its sublicensees the royalty-free, perpetual, irrevocable right and license to use the review in accordance with the Barnes & Terms of Use.
  • - Barnes & reserves the right not to post any review -- particularly those that do not follow the terms and conditions of these Rules. Barnes & also reserves the right to remove any review at any time without notice.
  • - See Terms of Use for other conditions and disclaimers.
Search for Products You'd Like to Recommend

Recommend other products that relate to your review. Just search for them below and share!

Create a Pen Name

Your Pen Name is your unique identity on It will appear on the reviews you write and other website activities. Your Pen Name cannot be edited, changed or deleted once submitted.

Your Pen Name can be any combination of alphanumeric characters (plus - and _), and must be at least two characters long.

Continue Anonymously
Sort by: Showing 1 Customer Reviews
  • Posted March 8, 2011

    Highly Recommended

    This is a great Reference book, if you are a vb programmer (like me) and want to transfer to C# this is the right book, it explains, everything! in detail from basic Data Types to Interop with COM and Win32, going through Databases, WCF,RIA, and lots more, there is a chapter dedicated to explain XLM and LINQ to XML, it was very helpful for me, and it is going to save you a lot of time if you are developing asp dotnet applications.

    Was this review helpful? Yes  No   Report this review
Sort by: Showing 1 Customer Reviews

If you find inappropriate content, please report it to Barnes & Noble
Why is this product inappropriate?
Comments (optional)