You are previewing Pro C# 5.0 and the .NET 4.5 Framework, Sixth Edition.
O'Reilly logo
Pro C# 5.0 and the .NET 4.5 Framework, Sixth Edition

Book Description

This new edition of Pro C# 5.0 and the .NET 4.5 Platform has been completely revised and rewritten to reflect the latest changes to the C# language specification and new advances in the .NET Framework. You'll find new chapters covering all the important new features that make .NET 4.5 the most comprehensive release yet, including:

  • .NET APIs for Metro-style apps

  • An improved Managed Extesibility Framework (MEF)

  • New asynchronous task-based model for async operations

  • How HTML5 support is being wrapped into C# web applications

  • New programming interfaces for HTTP applications, including improved IPv6 support

  • Expanded WPF, WCF and WF libraries giving C# more power than ever before

This comes on top of award winning coverage of core C# features, both old and new, that have made the previous editions of this book so popular (you'll find everything from generics to pLINQ covered here).

The mission of this text is to provide you with a rock–solid foundation in the C# programming language and the core aspects of the .NET platform (assemblies, remoting, Windows Forms, Web Forms, Metro, ADO.NET, XML web services, etc.). Once you digest the information presented in these 25 chapters, you'll be in a perfect position to apply this knowledge to your specific programming assignments, and you'll be well equipped to explore the .NET universe on your own terms.

What you'll learn

  • Be the first to understand the .NET 4.5 platform and Visual C# 2012.

  • Discover the ins and outs of the leading .NET technology.

  • Learn from an award-winning author who has been teaching the .NET world since version 1.0.

  • Find complete coverage of the XAML, .NET 4.5 and Visual Studio 12 together with discussion of Metro programming and the new Windows Runtime.

Who this book is for

