C# 4.0 The Complete Reference

Book description

The Definitive Guide to C#

“From using LINQ to covariance and from string formatting to optional arguments, Herb’s update covers all you need to know about using real-world C# 4.0.” —Michael Howard, Microsoft Corporation

Bestselling author Herb Schildt has updated and expanded his classic programming reference to cover C# 4.0--the latest release of this powerful, feature-rich language. Using carefully crafted explanations, insider tips, and hundreds of examples, C# 4.0: The Complete Reference offers in-depth details on all aspects of C#, including keywords, syntax, and core libraries. New C# 4.0 features, such as named and optional arguments, the dynamic type, expanded covariance and contravariance capabilities, and support for parallel programming with the Task Parallel Library (TPL) and PLINQ are also discussed.

Perfect for beginners and professional C# programmers alike, this expert resource is written in the clear, crisp, uncompromising style that has made Herb the choice of millions worldwide. Begin mastering C# 4.0 today with help from this authoritative volume!

Coverage includes:

  • Data types and operators
  • Control statements
  • Classes and objects
  • Constructors, destructors, and methods
  • Interfaces, arrays, enumerations, and structures
  • Method and operator overloading
  • Inheritance and virtual methods
  • Reflection and runtime type ID
  • Exception handling
  • Delegates, properties, events, and indexers
  • Attributes
  • Multithreading and the Task Parallel Library
  • Generics
  • LINQ and PLINQ
  • Lambda expressions
  • Anonymous types
  • Extension methods
  • Named and optional arguments
  • Implicitly typed variables
  • I/O, networking, and collections
  • The preprocessor and much, much more

