Create bookmark
C# Cookbook
350 Solutions for C# Programmers
Notes
Please login to add notes
- Table of Contents
- + Preface
-
+
Numbers and Enumerations
- 1.0 Introduction
- + 1.1 Determining Approximate Equality Between a Fraction and Floating-Point Value
- + 1.2 Converting Degrees to Radians
- + 1.3 Converting Radians to Degrees
- + 1.4 Using the Bitwise Complement Operator with Various Data Types
- + 1.5 Testing for an Even or Odd Value
- + 1.6 Obtaining the High Word or Low Word of a Number
- + 1.7 Converting a Number in Another Base to Base10
- + 1.8 Determining Whether a String Is a Valid Number
- + 1.9 Rounding a Floating-Point Value
- + 1.10 Choosing a Rounding Algorithm
- + 1.11 Converting Celsius to Fahrenheit
- + 1.12 Converting Fahrenheit to Celsius
- + 1.13 Safely Performing a Narrowing Numeric Cast
- + 1.14 Finding the Length of Any Three Sides of a Right Triangle
- + 1.15 Finding the Angles of a Right Triangle
- + 1.16 Displaying an Enumeration Value as a String
- + 1.17 Converting Plain Text to an Equivalent Enumeration Value
- + 1.18 Testing for a Valid Enumeration Value
- + 1.19 Testing for a Valid Enumeration of Flags
- + 1.20 Using Enumerated Members in a Bit Mask
- + 1.21 Determining if One or More Enumeration Flags Are Set
- + 1.22 Determining the Integral Part of a Decimal or Double
-
+
Strings and Characters
- 2.0 Introduction
- + 2.1 Determining the Kind of Character a char Contains
- + 2.2 Determining Whether a Character Is Within a Specified Range
- + 2.3 Controlling Case Sensitivity When Comparing Two Characters
- + 2.4 Finding All Occurrences of a Character Within a String
- + 2.5 Finding the Location of All Occurrences of a String Within Another String
- + 2.6 Implementing a Poor Man’s Tokenizer to Deconstruct a String
- + 2.7 Controlling Case Sensitivity When Comparing Two Strings
- + 2.8 Comparing a String to the Beginning or End of a Second String
- + 2.9 Inserting Text into a String
- + 2.10 Removing or Replacing Characters Within a String
- + 2.11 Encoding Binary Data as Base64
- + 2.12 Decoding a Base64-Encoded Binary
- + 2.13 Converting a String Returned as a Byte[ ] Back into a String
- + 2.14 Passing a String to a Method That Accepts only a Byte[ ]
- + 2.15 Converting Strings to Other Types
- + 2.16 Formatting Data in Strings
- + 2.17 Creating a Delimited String
- + 2.18 Extracting Items from a Delimited String
- + 2.19 Setting the Maximum Number of Characters a StringBuilder Can Contain
- + 2.20 Iterating over Each Character in a String
- + 2.21 Improving String Comparison Performance
- + 2.22 Improving StringBuilder Performance
- + 2.23 Pruning Characters from the Head and/or Tail of a String
- + 2.24 Testing a String for Null or Empty
- + 2.25 Appending a Line
- + 2.26 Encoding Chunks of Data
-
+
Classes and Structures
- 3.0 Introduction
- + 3.1 Creating Union-Type Structures
- + 3.2 Allowing a Type to Represent Itself as a String
- + 3.3 Converting a String Representation of an Object into an Actual Object
- + 3.4 Implementing Polymorphism with Abstract Base Classes
- + 3.5 Making a Type Sortable
- + 3.6 Making a Type Searchable
- + 3.7 Indirectly Overloading the +=, –=, /=, and *= Operators
- + 3.8 Indirectly Overloading the &&, ||, and ?: Operators
- + 3.9 Turning Bits On or Off
- + 3.10 Making Error-Free Expressions
- + 3.11 Minimizing (Reducing) Your Boolean Logic
- + 3.12 Converting Between Simple Types in a Language-Agnostic Manner
- + 3.13 Determining When to Use the Cast Operator, the as Operator, or the is Operator
- + 3.14 Casting with the as Operator
- + 3.15 Determining a Variable’s Type with the is Operator
- + 3.16 Implementing Polymorphism with Interfaces
- + 3.17 Calling the Same Method on Multiple Object Types
- + 3.18 Adding a Notification Callback Using an Interface
- + 3.19 Using Multiple Entry Points to Version an Application
- + 3.20 Preventing the Creation of an Only Partially Initialized Object
- + 3.21 Returning Multiple Items from a Method
- + 3.22 Parsing Command-Line Parameters
- + 3.23 Retrofitting a Class to Interoperate with COM
- + 3.24 Initializing a Constant Field at Runtime
- + 3.25 Writing Code That Is Compatible with the Widest Range of Managed Languages
- + 3.26 Building Cloneable Classes
- + 3.27 Assuring an Object’s Disposal
- + 3.28 Releasing a COM Object Through Managed Code
- + 3.29 Creating an Object Cache
- + 3.30 Rolling Back Object Changes
- + 3.31 Disposing of Unmanaged Resources
- + 3.32 Determining Where Boxing and Unboxing Occur
-
+
Generics
- 4.0 Introduction
- + 4.1 Deciding When and Where to Use Generics
- + 4.2 Understanding Generic Types
- + 4.3 Getting the Type of a Generic Type
- + 4.4 Replacing the ArrayList with Its Generic Counterpart
- + 4.5 Replacing the Stack and Queue with Their Generic Counterparts
- + 4.6 Implementing a Linked List
- + 4.7 Creating a Value Type That Can be Initialized to Null
- + 4.8 Reversing the Contents of a Sorted List
- + 4.9 Making Read-Only Collections the Generic Way
- + 4.10 Replacing the Hashtable with Its Generic Counterpart
- + 4.11 Using foreach with Generic Dictionary Types
- + 4.12 Constraining Type Arguments
- + 4.13 Initializing Generic Variables to Their Default Values
-
+
Collections
- 5.0 Introduction
- + 5.1 Swapping Two Elements in an Array
- + 5.2 Reversing an Array Quickly
- + 5.3 Reversing a Two-Dimensional Array
- + 5.4 Reversing a Jagged Array
- + 5.5 Writing a More Flexible StackTrace Class
- + 5.6 Determining the Number of Times an Item Appears in a List<T>
- + 5.7 Retrieving All Instances of a Specific Item in a List<T>
- + 5.8 Inserting and Removing Items from an Array
- + 5.9 Keeping Your List<T> Sorted
- + 5.10 Sorting a Dictionary’s Keys and/or Values
- + 5.11 Creating a Dictionary with Max and Min Value Boundaries
- + 5.12 Displaying an Array’s Data as a Delimited String
- + 5.13 Storing Snapshots of Lists in an Array
- + 5.14 Persisting a Collection Between Application Sessions
- + 5.15 Testing Every Element in an Array or List<T>
- + 5.16 Performing an Action on Each Element in an Array or List<T>
- + 5.17 Creating a Read-Only Array or List<T>
-
+
Iterators and Partial Types
- 6.0 Introduction
- + 6.1 Implementing Nested foreach Functionality in a Class
- + 6.2 Creating Custom Enumerators
- + 6.3 Creating an Iterator on a Generic Type
- + 6.4 Creating an Iterator on a Non-generic Type
- + 6.5 Creating Iterators That Accept Parameters
- + 6.6 Adding Multiple Iterators on a Single Type
- + 6.7 Implementing Iterators as Overloaded Operators
- + 6.8 Forcing an Iterator to Stop Iterating
- + 6.9 Dealing with Finally Blocks and Iterators
- + 6.10 Organizing Your Interface Implementations
- + 6.11 Generating Code That Is No Longer in Your Main Code Paths
-
+
Exception Handling
- 7.0 Introduction
- + 7.1 Verifying Critical Parameters
- + 7.2 Knowing When to Catch and Rethrow Exceptions
- + 7.3 Identifying Exceptions and Their Usage
- + 7.4 Handling Derived Exceptions Individually
- + 7.5 Assuring Exceptions Are Not Lost When Using Finally Blocks
- + 7.6 Handling Exceptions Thrown from Methods Invoked via Reflection
- + 7.7 Debugging Problems When Loading an Assembly
- + 7.8 Mapping Back and Forth Between Managed Exceptions and HRESULTs
- + 7.9 Handling User-Defined HRESULTs
- + 7.10 Preventing Unhandled Exceptions
- + 7.11 Getting Exception Information
- + 7.12 Getting to the Root of a Problem Quickly
- + 7.13 Creating a New Exception Type
- + 7.14 Obtaining a Stack Trace
- + 7.15 Breaking on a First-Chance Exception
- + 7.16 Preventing the Nefarious TypeInitializationException
- + 7.17 Handling Exceptions Thrown from an Asynchronous Delegate
- + 7.18 Giving Exceptions the Extra Info They Need with Exception.Data
- + 7.19 Looking at Exceptions in a New Way Using Visualizers
- + 7.20 Dealing with Unhandled Exceptions in WinForms Applications
-
+
Diagnostics
- 8.0 Introduction
- + 8.1 Controlling Tracing Output in Production Code
- + 8.2 Providing Fine-Grained Control over Debugging/Tracing Output
- + 8.3 Creating Your Own Custom Switch Class
- + 8.4 Compiling Blocks of Code Conditionally
- + 8.5 Determining Whether a Process Has Stopped Responding
- + 8.6 Using Event Logs in Your Application
- + 8.7 Changing the Maximum Size of a Custom Event Log
- + 8.8 Searching Event Log Entries
- + 8.9 Watching the Event Log for a Specific Entry
- + 8.10 Finding All Sources Belonging to a Specific Event Log
- + 8.11 Implementing a Simple Performance Counter
- + 8.12 Implementing Performance Counters That Require a Base Counter
- + 8.13 Enabling and Disabling Complex Tracing Code
- + 8.14 Capturing Standard Output for a Process
- + 8.15 Creating Custom Debugging Displays for Your Classes
- + 8.16 Determining Current AppDomain Settings Information
- + 8.17 Boosting the Priority of a Process Programmatically
- + 8.18 Looking at Your Runtime Environment and Seeing What You Can Do About It
-
+
Delegates, Events, and Anonymous Methods
- 9.0 Introduction
- + 9.1 Controlling When and If a Delegate Fires Within a Multicast Delegate
- + 9.2 Obtaining Return Values from Each Delegate in a Multicast Delegate
- + 9.3 Handling Exceptions Individually for Each Delegate in a Multicast Delegate
- + 9.4 Converting Delegate Invocation from Synchronous to Asynchronous
- + 9.5 Wrapping Sealed Classes to Add Events
- + 9.6 Passing Specialized Parameters to and from an Event
- + 9.7 An Advanced Interface Search Mechanism
- + 9.8 An Advanced Member Search Mechanism
- + 9.9 Observing Additions and Modifications to a Hashtable
- + 9.10 Using the Windows Keyboard Hook
- + 9.11 Tracking and Responding to the Mouse
- + 9.12 Using Anonymous Methods
- + 9.13 Set Up Event Handlers Without the Mess
- + 9.14 Using Different Parameter Modifiers in Anonymous Methods
- + 9.15 Using Closures in C#
- + 9.16 Performing Multiple Operations on a List Using Functors
-
+
Regular Expressions
- 10.0 Introduction
- + 10.1 Enumerating Matches
- + 10.2 Extracting Groups from a MatchCollection
- + 10.3 Verifying the Syntax of a Regular Expression
- + 10.4 Quickly Finding Only the Last Match in a String
- + 10.5 Replacing Characters or Words in a String
- + 10.6 Augmenting the Basic String Replacement Function
- + 10.7 Implementing a Better Tokenizer
- + 10.8 Compiling Regular Expressions
- + 10.9 Counting Lines of Text
- + 10.10 Returning the Entire Line in Which a Match Is Found
- + 10.11 Finding a Particular Occurrence of a Match
- + 10.12 Using Common Patterns
- + 10.13 Documenting Your Regular Expressions
- + 10.14 Using Built-in Regular Expressions to Parse ASP.NET Pages
-
+
Data Structures and Algorithms
- 11.0 Introduction
-
+
11.1 Creating a Hash Code for a Data Type
- + 11.2 Creating a Priority Queue
- + 11.3 Creating a Double Queue
- + 11.4 Determining Where Characters or Strings Do Not Balance
- + 11.5 Creating a One-to-Many Map (MultiMap)
- + 11.6 Creating a Binary Tree
- + 11.7 Creating an n-ary Tree
- + 11.8 Creating a Set Object
-
+
Filesystem I/O
- 12.0 Introduction
- + 12.1 Creating, Copying, Moving, or Deleting a File
- + 12.2 Manipulating File Attributes
- + 12.3 Renaming a File
- + 12.4 Determining Whether a File Exists
- + 12.5 Choosing a Method of Opening a File or Stream for Reading and/or Writing
- + 12.6 Accessing Part of a File Randomly
- + 12.7 Outputting a Platform-Independent EOL Character
- + 12.8 Creating, Writing to, and Reading from a File
- + 12.9 Determining Whether a Directory Exists
- + 12.10 Creating, Moving, or Deleting a Directory
- + 12.11 Manipulating Directory Attributes
- + 12.12 Renaming a Directory
- + 12.13 Searching for Directories or Files Using Wildcards
- + 12.14 Obtaining the Directory Tree
- + 12.15 Parsing a Path
- + 12.16 Parsing Paths in Environment Variables
- + 12.17 Verifying a Path
- + 12.18 Using a Temporary File in Your Application
- + 12.19 Opening a File Stream with Just a File Handle
- + 12.20 Writing to Multiple Output Files at One Time
- + 12.21 Launching and Interacting with Console Utilities
- + 12.22 Locking Subsections of a File
- + 12.23 Watching the Filesystem for Specific Changes to One or More Files or Directories
- + 12.24 Waiting for an Action to Occur in the Filesystem
- + 12.25 Comparing Version Information of Two Executable Modules
- + 12.26 Querying Information for All Drives on a System
- + 12.27 Encrypting/Decrypting an Existing File
- + 12.28 Compressing and Decompressing Your Files
-
+
Reflection
- 13.0 Introduction
- + 13.1 Listing Referenced Assemblies
- + 13.2 Listing Exported Types
- + 13.3 Finding Overridden Methods
- + 13.4 Finding Members in an Assembly
- + 13.5 Finding Members Within an Interface
- + 13.6 Determining and Obtaining Nested Types Within an Assembly
- + 13.7 Displaying the Inheritance Hierarchy for a Type
- + 13.8 Finding the Subclasses of a Type
- + 13.9 Finding All Serializable Types Within an Assembly
- + 13.10 Filtering Output When Obtaining Members
- + 13.11 Dynamically Invoking Members
- + 13.12 Providing Guidance to Obfuscators
- + 13.13 Determining if a Type or Method Is Generic
- + 13.14 Reading Manifest Resources Programmatically
- + 13.15 Accessing Local Variable Information
- + 13.16 Creating a Generic Type
-
+
Web
- 14.0 Introduction
- + 14.1 Converting an IP Address to a Hostname
- + 14.2 Converting a Hostname to an IP Address
- + 14.3 Parsing a URI
- + 14.4 Forming and Validating an Absolute URI
- + 14.5 Handling Web Server Errors
- + 14.6 Communicating with a Web Server
- + 14.7 Going Through a Proxy
- + 14.8 Obtaining the HTML from a URL
- + 14.9 Using the New Web Browser Control
- + 14.10 Tying Database Tables to the Cache
- + 14.11 Caching Data with Multiple Dependencies
- + 14.12 Prebuilding an ASP.NET Web Site Programmatically
- + 14.13 Escaping and Unescaping Data for the Web
- + 14.14 Using the UriBuilder Class
- + 14.15 Inspect and Change Your Web Application Configuration
- + 14.16 Working with HTML
- + 14.17 Using Cached Results When Working with HTTP for Faster Performance
- + 14.18 Checking out a Web Server’s Custom Error Pages
- + 14.19 Determining the Application Mappings for ASP.NET Set Up on IIS
-
+
XML
- 15.0 Introduction
- + 15.1 Reading and Accessing XML Data in Document Order
- + 15.2 Reading XML on the Web
- + 15.3 Querying the Contents of an XML Document
- + 15.4 Validating XML
- + 15.5 Creating an XML Document Programmatically
- + 15.6 Detecting Changes to an XML Document
- + 15.7 Handling Invalid Characters in an XML String
- + 15.8 Transforming XML
- + 15.9 Tearing Apart an XML Document
- + 15.10 Putting Together an XML Document
- + 15.11 Validating Modified XML Documents Without Reloading
- + 15.12 Extending XSLT Transformations
- + 15.13 Getting Your Schema in Bulk from Existing XML Files
- + 15.14 Passing Parameters to XSLT Transformations
-
+
Networking
- 16.0 Introduction
- + 16.1 Writing a TCP Server
- + 16.2 Writing a TCP Client
- + 16.3 Simulating Form Execution
- + 16.4 Downloading Data from a Server
- + 16.5 Using Named Pipes to Communicate
- + 16.6 Pinging Programmatically
- + 16.7 Send SMTP Mail Using the SMTP Service
- + 16.8 Check Out Your Network Connectivity
- + 16.9 Use Sockets to Scan the Ports on a Machine
- + 16.10 Use the Current Internet Connection Settings
- + 16.11 Download a File Using FTP
-
+
Security
- 17.0 Introduction
- + 17.1 Controlling Access to Types in a Local Assembly
- + 17.2 Encrypting and Decrypting a String
- + 17.3 Encrypting and Decrypting a File
- + 17.4 Cleaning up Cryptography Information
- + 17.5 Verifying that a String Remains Uncorrupted Following Transmission
- + 17.6 Wrapping a String Hash for Ease of Use
- + 17.7 A Better Random Number Generator
- + 17.8 Storing Data Securely
- + 17.9 Making a Security Assert Safe
- + 17.10 Preventing Malicious Modifications to an Assembly
- + 17.11 Verifying That an Assembly Has Been Granted Specific Permissions
- + 17.12 Minimizing the Attack Surface of an Assembly
- + 17.13 Obtaining Security/Audit Information
- + 17.14 Granting/Revoking Access to a File or Registry Key
- + 17.15 Protecting String Data with Secure Strings
- + 17.16 Securing Stream Data
- + 17.17 Encrypting web.config Information
- + 17.18 Obtaining the Full Reason a SecurityException Was Thrown
- + 17.19 Achieving Secure Unicode Encoding
- + 17.20 Obtaining a Safer File Handle
-
+
Threading and Synchronization
- 18.0 Introduction
- + 18.1 Creating Per-Thread Static Fields
- + 18.2 Providing Thread-Safe Access to Class Members
- + 18.3 Preventing Silent Thread Termination
- + 18.4 Polling an Asynchronous Delegate
- + 18.5 Timing Out an Asynchronous Delegate
- + 18.6 Being Notified of the Completion of an Asynchronous Delegate
- + 18.7 Determining Whether a Request for a Pooled Thread Will be Queued
- + 18.8 Configuring a Timer
- + 18.9 Storing Thread-Specific Data Privately
- + 18.10 Granting Multiple Access to Resources with a Semaphore
- + 18.11 Synchronizing Multiple Processes with the Mutex
- + 18.12 Using Events to Make Threads Cooperate
- + 18.13 Get the Naming Rights for Your Events
- + 18.14 Performing Atomic Operations Among Threads
-
+
Unsafe Code
- 19.0 Introduction
- + 19.1 Controlling Changes to Pointers Passed to Methods
- + 19.2 Comparing Pointers
- + 19.3 Navigating Arrays
- + 19.4 Manipulating a Pointer to a Fixed Array
- + 19.5 Returning a Pointer to a Particular Element in an Array
- + 19.6 Creating and Using an Array of Pointers
- + 19.7 Switching Unknown Pointer Types
- + 19.8 Converting a String to a char*
- + 19.9 Declaring a Fixed-Size Structure with an Embedded Array
-
+
Toolbox
- 20.0 Introduction
- + 20.1 Dealing with Operating System Shutdown, Power Management, or User Session Changes
- + 20.2 Controlling a Service
- + 20.3 List What Processes an Assembly Is Loaded In
- + 20.4 Using Message Queues on a Local Workstation
- + 20.5 Finding the Path to the Current Framework Version
- + 20.6 Determining the Versions of an Assembly That Are Registered in the Global Assembly Cache (GAC)
- + 20.7 Getting the Windows Directory
- + 20.8 Capturing Output from the Standard Output Stream
- + 20.9 Running Code in Its Own AppDomain
- + 20.10 Determining the Operating System and Service Pack Version of the Current Operating System
- Index
With C# Cookbook, 2nd Edition, you'll be able to learn and improve your mastery of both the C# language and the .NET platform. This updated bestseller has been completely revised to account for C# 2.0, the latest version of this popular object-oriented programming language. It also includes more than 100 new code solutions (over 300 overall) to common problems and tasks that you're sure to face as a C# programmer.
Nearly every solution, or "recipe," contains a complete, documented code sample showing you how to solve the specific problem, as well as a detailed discussion of how and why the underling technology works. This question- solution-discussion format is a proven teaching method, as any fan of O'Reilly's "Cookbook" series can attest to. In fact, no other source offers a learn-as-you-go format quite like this.
C# Cookbook, 2nd Edition is organized into 20 chapters, each of which focuses on a particular topic in creating C# solutions. Here are just some of what's covered:
Numeric data types
Strings and characters
Classes and structures
Generics
Exception handling
Delegates, events, and anonymous methods
Filesystem interactions
Web site access
XML usage (including XPath and XSLT)
Networking
Threading
Unsafe code
Best of all, you don't have to be an experienced C# or .NET developer to use C# Cookbook, 2nd Edition<
i>. You just have to be someone who wants to solve a problem now, without having to learn all the related theory first.
Test the closed alpha on paperc.com
Book Details
Authors
Jay Hilyard and Stephen Teilhet
Categories
Computers > Programming Languages > C#
Publishers
Publication year : 2007
License: All rights reserved ©
Times read: 2,290