If you're checking out this book for the first time, do understand that it's targeted at experienced software professionals or graduate students of computer science (so don't expect three chapters on iteration or decision constructs!).

It is perfect for anyone who is interested in the new .NET Framework 4.5 and the C# language. Whether you are moving to .NET for the first time or are already writing applications using previous .NET versions, this book will provide you with a comprehensive grounding in the new technology and serve as a complete reference throughout your coding career.

Table of Contents

  1. Title
  2. Dedication
  3. Contents at a Glance
  4. Contents
  5. About the Author
  6. About the Technical Reviewer
  7. Acknowledgments
  8. Introduction
  9. Part I: Introducing C# and .NET Platform
    1. Chapter 1: The Philosophy of .NET
      1. An Initial Look at the .NET Platform
      2. Introducing the Building Blocks of the .NET Platform (the CLR, CTS, and CLS)
      3. Additional .NET-Aware Programming Languages
      4. An Overview of .NET Assemblies
      5. Understanding the Common Type System
      6. Understanding the Common Language Specification
      7. Understanding the Common Language Runtime
      8. The Assembly/Namespace/Type Distinction
      9. Exploring an Assembly Using ildasm.exe
      10. The Platform-Independent Nature of .NET
      11. A Brief Word Regarding Windows 8 Metro-Style Applications
      12. Summary
    2. Chapter 2: Building C# Applications
      1. The Role of the .NET Framework 4.5 SDK
      2. Building C# Applications Using csc.exe
      3. Building .NET Applications Using Notepad++
      4. Building .NET Applications Using SharpDevelop
      5. Building .NET Applications Using Visual C# Express
      6. Building .NET Applications Using Visual Studio
      7. Summary
  10. Part II: Core C# Programming
    1. Chapter 3: Core C# Programming Constructs, Part I
      1. The Anatomy of a Simple C# Program
      2. An Interesting Aside: Some Additional Members of the System.Environment Class
      3. The System.Console Class
      4. System Data Types and Corresponding C# Keywords
      5. Working with String Data
      6. Narrowing and Widening Data Type Conversions
      7. Understanding Implicitly Typed Local Variables
      8. C# Iteration Constructs
      9. Decision Constructs and the Relational/Equality Operators
      10. Summary
    2. Chapter 4: Core C# Programming Constructs, Part II
      1. Methods and Parameter Modifiers
      2. Understanding C# Arrays
      3. Understanding the enum Type
      4. Understanding the Structure Type
      5. Understanding Value Types and Reference Types
      6. Understanding C# Nullable Types
      7. Summary
  11. Part III: Object-Oriented Programming with C#
    1. Chapter 5: Understanding Encapsulation
      1. Introducing the C# Class Type
      2. Understanding Constructors
      3. The Role of the this Keyword
      4. Understanding the static Keyword
      5. Defining the Pillars of OOP
      6. C# Access Modifiers
      7. The First Pillar: C#’s Encapsulation Services
      8. Understanding Automatic Properties
      9. Understanding Object Initialization Syntax
      10. Working with Constant Field Data
      11. Understanding Partial Types
      12. Summary
    2. Chapter 6: Understanding Inheritance and Polymorphism
      1. The Basic Mechanics of Inheritance
      2. Revising Visual Studio Class Diagrams
      3. The Second Pillar of OOP: The Details of Inheritance
      4. Programming for Containment/Delegation
      5. The Third Pillar of OOP: C#’s Polymorphic Support
      6. Understanding Base Class/Derived Class Casting Rules
      7. The Master Parent Class: System.Object
      8. Testing Your Modified Person Class
      9. Summary
    3. Chapter 7: Understanding Structured Exception Handling
      1. Ode to Errors, Bugs, and Exceptions
      2. The Role of .NET Exception Handling
      3. The Simplest Possible Example
      4. Configuring the State of an Exception
      5. System-Level Exceptions (System.SystemException)
      6. Application-Level Exceptions (System.ApplicationException)
      7. Processing Multiple Exceptions
      8. Who Is Throwing What?
      9. The Result of Unhandled Exceptions
      10. Debugging Unhandled Exceptions Using Visual Studio
      11. Summary
    4. Chapter 8: Working with Interfaces
      1. Understanding Interface Types
      2. Defining Custom Interfaces
      3. Implementing an Interface
      4. Invoking Interface Members at the Object Level
      5. Interfaces As Parameters
      6. Interfaces As Return Values
      7. Arrays of Interface Types
      8. Implementing Interfaces Using Visual Studio
      9. Explicit Interface Implementation
      10. Designing Interface Hierarchies
      11. The IEnumerable and IEnumerator Interfaces
      12. The ICloneable Interface
      13. The IComparable Interface
      14. Summary
  12. Part IV: Advanced C# Programming
    1. Chapter 9: Collections and Generics
      1. The Motivation for Collection Classes
      2. The Problems of Nongeneric Collections
      3. The Role of Generic Type Parameters
      4. The System.Collections.Generic Namespace
      5. The System.Collections.ObjectModel Namespace
      6. Creating Custom Generic Methods
      7. Creating Custom Generic Structures and Classes
      8. Constraining Type Parameters
      9. Summary
    2. Chapter 10: Delegates, Events, and Lambda Expressions
      1. Understanding the .NET Delegate Type
      2. The Simplest Possible Delegate Example
      3. Sending Object State Notifications Using Delegates
      4. Understanding Generic Delegates
      5. Understanding C# Events
      6. Understanding C# Anonymous Methods
      7. Understanding Lambda Expressions
      8. Summary
    3. Chapter 11: Advanced C# Language Features
      1. Understanding Indexer Methods
      2. Understanding Operator Overloading
      3. Understanding Custom Type Conversions
      4. Understanding Extension Methods
      5. Understanding Anonymous Types
      6. Working with Pointer Types
      7. Summary
    4. Chapter 12: LINQ to Objects
      1. LINQ-Specific Programming Constructs
      2. Understanding the Role of LINQ
      3. Applying LINQ Queries to Primitive Arrays
      4. Returning the Result of a LINQ Query
      5. Applying LINQ Queries to Collection Objects
      6. Investigating the C# LINQ Query Operators
      7. The Internal Representation of LINQ Query Statements
      8. Summary
    5. Chapter 13: Understanding Object Lifetime
      1. Classes, Objects, and References
      2. The Basics of Object Lifetime
      3. The Role of Application Roots
      4. Understanding Object Generations
      5. Concurrent Garbage Collection Under .NET 1.0–3.5
      6. Background Garbage Collection Under .NET 4.0 and Greater
      7. The System.GC Type
      8. Building Finalizable Objects
      9. Building Disposable Objects
      10. Building Finalizable and Disposable Types
      11. Understanding Lazy Object Instantiation
      12. Summary
  13. Part V: Programming with .NET Assemblies
    1. Chapter 14: Building and Configuring Class Libraries
      1. Defining Custom Namespaces
      2. The Role of .NET Assemblies
      3. Understanding the Format of a .NET Assembly
      4. Building and Consuming Custom Class Library
      5. Understanding Private Assemblies
      6. Understanding Shared Assemblies
      7. Consuming a Shared Assembly
      8. Configuring Shared Assemblies
      9. Understanding Publisher Policy Assemblies
      10. Understanding the <codeBase> Element
      11. The System.Configuration Namespace
      12. The Configuration File Schema Documentation
      13. Summary
    2. Chapter 15: Type Reflection, Late Binding, and Attribute-Based Programming
      1. The Necessity of Type Metadata
      2. Understanding Reflection
      3. Building a Custom Metadata Viewer
      4. Dynamically Loading Assemblies
      5. Reflecting on Shared Assemblies
      6. Understanding Late Binding
      7. Understanding the Role of .NET Attributes
      8. Building Custom Attributes
      9. Assembly-Level Attributes
      10. Reflecting on Attributes Using Early Binding
      11. Reflecting on Attributes Using Late Binding
      12. Putting Reflection, Late Binding, and Custom Attributes in Perspective
      13. Building an Extendable Application
      14. Summary
    3. Chapter 16: Dynamic Types and the Dynamic Language Runtime
      1. The Role of the C# dynamic Keyword
      2. The Role of the Dynamic Language Runtime (DLR)
      3. Simplifying Late-Bound Calls Using Dynamic Types
      4. Simplifying COM Interoperability Using Dynamic Data
      5. COM Interop Using C# Dynamic Data
      6. Summary
    4. Chapter 17: Processes, AppDomains, and Object Contexts
      1. The Role of a Windows Process
      2. Interacting with Processes Under the .NET Platform
      3. Understanding .NET Application Domains
      4. Interacting with the Default Application Domain
      5. Creating New Application Domains
      6. Understanding Object Context Boundaries
      7. Summarizing Processes, AppDomains, and Context
      8. Summary
    5. Chapter 18: Understanding CIL and the Role of Dynamic Assemblies
      1. Reasons for Learning the Grammar of CIL
      2. Examining CIL Directives, Attributes, and Opcodes
      3. Pushing and Popping: The Stack-Based Nature of CIL
      4. Understanding Round-Trip Engineering
      5. Understanding CIL Directives and Attributes
      6. .NET Base Class Library, C#, and CIL Data Type Mappings
      7. Defining Type Members in CIL
      8. Examining CIL Opcodes
      9. Building a .NET Assembly with CIL
      10. Understanding Dynamic Assemblies
      11. Summary
  14. Part VI: Introducing the .NET Base Class Libraries
    1. Chapter 19: Multithreaded, Parallel, and Async Programming
      1. The Process/AppDomain/Context/Thread Relationship
      2. A Brief Review of the .NET Delegate
      3. The Asynchronous Nature of Delegates
      4. Invoking a Method Asynchronously
      5. The System.Threading Namespace
      6. The System.Threading.Thread Class
      7. Manually Creating Secondary Threads
      8. The Issue of Concurrency
      9. Programming with Timer Callbacks
      10. Understanding the CLR ThreadPool
      11. Parallel Programming Using the Task Parallel Library
      12. Parallel LINQ Queries (PLINQ)
      13. Asynchronous Calls Under .NET 4.5
      14. Summary
    2. Chapter 20: File I/O and Object Serialization
      1. Exploring the System.IO Namespace
      2. The Directory(Info) and File(Info) Types
      3. Working with the DirectoryInfo Type
      4. Working with the Directory Type
      5. Working with the DriveInfo Class Type
      6. Working with the FileInfo Class
      7. Working with the File Type
      8. The Abstract Stream Class
      9. Working with StreamWriters and StreamReaders
      10. Working with StringWriters and StringReaders
      11. Working with BinaryWriters and BinaryReaders
      12. Watching Files Programmatically
      13. Understanding Object Serialization
      14. Configuring Objects for Serialization
      15. Choosing a Serialization Formatter
      16. Serializing Objects Using the BinaryFormatter
      17. Serializing Objects Using the SoapFormatter
      18. Serializing Objects Using the XmlSerializer
      19. Serializing Collections of Objects
      20. Customizing the Soap/Binary Serialization Process
      21. Summary
    3. Chapter 21: ADO.NET Part I: The Connected Layer
      1. A High-Level Definition of ADO.NET
      2. Understanding ADO.NET Data Providers
      3. Additional ADO.NET Namespaces
      4. The Types of the System.Data Namespace
      5. Abstracting Data Providers Using Interfaces
      6. Creating the AutoLot Database
      7. The ADO.NET Data Provider Factory Model
      8. Understanding the Connected Layer of ADO.NET
      9. Working with Data Readers
      10. Building a Reusable Data Access Library
      11. Creating a Console UI–Based Front End
      12. Understanding Database Transactions
      13. Summary
    4. Chapter 22: ADO.NET Part II: The Disconnected Layer
      1. Understanding the Disconnected Layer of ADO.NET
      2. Understanding the Role of the DataSet
      3. Working with DataColumns
      4. Working with DataRows
      5. Working with DataTables
      6. Binding DataTable Objects to Windows Forms GUIs
      7. Working with Data Adapters
      8. Adding Disconnection Functionality to AutoLotDAL.dll
      9. Multitabled DataSet Objects and Data Relationships
      10. The Windows Forms Database Designer Tools
      11. Isolating Strongly Typed Database Code into a Class Library
      12. Programming with LINQ to DataSet
      13. Summary
    5. Chapter 23: ADO.NET Part III: The Entity Framework
      1. Understanding the Role of Entity Framework
      2. Building and Analyzing Your First EDM
      3. Programming Against the Conceptual Model
      4. AutoLotDAL Version Four, Now with Entities
      5. Data Binding Entities to Windows Forms GUIs
      6. Going Forward with .NET Data-Access APIs
      7. Summary
    6. Chapter 24: Introducing LINQ to XML
      1. A Tale of Two XML APIs
      2. Members of the System.Xml.Linq Namespace
      3. Working with XElement and XDocument
      4. Manipulating an In-Memory XML Document
      5. Summary
    7. Chapter 25: Introducing Windows Communication Foundation
      1. A Potpourri of Distributed Computing APIs
      2. The Role of WCF
      3. Investigating the Core WCF Assemblies
      4. The Visual Studio WCF Project Templates
      5. The Basic Composition of a WCF Application
      6. The ABCs of WCF
      7. Building a WCF Service
      8. Hosting the WCF Service
      9. Building the WCF Client Application
      10. Simplifying Configuration Settings
      11. Using the WCF Service Library Project Template
      12. Hosting the WCF Service Within a Windows Service
      13. Invoking a Service Asynchronously from the Client
      14. Designing WCF Data Contracts
      15. Summary
    8. Chapter 26: Introducing Windows Workflow Foundation
      1. Defining a Business Process
      2. Building a Simple Workflow
      3. The Workflow Runtime
      4. Examining the Workflow Activities
      5. Building a Flowchart Workflow
      6. Building a Sequence Workflow (in a Dedicated DLL)
      7. Consuming the Workflow Library
      8. Summary
  15. Part VII: Windows Presentation Foundation
    1. Chapter 27: Introducing Windows Presentation Foundation and XAML
      1. The Motivation Behind WPF
      2. The Various Flavors of WPF
      3. Investigating the WPF Assemblies
      4. Building a WPF Application Without XAML
      5. Building a WPF Application Using Only XAML
      6. Transforming Markup into a .NET Assembly
      7. Understanding the Syntax of WPF XAML
      8. Building a WPF Application Using Code-Behind Files
      9. Building WPF Applications Using Visual Studio
      10. Building a Custom XAML Editor with Visual Studio
      11. Summary
    2. Chapter 28: Programming with WPF Controls
      1. A Survey of the Core WPF Controls
      2. A Brief Review of the Visual Studio WPF Designer
      3. Controlling Content Layout Using Panels
      4. Building a Window’s Frame Using Nested Panels
      5. Understanding WPF Commands
      6. A Deeper Look at WPF APIs and Controls
      7. Building the Ink API Tab
      8. Introducing the Documents API
      9. Building the Documents Tab
      10. Introducing the WPF Data-Binding Model
      11. Summary
    3. Chapter 29: WPF Graphics Rendering Services
      1. Understanding WPF’s Graphical Rendering Services
      2. Rendering Graphical Data Using Shapes
      3. WPF Brushes and Pens
      4. Applying Graphical Transformations
      5. Working with the Visual Studio Transform Editor
      6. Rendering Graphical Data Using Drawings and Geometries
      7. The Role of Expression Design
      8. Rendering Graphical Data Using the Visual Layer
      9. Summary
    4. Chapter 30: WPF Resources, Animations, and Styles
      1. Understanding the WPF Resource System
      2. Working with Object (Logical) Resources
      3. Understanding WPF’s Animation Services
      4. Authoring Animations in XAML
      5. Understanding the Role of WPF Styles
      6. Summary
    5. Chapter 31: Dependency Properties, Routed Events, and Templates
      1. Understanding the Role of Dependency Properties
      2. Building a Custom Dependency Property
      3. Understanding Routed Events
      4. Logical Trees, Visual Trees, and Default Templates
      5. Building a Control Template with the Trigger Framework
      6. Summary
  16. Part VIII: ASP.NET Web Forms
    1. Chapter 32: Introducing ASP.NET Web Forms
      1. The Role of HTTP
      2. Understanding Web Applications and Web Servers
      3. The Role of HTML
      4. The Role of Client-Side Scripting
      5. Posting Back to the Web Server
      6. An Overview of the ASP.NET API
      7. Building a Single-File ASP.NET Web Page
      8. Building an ASP.NET Web Page Using Code Files
      9. ASP.NET Web Sites vs. ASP.NET Web Applications
      10. The ASP.NET Web Site Directory Structure
      11. The Inheritance Chain of the Page Type
      12. Interacting with the Incoming HTTP Request
      13. Interacting with the Outgoing HTTP Response
      14. The Life Cycle of an ASP.NET Web Page
      15. The Role of the web.config File
      16. Summary
    2. Chapter 33: ASP.NET Web Controls, Master Pages, and Themes
      1. Understanding the Nature of Web Controls
      2. The Control and WebControl Base Classes
      3. Major Categories of ASP.NET Web Controls
      4. Building the ASP.NET Cars Web Site
      5. The Role of the Validation Controls
      6. Working with Themes
      7. Summary
    3. Chapter 34: ASP.NET State Management Techniques
      1. The Issue of State
      2. ASP.NET State Management Techniques
      3. Understanding the Role of ASP.NET View State
      4. The Role of the Global.asax File
      5. Understanding the Application/Session Distinction
      6. Working with the Application Cache
      7. Maintaining Session Data
      8. Understanding Cookies
      9. The Role of the <sessionState> Element
      10. Introducing the ASP.NET Profile API
      11. Summary
  17. Index