Table of contents

  1. Cover Page
  2. C# 4.0: The Complete Reference
  3. Copyright Page
  4. Contents
  5. Special Thanks
  6. Preface
  7. Part I The C# Language
    1. 1 The Creation of C#
      1. C#’s Family Tree
        1. C: The Beginning of the Modern Age of Programming
        2. The Creation of OOP and C++
        3. The Internet and Java Emerge
        4. The Creation of C#
        5. The Evolution of C#
      2. How C# Relates to the .NET Framework
        1. What Is the .NET Framework?
      3. How the Common Language Runtime Works
      4. Managed vs. Unmanaged Code
        1. The Common Language Specification
    2. 2 An Overview of C#
      1. Object-Oriented Programming
        1. Encapsulation
        2. Polymorphism
        3. Inheritance
      2. A First Simple Program
        1. Using csc.exe, the C# Command-Line Compiler
        2. Using the Visual Studio IDE
        3. The First Sample Program, Line by Line
      3. Handling Syntax Errors
      4. A Small Variation
      5. A Second Simple Program
      6. Another Data Type
      7. Two Control Statements
        1. The if Statement
        2. The for Loop
      8. Using Code Blocks
      9. Semicolons, Positioning, and Indentation
      10. The C# Keywords
      11. Identifiers
      12. The .NET Framework Class Library
    3. 3 Data Types, Literals, and Variables
      1. Why Data Types Are Important
      2. C#’s Value Types
      3. Integers
      4. Floating-Point Types
      5. The decimal Type
      6. Characters
      7. The bool Type
      8. Some Output Options
      9. Literals
        1. Hexadecimal Literals
        2. Character Escape Sequences
        3. String Literals
      10. A Closer Look at Variables
        1. Initializing a Variable
        2. Dynamic Initialization
        3. Implicitly Typed Variables
      11. The Scope and Lifetime of Variables
      12. Type Conversion and Casting
        1. Automatic Conversions
        2. Casting Incompatible Types
      13. Type Conversion in Expressions
        1. Using Casts in Expressions
    4. 4 Operators
      1. Arithmetic Operators
        1. Increment and Decrement
      2. Relational and Logical Operators
        1. Short-Circuit Logical Operators
      3. The Assignment Operator
        1. Compound Assignments
      4. The Bitwise Operators
        1. The Bitwise AND, OR, XOR, and NOT Operators
        2. The Shift Operators
        3. Bitwise Compound Assignments
      5. The ? Operator
      6. Spacing and Parentheses
      7. Operator Precedence
    5. 5 Program Control Statements
      1. The if Statement
        1. Nested ifs
        2. The if-else-if Ladder
      2. The switch Statement
        1. Nested switch Statements
      3. The for Loop
        1. Some Variations on the for Loop
      4. The while Loop
      5. The do-while Loop
      6. The foreach Loop
      7. Using break to Exit a Loop
      8. Using continue
        1. return
      9. The goto
    6. 6 Introducing Classes and Objects
      1. Class Fundamentals
        1. The General Form of a Class
        2. Define a Class
      2. How Objects Are Created
      3. Reference Variables and Assignment
      4. Methods
        1. Add a Method to the Building Class
        2. Return from a Method
        3. Return a Value
        4. Use Parameters
        5. Add a Parameterized Method to Building
        6. Avoiding Unreachable Code
      5. Constructors
        1. Parameterized Constructors
        2. Add a Constructor to the Building Class
      6. The new Operator Revisited
        1. Using new with Value Types
      7. Garbage Collection and Destructors
        1. Destructors
      8. The this Keyword
    7. 7 Arrays and Strings
      1. Arrays
        1. One-Dimensional Arrays
      2. Multidimensional Arrays
        1. Two-Dimensional Arrays
        2. Arrays of Three or More Dimensions
        3. Initializing Multidimensional Arrays
      3. Jagged Arrays
      4. Assigning Array References
      5. Using the Length Property
        1. Using Length with Jagged Arrays
      6. Implicitly Typed Arrays
      7. The foreach Loop
      8. Strings
        1. Constructing Strings
        2. Operating on Strings
        3. Arrays of Strings
        4. Strings Are Immutable
        5. Strings Can Be Used in switch Statements
    8. 8 A Closer Look at Methods and Classes
      1. Controlling Access to Class Members
        1. C#’s Access Modifiers
        2. Applying Public and Private Access
        3. Controlling Access: A Case Study
      2. Pass References to Methods
        1. How Arguments Are Passed
      3. Use ref and out Parameters
        1. Use ref
        2. Use out
        3. Use ref and out on References
      4. Use a Variable Number of Arguments
      5. Return Objects
        1. Return an Array
      6. Method Overloading
      7. Overload Constructors
        1. Invoke an Overloaded Constructor Through this
      8. Object Initializers
      9. Optional Arguments
        1. Optional Arguments vs. Overloading
        2. Optional Arguments and Ambiguity
        3. A Practical Example of Optional Arguments
      10. Named Arguments
      11. The Main( ) Method
        1. Return Values from Main( )
        2. Pass Arguments to Main( )
      12. Recursion
      13. Understanding static
        1. Static Constructors
      14. Static Classes
    9. 9 Operator Overloading
      1. Operator Overloading Fundamentals
        1. Overloading Binary Operators
        2. Overloading Unary Operators
      2. Handling Operations on C# Built-in Types
      3. Overloading the Relational Operators
      4. Overloading true and false
      5. Overloading the Logical Operators
        1. A Simple Approach to Overloading the Logical Operators
        2. Enabling the Short-Circuit Operators
      6. Conversion Operators
      7. Operator Overloading Tips and Restrictions
      8. Another Example of Operator Overloading
    10. 10 Indexers and Properties
      1. Indexers
        1. Creating One-Dimensional Indexers
        2. Indexers Can Be Overloaded
        3. Indexers Do Not Require an Underlying Array
        4. Multidimensional Indexers
      2. Properties
        1. Auto-Implemented Properties
        2. Use Object Initializers with Properties
        3. Property Restrictions
      3. Use Access Modifiers with Accessors
      4. Using Indexers and Properties
    11. 11 Inheritance
      1. Inheritance Basics
      2. Member Access and Inheritance
        1. Using Protected Access
      3. Constructors and Inheritance
        1. Calling Base Class Constructors
      4. Inheritance and Name Hiding
        1. Using base to Access a Hidden Name
      5. Creating a Multilevel Hierarchy
      6. When Are Constructors Called?
      7. Base Class References and Derived Objects
      8. Virtual Methods and Overriding
        1. Why Overridden Methods?
        2. Applying Virtual Methods
      9. Using Abstract Classes
      10. Using sealed to Prevent Inheritance
      11. The object Class
        1. Boxing and Unboxing
        2. Is object a Universal Data Type?
    12. 12 Interfaces, Structures, and Enumerations
      1. Interfaces
        1. Implementing Interfaces
      2. Using Interface References
      3. Interface Properties
      4. Interface Indexers
      5. Interfaces Can Be Inherited
      6. Name Hiding with Interface Inheritance
      7. Explicit Implementations
      8. Choosing Between an Interface and an Abstract Class
      9. The .NET Standard Interfaces
      10. Structures
        1. Why Structures?
      11. Enumerations
        1. Initialize an Enumeration
        2. Specify the Underlying Type of an Enumeration
        3. Use Enumerations
    13. 13 Exception Handling
      1. The System.Exception Class
      2. Exception-Handling Fundamentals
        1. Using try and catch
        2. A Simple Exception Example
        3. A Second Exception Example
      3. The Consequences of an Uncaught Exception
      4. Exceptions Let You Handle Errors Gracefully
      5. Using Multiple catch Clauses
      6. Catching All Exceptions
      7. Nesting try Blocks
      8. Throwing an Exception
        1. Rethrowing an Exception
      9. Using finally
      10. A Closer Look at the Exception Class
        1. Commonly Used Exceptions
      11. Deriving Exception Classes
      12. Catching Derived Class Exceptions
      13. Using checked and unchecked
    14. 14 Using I/O
      1. C#’s I/O Is Built Upon Streams
        1. Byte Streams and Character Streams
        2. The Predefined Streams
      2. The Stream Classes
        1. The Stream Class
        2. The Byte Stream Classes
        3. The Character Stream Wrapper Classes
        4. Binary Streams
      3. Console I/O
        1. Reading Console Input
        2. Using ReadKey( )
        3. Writing Console Output
      4. FileStream and Byte-Oriented File I/O
        1. Opening and Closing a File
        2. Reading Bytes from a FileStream
        3. Writing to a File
        4. Using FileStream to Copy a File
      5. Character-Based File I/O
        1. Using StreamWriter
        2. Using a StreamReader
      6. Redirecting the Standard Streams
      7. Reading and Writing Binary Data
        1. BinaryWriter
        2. BinaryReader
        3. Demonstrating Binary I/O
      8. Random Access Files
      9. Using MemoryStream
      10. Using StringReader and StringWriter
      11. The File Class
        1. Using Copy( ) to Copy a File
        2. Using Exists( ) and GetLastAccessTime( )
      12. Converting Numeric Strings to Their Internal Representation
    15. 15 Delegates, Events, and Lambda Expressions
      1. Delegates
        1. Delegate Method Group Conversion
        2. Using Instance Methods as Delegates
        3. Multicasting
        4. Covariance and Contravariance
        5. System.Delegate
        6. Why Delegates
      2. Anonymous Functions
      3. Anonymous Methods
        1. Pass Arguments to an Anonymous Method
        2. Return a Value from an Anonymous Method
        3. Use Outer Variables with Anonymous Methods
      4. Lambda Expressions
        1. The Lambda Operator
        2. Expression Lambdas
        3. Statement Lambdas
      5. Events
        1. A Multicast Event Example
        2. Instance Methods vs. Static Methods as Event Handlers
        3. Using Event Accessors
        4. Miscellaneous Event Features
      6. Use Anonymous Methods and Lambda Expressions with Events
      7. .NET Event Guidelines
        1. Use EventHandler<TEventArgs> and EventHandler
      8. Applying Events: A Case Study
    16. 16 Namespaces, the Preprocessor, and Assemblies
      1. Namespaces
        1. Declaring a Namespace
        2. Namespaces Prevent Name Conflicts
        3. using
        4. A Second Form of using
        5. Namespaces Are Additive
        6. Namespaces Can Be Nested
        7. The Global Namespace
        8. Using the :: Namespace Alias Qualifier
      2. The Preprocessor
        1. #define
        2. #if and #endif
        3. #else and #elif
        4. #undef
        5. #error
        6. #warning
        7. #line
        8. #region and #endregion
        9. #pragma
      3. Assemblies and the internal Access Modifier
        1. The internal Access Modifier
    17. 17 Runtime Type ID, Reflection, and Attributes
      1. Runtime Type Identification
        1. Testing a Type with is
        2. Using as
        3. Using typeof
      2. Reflection
        1. The Reflection Core: System.Type
      3. Using Reflection
        1. Obtaining Information About Methods
        2. Calling Methods Using Reflection
        3. Obtaining a Type’s Constructors
        4. Obtaining Types from Assemblies
        5. Fully Automating Type Discovery
      4. Attributes
        1. Attribute Basics
        2. Positional vs. Named Parameters
      5. Three Built-in Attributes
        1. AttributeUsage
        2. The Conditional Attribute
        3. The Obsolete Attribute
    18. 18 Generics
      1. What Are Generics?
      2. A Simple Generics Example
        1. Generic Types Differ Based on Their Type Arguments
        2. How Generics Improve Type Safety
      3. A Generic Class with Two Type Parameters
      4. The General Form of a Generic Class
      5. Constrained Types
        1. Using a Base Class Constraint
        2. Using an Interface Constraint
        3. Using the new( ) Constructor Constraint
        4. The Reference Type and Value Type Constraints
        5. Using a Constraint to Establish a Relationship Between Two Type Parameters
        6. Using Multiple Constraints
      6. Creating a Default Value of a Type Parameter
      7. Generic Structures
      8. Creating a Generic Method
        1. Using Explicit Type Arguments to Call a Generic Method
        2. Using a Constraint with a Generic Method
      9. Generic Delegates
      10. Generic Interfaces
      11. Comparing Instances of a Type Parameter
      12. Generic Class Hierarchies
        1. Using a Generic Base Class
        2. A Generic Derived Class
      13. Overriding Virtual Methods in a Generic Class
      14. Overloading Methods That Use Type Parameters
      15. Covariance and Contravariance in Generic Type Parameters
        1. Using Covariance in a Generic Interface
        2. Using Contravariance in a Generic Interface
        3. Variant Delegates
      16. How Generic Types Are Instantiated
      17. Some Generic Restrictions
      18. Final Thoughts on Generics
    19. 19 LINQ
      1. LINQ Fundamentals
        1. A Simple Query
        2. A Query Can Be Executed More Than Once
        3. How the Data Types in a Query Relate
        4. The General Form of a Query
      2. Filter Values with where
      3. Sort Results with orderby
      4. A Closer Look at select
      5. Use Nested from Clauses
      6. Group Results with group
      7. Use into to Create a Continuation
      8. Use let to Create a Variable in a Query
      9. Join Two Sequences with join
      10. Anonymous Types
      11. Create a Group Join
      12. The Query Methods
        1. The Basic Query Methods
        2. Create Queries by Using the Query Methods
        3. Query Syntax vs. Query Methods
        4. More Query-Related Extension Methods
      13. Deferred vs. Immediate Query Execution
      14. Expression Trees
      15. Extension Methods
      16. PLINQ
    20. 20 Unsafe Code, Pointers, Nullable Types, Dynamic Types, and Miscellaneous Topics
      1. Unsafe Code
        1. Pointer Basics
        2. Using unsafe
        3. Using fixed
        4. Accessing Structure Members Through a Pointer
        5. Pointer Arithmetic
        6. Pointer Comparisons
        7. Pointers and Arrays
        8. Pointers and Strings
        9. Multiple Indirection
        10. Arrays of Pointers
        11. stackalloc
        12. Creating Fixed-Size Buffers
      2. Nullable Types
        1. Nullable Basics
        2. Nullable Objects in Expressions
        3. The ?? Operator
        4. Nullable Objects and the Relational and Logical Operators
      3. Partial Types
      4. Partial Methods
      5. Create a Dynamic Type with dynamic
      6. COM Interoperability
      7. Friend Assemblies
      8. Miscellaneous Keywords
        1. lock
        2. readonly
        3. const and volatile
        4. The using Statement
        5. extern
  8. Part II Exploring the C# Library
    1. 21 Exploring the System Namespace
      1. The Members of System
      2. The Math Class
      3. The .NET Structures Corresponding to the Built-in Value Types
        1. The Integer Structures
        2. The Floating-Point Structures
        3. Decimal
        4. Char
        5. The Boolean Structure
      4. The Array Class
        1. Sorting and Searching Arrays
        2. Reversing an Array
        3. Copying an Array
        4. Using a Predicate
        5. Using an Action
      5. BitConverter
      6. Generating Random Numbers with Random
      7. Memory Management and the GC Class
      8. Object
      9. Tuple
      10. The IComparable and IComparable<T> Interfaces
      11. The IEquatable<T> Interface
      12. The IConvertible Interface
      13. The ICloneable Interface
      14. IFormatProvider and IFormattable
      15. IObservable<T> and IObserver<T>
    2. 22 Strings and Formatting
      1. Strings in C#
      2. The String Class
        1. The String Constructors
        2. The String Field, Indexer, and Property
        3. The String Operators
        4. The String Methods
        5. Padding and Trimming Strings
        6. Inserting, Removing, and Replacing
        7. Changing Case
        8. Using the Substring( ) Method
        9. The String Extension Methods
      3. Formatting
        1. Formatting Overview
        2. The Numeric Format Specifiers
        3. Understanding Argument Numbers
      4. Using String.Format( ) and ToString( ) to Format Data
        1. Using String.Format( ) to Format Values
        2. Using ToString( ) to Format Data
      5. Creating a Custom Numeric Format
        1. The Custom Format Placeholder Characters
      6. Formatting Date and Time
        1. Creating a Custom Date and Time Format
        2. Formatting Time Spans
      7. Formatting Enumerations
    3. 23 Multithreaded Programming, Part One
      1. Multithreading Fundamentals
      2. The Thread Class
        1. Creating and Starting a Thread
        2. Some Simple Improvements
        3. Creating Multiple Threads
      3. Determining When a Thread Ends
      4. Passing an Argument to a Thread
      5. The IsBackground Property
      6. Thread Priorities
      7. Synchronization
        1. An Alternative Approach
        2. The Monitor Class and lock
      8. Thread Communication Using Wait( ), Pulse( ), and PulseAll( )
        1. An Example That Uses Wait( ) and Pulse( )
      9. Deadlock and Race Conditions
      10. Using MethodImplAttribute
      11. Using a Mutex and a Semaphore
        1. The Mutex
        2. The Semaphore
      12. Using Events
      13. The Interlocked Class
      14. Synchronization Classes Added by .NET 4.0
      15. Terminating a Thread Via Abort( )
        1. An Abort( ) Alternative
        2. Canceling Abort( )
      16. Suspending and Resuming a Thread
      17. Determining a Thread’s State
      18. Using the Main Thread
      19. Additional Multithreading Features Added by .NET 4.0
      20. Multithreading Tips
      21. Starting a Separate Task
    4. 24 Multithreading, Part Two: Exploring the Task Parallel Library and PLINQ
      1. Two Approaches to Parallel Programming
      2. The Task Class
        1. Creating a Task
        2. Use a Task ID
      3. Using Wait Methods
        1. Calling Dispose( )
      4. Using TaskFactory to Start a Task
      5. Use a Lambda Expression as a Task
      6. Create a Task Continuation
      7. Returning a Value from a Task
      8. Cancelling a Task and Using AggregateException
      9. Some Other Task Features
      10. The Parallel Class
        1. Parallelizing Tasks via Invoke( )
        2. Using the For( ) Method
        3. Using the ForEach( ) Method
      11. Exploring PLINQ
        1. ParallelEnumerable
        2. Parallelizing a Query with AsParallel( )
        3. Using AsOrdered( )
        4. Cancelling a Parallel Query
        5. Other PLINQ Features
        6. PLINQ Efficiency Concerns
    5. 25 Collections, Enumerators, and Iterators
      1. Collections Overview
      2. The Non-Generic Collections
        1. The Non-Generic Interfaces
        2. The DictionaryEntry Structure
        3. The Non-Generic Collection Classes
      3. Storing Bits with BitArray
      4. The Specialized Collections
      5. The Generic Collections
        1. The Generic Interfaces
        2. The KeyValuePair<TKey, TValue> Structure
        3. The Generic Collection Classes
      6. The Concurrent Collections
      7. Storing User-Defined Classes in Collections
      8. Implementing IComparable
        1. Implementing IComparable for Non-Generic Collections
        2. Implementing IComparable<T> for Generic Collections
      9. Using an IComparer
        1. Using a Non-Generic IComparer
        2. Using a Generic IComparer<T>
      10. Using StringComparer
      11. Accessing a Collection via an Enumerator
        1. Using an Enumerator
        2. Using IDictionaryEnumerator
      12. Implementing IEnumerable and IEnumerator
      13. Using Iterators
        1. Stopping an Iterator
        2. Using Multiple yield Directives
        3. Creating a Named Iterator
        4. Creating a Generic Iterator
      14. Collection Initializers
    6. 26 Networking Through the Internet Using System.Net
      1. The System.Net Members
      2. Uniform Resource Identifiers
      3. Internet Access Fundamentals
        1. WebRequest
        2. WebResponse
        3. HttpWebRequest and HttpWebResponse
        4. A Simple First Example
      4. Handling Network Errors
        1. Exceptions Generated by Create( )
        2. Exceptions Generated by GetReponse( )
        3. Exceptions Generated by GetResponseStream( )
        4. Using Exception Handling
      5. The URI Class
      6. Accessing Additional HTTP Response Information
        1. Accessing the Header
        2. Accessing Cookies
        3. Using the LastModified Property
      7. MiniCrawler: A Case Study
      8. Using WebClient
  9. A Documentation Comment Quick Reference
    1. The XML Comment Tags
    2. Compiling Documentation Comments
    3. An XML Documentation Example
  10. Index

Product information

  • Title: C# 4.0 The Complete Reference
  • Author(s): Herbert Schildt
  • Release date: April 2010
  • Publisher(s): McGraw-Hill
  • ISBN: 9780071741170