C# Cookbook
By
Stephen Teilhet,
Jay Hilyard
January 2004
Pages: 856
| Table of Contents
| Index
| Sample Chapter
| Colophon
Table of Contents
-
Chapter 1 Numbers
-
Determining Approximate Equality Between a Fraction and Floating-Point Value
-
Converting Degrees to Radians
-
Converting Radians to Degrees
-
Using the Bitwise Complement Operator with Various Data Types
-
Test for an Even or Odd Value
-
Obtaining the Most- or Least-Significant Bits of a Number
-
Converting a Number in Another Base to Base10
-
Determining Whether a String Is a Valid Number
-
Rounding a Floating-Point Value
-
Different Rounding Algorithms
-
Converting Celsius to Fahrenheit
-
Converting Fahrenheit to Celsius
-
Safely Performing a Narrowing Numeric Cast
-
Finding the Length of Any Three Sidesof a Right Triangle
-
Finding the Angles of a Right Triangle
-
Chapter 2 Strings and Characters
-
Determining the Kind of Character
-
Determining Whether a Character Is Within a Specified Range
-
Controlling Case Sensitivity when Comparing Two Characters
-
Finding All Occurrences of a Character Within a String
-
Finding the Location of All Occurrencesof a String Within Another String
-
The Poor Man's Tokenizer
-
Controlling Case Sensitivity when Comparing Two Strings
-
Comparing a String to the Beginning or End of a Second String
-
Inserting Text into a String
-
Removing or Replacing Characters Within a String
-
Encoding Binary Data as Base64
-
Decoding a Base64-Encoded Binary
-
Converting a String Returned as a Byte[ ] Back into a String
-
Passing a String to a Method that Accepts Only a Byte[ ]
-
Converting Strings to Their Equivalent Value Type
-
Formatting Data in Strings
-
Creating a Delimited String
-
Extracting Items from a Delimited String
-
Setting the Maximum Number of Characters a String Can Contain
-
Iterating Over Each Character in a String
-
Improving String Comparison Performance
-
Improving StringBuilder Performance
-
Pruning Characters from the Headand/or Tail of a String
-
Chapter 3 Classes and Structures
-
Creating Union-Type Structures
-
Allowing a Type to Represent Itself as a String
-
Converting a String Representation of an Object into an Actual Object
-
Polymorphism via Concrete or Abstract Base Classes
-
Making a Type Sortable
-
Making a Type Searchable
-
Indirectly Overloading the +=, -=, /=, and *= Operators
-
Indirectly Overloading the &&, ||, and ?: Operators
-
Improving the Performance of a Structure's Equals Method
-
Turning Bits On or Off
-
Making Error-Free Expressions
-
Minimizing (Reducing) Your Boolean Logic
-
Converting Between Simple Types in a Language Agnostic Manner
-
Determining Whether to Use theCast Operator, the as Operator, or theis Operator
-
Casting with the as Operator
-
Determining a Variable's Type with the is Operator
-
Polymorphism via Interfaces
-
Calling the Same Method on Multiple Object Types
-
Adding a Notification Callback Using an Interface
-
Using Multiple Entry Points toVersion an Application
-
Preventing the Creation of an Only Partially Initialized Object
-
Returning Multiple Items from a Method
-
Parsing Command-Line Parameters
-
Retrofitting a Class to Interoperate with COM
-
Initializing a Constant Field at Runtime
-
Writing Code that Is Compatible with the Widest Range of Managed Languages
-
Implementing Nested foreach Functionality in a Class
-
Building Cloneable Classes
-
Assuring an Object's Disposal
-
Releasing a COM Object ThroughManaged Code
-
Creating an Object Cache
-
The Single Instance Object
-
Choosing a Serializer
-
Creating Custom Enumerators
-
Rolling Back Object Changes
-
Disposing of Unmanaged Resources
-
Determining Where Boxing and Unboxing Occur
-
Chapter 4 Enumerations
-
Displaying an Enumeration Value as a String
-
Converting Plain Text to an Equivalent Enumeration Value
-
Testing for a Valid Enumeration Value
-
Testing for a Valid Enumeration of Flags
-
Using Enumerated Members in a Bitmask
-
Determining Whether One or More Enumeration Flags Are Set
-
Chapter 5 Exception Handling
-
Verifying Critical Parameters
-
Indicating Where Exceptions Originate
-
Choosing when to Throw a Particular Exception
-
Handling Derived Exceptions Individually
-
Assuring Exceptions are Not Lost when Using Finally Blocks
-
Handling Exceptions Thrown from Methods Invoked via Reflection
-
Debugging Problems whenLoading an Assembly
-
HRESULT-Exception Mapping
-
Handling User-Defined HRESULTs
-
Preventing Unhandled Exceptions
-
Displaying Exception Information
-
Getting to the Root of a Problem Quickly
-
Creating a New Exception Type
-
Obtaining a Stack Trace
-
Breaking on a First Chance Exception
-
Preventing the Nefarious TypeInitializationException
-
Handling Exceptions Thrown from an Asynchronous Delegate
-
Chapter 6 Diagnostics
-
Controlling Tracing Output inProduction Code
-
Providing Fine-Grained Control Over Debugging/Tracing Output
-
Creating Your Own Custom Switch Class
-
A Custom Trace Class that Outputs Information in an XML Format
-
Conditionally Compiling Blocks of Code
-
Determining Whether a Process Has Stopped Responding
-
Using One or More Event Logs in Your Application
-
Changing the Maximum Size of a Custom Event Log
-
Searching Event Log Entries
-
Watching the Event Log for a Specific Entry
-
Finding All Sources Belonging to a Specific Event Log
-
Implementing a Simple Performance Counter
-
Implementing Performance Counters that Require a Base Counter
-
Enable/Disable Complex Tracing Code
-
Chapter 7 Delegates and Events
-
Controlling when and if a Delegate Fires Within a Multicast Delegate
-
Obtaining Return Values from Each Delegate in a Multicast Delegate
-
Handling Exceptions Individually for Each Delegate in a Multicast Delegate
-
Converting a Synchronous Delegate to an Asynchronous Delegate
-
Adding Events to a Sealed Class
-
Passing Specialized Parameters to and from an Event
-
An Advanced Interface Search Mechanism
-
An Advanced Member Search Mechanism
-
Observing Additions and Modifications to a Hashtable
-
Using the Windows Keyboard Hook
-
Using Windows Hooks to Manipulate the Mouse
-
Chapter 8 Regular Expressions
-
Enumerating Matches
-
Extracting Groups from a MatchCollection
-
Verifying the Syntax of a Regular Expression
-
Quickly Finding Only the Last Match in a String
-
Replacing Characters or Words in a String
-
Augmenting the Basic String Replacement Function
-
A Better Tokenizer
-
Compiling Regular Expressions
-
Counting Lines of Text
-
Returning the Entire Line in Which a Match Is Found
-
Finding a Particular Occurrence of a Match
-
Using Common Patterns
-
Documenting Your Regular Expressions
-
Chapter 9 Collections
-
Swapping Two Elements in an Array
-
Quickly Reversing an Array
-
Reversing a Two-Dimensional Array
-
Reversing a Jagged Array
-
A More Flexible StackTrace Class
-
Determining the Number of Times an Item Appears in an ArrayList
-
Retrieving All Instances of a Specific Itemin an ArrayList
-
Inserting and Removing Items from an Array
-
Keeping Your ArrayList Sorted
-
Sorting a Hashtable's Keys and/or Values
-
Creating a Hashtable with Max and Min Size Boundaries
-
Creating a Hashtable with Max and Min Value Boundaries
-
Displaying an Array's Data as a Delimited String
-
Storing Snapshots of Lists in an Array
-
Creating a Strongly Typed Collection
-
Persisting a Collection Between Application Sessions
-
Chapter 10 Data Structures and Algorithms
-
Creating a Hash Code for a Data Type
-
Creating a Priority Queue
-
Creating a More Versatile Queue
-
Determining Where Characters or Strings Do Not Balance
-
Creating a One-to-Many Map (MultiMap)
-
Creating a Binary Tree
-
Creating an n-ary Tree
-
Creating a Set Object
-
Chapter 11 Filesystem I/O
-
Creating, Copying, Moving, and Deleting a File
-
Manipulating File Attributes
-
Renaming a File
-
Determining Whether a File Exists
-
Choosing a Method of Opening a File or Stream for Reading and/or Writing
-
Randomly Accessing Part of a File
-
Outputting a Platform-Independent EOL Character
-
Create, Write to, and Read from a File
-
Determining Whether a Directory Exists
-
Creating, Moving, and Deleting a Directory
-
Manipulating Directory Attributes
-
Renaming a Directory
-
Searching for Directories or FilesUsing Wildcards
-
Obtaining the Directory Tree
-
Parsing a Path
-
Parsing Paths in Environment Variables
-
Verifying a Path
-
Using a Temporary File in Your Application
-
Opening a File Stream with just aFile Handle
-
Write to Multiple Output Files at One Time
-
Launching and Interacting withConsole Utilities
-
Locking Subsections of a File
-
Watching the Filesystem for Specific Changes to One or More Files or Directories
-
Waiting for an Action to Occurin the Filesystem
-
Comparing Version Information of Two Executable Modules
-
Chapter 12 Reflection
-
Listing Imported Assemblies
-
Listing Exported Types
-
Finding Overridden Methods
-
Finding Members in an Assembly
-
Finding Members Within an Interface
-
Obtaining Types Nested Within a Type
-
Displaying the Inheritance Hierarchy for a Type
-
Finding the Subclasses of a Type
-
Finding All Serializable Types Within an Assembly
-
Controlling Additions to an ArrayList Through Attributes
-
Filtering Output when Obtaining Members
-
Dynamically Invoking Members
-
Chapter 13 Networking
-
Converting an IP Address to a Hostname
-
Converting a Hostname to an IP Address
-
Parsing a URI
-
Forming an Absolute URI
-
Handling Web Server Errors
-
Communicating with a Web Server
-
Going Through a Proxy
-
Obtaining the HTML from a URL
-
Writing a TCP Server
-
Writing a TCP Client
-
Simulating Form Execution
-
Downloading Data from a Server
-
Using Named Pipes to Communicate
-
Chapter 14 Security
-
Controlling Access to Types in aLocal Assembly
-
Encrypting/Decrypting a String
-
Encrypting and Decrypting a File
-
Cleaning Up Cryptography Information
-
Verifying that a String Is Uncorrupted During Transmission
-
Wrapping a String Hash for Ease of Use
-
A Better Random Number Generator
-
Securely Storing Data
-
Making a Security Assert Safe
-
Preventing Malicious Modifications to an Assembly
-
Verifying that an Assembly Has Been Granted Specific Permissions
-
Minimizing the Attack Surface of an Assembly
-
Chapter 15 Threading
-
Creating Per-Thread Static Fields
-
Providing Thread Safe Access to Class Members
-
Preventing Silent Thread Termination
-
Polling an Asynchronous Delegate
-
Timing Out an Asynchronous Delegate
-
Being Notified of the Completionof an Asynchronous Delegate
-
Waiting for Worker Thread Completion
-
Synchronizing the Reading and Writingof a Resource Efficiently
-
Determining Whether a Requestfor a Pooled Thread Will Be Queued
-
Waiting for All Threads in theThread Pool to Finish
-
Configuring a Timer
-
Storing Thread-Specific Data Privately
-
Chapter 16 Unsafe Code
-
Controlling Changes to Pointers Passedto Methods
-
Comparing Pointers
-
Navigating Arrays
-
Manipulating a Pointer to a Fixed Array
-
Returning a Pointer to a Particular Element in an Array
-
Creating and Using an Array of Pointers
-
Creating and Using an Array of Pointersto Unknown Types
-
Switching Unknown Pointer Types
-
Breaking Up Larger Numbers into Their Equivalent Byte Array Representation
-
Converting Pointers to a Byte[ ], SByte[ ],or Char[ ] to a String
-
Chapter 17 XML
-
Reading and Accessing XML Datain Document Order
-
Reading XML on the Web
-
Querying the Contents of an XML Document
-
Validating XML
-
Creating an XML Document Programmatically
-
Detecting Changes to an XML Document
-
Handling Invalid Characters in anXML String
-
Transforming XML to HTML
-
Tearing Apart an XML Document
-
Putting Together an XML Document
-
Colophon
Return to C# Cookbook