C# 6.0 Cookbook: Solutions for C# Developers

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

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.C# 6.0 Cookbook offers a new recipes for asynchronous methods, dynamic objects, enhanced error handling, the Rosyln compiler, and more. Here are some of topics covered: Classes and generics Collections, enumerators, and iterators Data types LINQ and Lambda expressions Exception handling Reflection and dynamic programming Regular expressions Filesystem interactions Networking and the Web XML usage Threading, Synchronization, and Concurrency Each recipe in the book includes tested code that you can download from oreilly.com and reuse in your own applications, and each one includes a detailed discussion of how and why the underling technology works. You don't have to be an experienced C# or .NET developer to use C# 6.0 Cookbook. You just have to be someone who wants to solve a problem now, without having to learn all the related theory first.

Author(s): Jay Hilyard; Stephen Teilhet
Edition: 4
Publisher: O'Reilly Media
Year: 2015

Language: English
Pages: 600

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