You are previewing C# 6.0 Cookbook, 4th Edition.
O'Reilly logo
C# 6.0 Cookbook, 4th Edition

Book Description

Completely updated for C# 6.0, the new edition of this bestseller offers more than 150 code recipes to common and not-so-common problems that C# programmers face every day. More than a third of the recipes have been rewritten to take advantage of new C# 6.0 features. If you prefer solutions to general C# language instruction and quick answers to theory, this is your book.

Table of Contents

  1. Preface
    1. Who This Book Is For
    2. What You Need to Use This Book
    3. Platform Notes
    4. How This Book Is Organized
    5. What Was Left Out
    6. Conventions Used in This Book
    7. About the Code
    8. Using Code Examples
    9. Safari® Books Online
    10. How to Contact Us
    11. Acknowledgments
  2. Classes and Generics
    1. 1.0. Introduction
    2. 1.1. Creating Union-Type Structures
    3. 1.2. Making a Type Sortable
    4. 1.3. Making a Type Searchable
    5. 1.4. Returning Multiple Items from a Method
    6. 1.5. Parsing Command-Line Parameters
    7. 1.6. Initializing a Constant Field at Runtime
    8. 1.7. Building Cloneable Classes
    9. 1.8. Ensuring an Object’s Disposal
    10. 1.9. Deciding When and Where to Use Generics
    11. 1.10. Understanding Generic Types
    12. 1.11. Reversing the Contents of a Sorted List
    13. 1.12. Constraining Type Arguments
    14. 1.13. Initializing Generic Variables to Their Default Values
    15. 1.14. Adding Hooks to Generated Entities
    16. 1.15. Controlling How a Delegate Fires Within a Multicast Delegate
    17. 1.16. Using Closures in C#
    18. 1.17. Performing Multiple Operations on a List Using Functors
    19. 1.18. Controlling Struct Field Initialization
    20. 1.19. Checking for null in a More Concise Way
  3. Collections, Enumerators, and Iterators
    1. 2.0. Introduction
    2. 2.1. Looking for Duplicate Items in a List<T>
    3. 2.2. Keeping Your List<T> Sorted
    4. 2.3. Sorting a Dictionary’s Keys and/or Values
    5. 2.4. Creating a Dictionary with Min and Max Value Boundaries
    6. 2.5. Persisting a Collection Between Application Sessions
    7. 2.6. Testing Every Element in an Array or List<T>
    8. 2.7. Creating Custom Enumerators
    9. 2.8. Dealing with finally Blocks and Iterators
    10. 2.9. Implementing Nested foreach Functionality in a Class
    11. 2.10. Using a Thread-Safe Dictionary for Concurrent Access Without Manual Locking
  4. Data Types
    1. 3.0. Introduction
    2. 3.1. Encoding Binary Data as Base64
    3. 3.2. Decoding a Base64-Encoded Binary
    4. 3.3. Converting a String Returned as a Byte[] Back into a String
    5. 3.4. Passing a String to a Method That Accepts Only a Byte[]
    6. 3.5. Determining Whether a String Is a Valid Number
    7. 3.6. Rounding a Floating-Point Value
    8. 3.7. Choosing a Rounding Algorithm
    9. 3.8. Safely Performing a Narrowing Numeric Cast
    10. 3.9. Testing for a Valid Enumeration Value
    11. 3.10. Using Enumerated Members in a Bit Mask
    12. 3.11. Determining Whether One or More Enumeration Flags Are Set
  5. Language Integrated Query (LINQ) and Lambda Expressions
    1. 4.0. Introduction
    2. 4.1. Querying a Message Queue
    3. 4.2. Using Set Semantics with Data
    4. 4.3. Reusing Parameterized Queries with LINQ to SQL
    5. 4.4. Sorting Results in a Culture-Sensitive Manner
    6. 4.5. Adding Functional Extensions for Use with LINQ
    7. 4.6. Querying and Joining Across Data Repositories
    8. 4.7. Querying Configuration Files with LINQ
    9. 4.8. Creating XML Straight from a Database
    10. 4.9. Being Selective About Your Query Results
    11. 4.10. Using LINQ with Collections That Don’t Support IEnumerable<T>
    12. 4.11. Performing an Advanced Interface Search
    13. 4.12. Using Lambda Expressions
    14. 4.13. Using Different Parameter Modifiers in Lambda Expressions
    15. 4.14. Speeding Up LINQ Operations with Parallelism
  6. Debugging and Exception Handling
    1. 5.0. Introduction
    2. 5.1. Knowing When to Catch and Rethrow Exceptions
    3. 5.2. Handling Exceptions Thrown from Methods Invoked via Reflection
    4. 5.3. Creating a New Exception Type
    5. 5.4. Breaking on a First-Chance Exception
    6. 5.5. Handling Exceptions Thrown from an Asynchronous Delegate
    7. 5.6. Giving Exceptions the Extra Info They Need with Exception.Data
    8. 5.7. Dealing with Unhandled Exceptions in WinForms Applications
    9. 5.8. Dealing with Unhandled Exceptions in WPF Applications
    10. 5.9. Determining Whether a Process Has Stopped Responding
    11. 5.10. Using Event Logs in Your Application
    12. 5.11. Watching the Event Log for a Specific Entry
    13. 5.12. Implementing a Simple Performance Counter
    14. 5.13. Creating Custom Debugging Displays for Your Classes
    15. 5.14. Tracking Where Exceptions Come From
    16. 5.15. Handling Exceptions in Asynchronous Scenarios
    17. 5.16. Being Selective About Exception Processing
  7. Reflection and Dynamic Programming
    1. 6.0. Introduction
    2. 6.1. Listing Referenced Assemblies
    3. 6.2. Determining Type Characteristics in Assemblies
    4. 6.3. Determining Inheritance Characteristics
    5. 6.4. Invoking Members Using Reflection
    6. 6.5. Accessing Local Variable Information
    7. 6.6. Creating a Generic Type
    8. 6.7. Using dynamic Versus object
    9. 6.8. Building Objects Dynamically
    10. 6.9. Make Your Objects Extensible
  8. Regular Expressions
    1. 7.0. Introduction
    2. 7.1. Extracting Groups from a MatchCollection
    3. 7.2. Verifying the Syntax of a Regular Expression
    4. 7.3. Augmenting the Basic String Replacement Function
    5. 7.4. Implementing a Better Tokenizer
    6. 7.5. Returning the Entire Line in Which a Match Is Found
    7. 7.6. Finding a Particular Occurrence of a Match
    8. 7.7. Using Common Patterns
  9. Filesystem I/O
    1. 8.0. Introduction
    2. 8.1. Searching for Directories or Files Using Wildcards
    3. 8.2. Obtaining the Directory Tree
    4. 8.3. Parsing a Path
    5. 8.4. Launching and Interacting with Console Utilities
    6. 8.5. Locking Subsections of a File
    7. 8.6. Waiting for an Action to Occur in the Filesystem
    8. 8.7. Comparing Version Information of Two Executable Modules
    9. 8.8. Querying Information for All Drives on a System
    10. 8.9. Compressing and Decompressing Your Files
  10. Networking and Web
    1. 9.0. Introduction
    2. 9.1. Handling Web Server Errors
    3. 9.2. Communicating with a Web Server
    4. 9.3. Going Through a Proxy
    5. 9.4. Obtaining the HTML from a URL
    6. 9.5. Using the Web Browser Control
    7. 9.6. Prebuilding an ASP.NET Website Programmatically
    8. 9.7. Escaping and Unescaping Data for the Web
    9. 9.8. Checking Out a Web Server’s Custom Error Pages
    10. 9.9. Writing a TCP Server
    11. 9.10. Writing a TCP Client
    12. 9.11. Simulating Form Execution
    13. 9.12. Transferring Data via HTTP
    14. 9.13. Using Named Pipes to Communicate
    15. 9.14. Pinging Programmatically
    16. 9.15. Sending SMTP Mail Using the SMTP Service
    17. 9.16. Using Sockets to Scan the Ports on a Machine
    18. 9.17. Using the Current Internet Connection Settings
    19. 9.18. Transferring Files Using FTP
  11. XML
    1. 10.0. Introduction
    2. 10.1. Reading and Accessing XML Data in Document Order
    3. 10.2. Querying the Contents of an XML Document
    4. 10.3. Validating XML
    5. 10.4. Detecting Changes to an XML Document
    6. 10.5. Handling Invalid Characters in an XML String
    7. 10.6. Transforming XML
    8. 10.7. Validating Modified XML Documents Without Reloading
    9. 10.8. Extending Transformations
    10. 10.9. Getting Your Schemas in Bulk from Existing XML Files
    11. 10.10. Passing Parameters to Transformations
  12. Security
    1. 11.0. Introduction
    2. 11.1. Encrypting and Decrypting a String
    3. 11.2. Encrypting and Decrypting a File
    4. 11.3. Cleaning Up Cryptography Information
    5. 11.4. Preventing String Tampering in Transit or at Rest
    6. 11.5. Making a Security Assert Safe
    7. 11.6. Verifying That an Assembly Has Been Granted Specific Permissions
    8. 11.7. Minimizing the Attack Surface of an Assembly
    9. 11.8. Obtaining Security and/or Audit Information
    10. 11.9. Granting or Revoking Access to a File or Registry Key
    11. 11.10. Protecting String Data with Secure Strings
    12. 11.11. Securing Stream Data
    13. 11.12. Encrypting web.config Information
    14. 11.13. Obtaining a Safer File Handle
    15. 11.14. Storing Passwords
  13. Threading, Synchronization, <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" class="keep-together">and Concurrency</span>
    1. 12.0. Introduction
    2. 12.1. Creating Per-Thread Static Fields
    3. 12.2. Providing Thread-Safe Access to Class Members
    4. 12.3. Preventing Silent Thread Termination
    5. 12.4. Being Notified of the Completion of an Asynchronous Delegate
    6. 12.5. Storing Thread-Specific Data Privately
    7. 12.6. Granting Multiple Access to Resources with a Semaphore
    8. 12.7. Synchronizing Multiple Processes with the Mutex
    9. 12.8. Using Events to Make Threads Cooperate
    10. 12.9. Performing Atomic Operations Among Threads
    11. 12.10. Optimizing Read-Mostly Access
    12. 12.11. Making Your Database Requests More Scalable
    13. 12.12. Running Tasks in Order
  14. Toolbox
    1. 13.0. Introduction
    2. 13.1. Dealing with Operating System Shutdown, Power Management, or User Session Changes
    3. 13.2. Controlling a Service
    4. 13.3. List What Processes an Assembly Is Loaded In
    5. 13.4. Using Message Queues on a Local Workstation
    6. 13.5. Capturing Output from the Standard Output Stream
    7. 13.6. Capturing Standard Output for a Process
    8. 13.7. Running Code in Its Own AppDomain
    9. 13.8. Determining the Operating System and Service Pack Version of the Current Operating System
  15. Index