Home Page Icon
Home Page
Table of Contents for
Cover
Close
Cover
by Jay Hilyard, Stephen Teilhet
C# Cookbook
C# Cookbook
Preface
Who This Book Is For
What You Need to Use This Book
How This Book Is Organized
What Was Left Out
Conventions Used in This Book
About the Code
Using Code Examples
Platform Notes
Comments and Questions
Acknowledgments
From Steve Teilhet
From Jay Hilyard
1. Numbers
1.1. Determining Approximate Equality Between a Fraction and Floating-Point Value
Problem
Solution
Discussion
See Also
1.2. Converting Degrees to Radians
Problem
Solution
Discussion
1.3. Converting Radians to Degrees
Problem
Solution
Discussion
1.4. Using the Bitwise Complement Operator with Various Data Types
Problem
Solution
Discussion
1.5. Test for an Even or Odd Value
Problem
Solution
Discussion
1.6. Obtaining the Most- or Least-Significant Bits of a Number
Problem
Solution
Discussion
1.7. Converting a Number in Another Base to Base10
Problem
Solution
Discussion
See Also
1.8. Determining Whether a String Is a Valid Number
Problem
Solution
Discussion
1.9. Rounding a Floating-Point Value
Problem
Solution
Discussion
See Also
1.10. Different Rounding Algorithms
Problem
Solution
Discussion
See Also
1.11. Converting Celsius to Fahrenheit
Problem
Solution
Discussion
1.12. Converting Fahrenheit to Celsius
Problem
Solution
Discussion
1.13. Safely Performing a Narrowing Numeric Cast
Problem
Solution
Discussion
See Also
1.14. Finding the Length of Any Three Sidesof a Right Triangle
Problem
Solution
Discussion
See Also
1.15. Finding the Angles of a Right Triangle
Problem
Solution
Discussion
See Also
2. Strings and Characters
2.1. Determining the Kind of Character
Problem
Solution
Discussion
See Also
2.2. Determining Whether a Character Is Within a Specified Range
Problem
Solution
Discussion
2.3. Controlling Case Sensitivity when Comparing Two Characters
Problem
Solution
Discussion
2.4. Finding All Occurrences of a Character Within a String
Problem
Solution
Discussion
2.5. Finding the Location of All Occurrencesof a String Within Another String
Problem
Solution
Discussion
See Also
2.6. The Poor Man’s Tokenizer
Problem
Solution
Discussion
See Also
2.7. Controlling Case Sensitivity when Comparing Two Strings
Problem
Solution
Discussion
See Also
2.8. Comparing a String to the Beginning or End of a Second String
Problem
Solution
Discussion
See Also
2.9. Inserting Text into a String
Problem
Solution
Discussion
See Also
2.10. Removing or Replacing Characters Within a String
Problem
Solution
Discussion
See Also
2.11. Encoding Binary Data as Base64
Problem
Solution
Discussion
See Also
2.12. Decoding a Base64-Encoded Binary
Problem
Solution
Discussion
See Also
2.13. Converting a String Returned as a Byte[ ] Back into a String
Problem
Solution
Discussion
See Also
2.14. Passing a String to a Method that Accepts Only a Byte[ ]
Problem
Solution
Discussion
See Also
2.15. Converting Strings to Their Equivalent Value Type
Problem
Solution
Discussion
2.16. Formatting Data in Strings
Problem
Solution
Discussion
See Also
2.17. Creating a Delimited String
Problem
Solution
Discussion
See Also
2.18. Extracting Items from a Delimited String
Problem
Solution
Discussion
See Also
2.19. Setting the Maximum Number of Characters a String Can Contain
Problem
Solution
Discussion
See Also
2.20. Iterating Over Each Character in a String
Problem
Solution
Discussion
2.21. Improving String Comparison Performance
Problem
Solution
Discussion
See Also
2.22. Improving StringBuilder Performance
Problem
Solution
Discussion
See Also
2.23. Pruning Characters from the Headand/or Tail of a String
Problem
Solution
Discussion
See Also
3. Classes and Structures
3.1. Creating Union-Type Structures
Problem
Solution
Discussion
See Also
3.2. Allowing a Type to Represent Itself as a String
Problem
Solution
Discussion
See Also
3.3. Converting a String Representation of an Object into an Actual Object
Problem
Solution
Discussion
See Also
3.4. Polymorphism via Concrete or Abstract Base Classes
Problem
Solution
Discussion
See Also
3.5. Making a Type Sortable
Problem
Solution
Discussion
See Also
3.6. Making a Type Searchable
Problem
Solution
Discussion
See Also
3.7. Indirectly Overloading the +=, -=, /=, and *= Operators
Problem
Solution
Discussion
See Also
3.8. Indirectly Overloading the &&, ||, and ?: Operators
Problem
Solution
Discussion
See Also
3.9. Improving the Performance of a Structure’s Equals Method
Problem
Solution
Discussion
See Also
3.10. Turning Bits On or Off
Problem
Solution
Discussion
See Also
3.11. Making Error-Free Expressions
Problem
Solution
Discussion
3.12. Minimizing (Reducing) Your Boolean Logic
Problem
Solution
Discussion
See Also
3.13. Converting Between Simple Types in a Language Agnostic Manner
Problem
Solution
Discussion
See Also
3.14. Determining Whether to Use theCast Operator, the as Operator, or theis Operator
Problem
Solution
Discussion
See Also
3.15. Casting with the as Operator
Problem
Solution
Discussion
See Also
3.16. Determining a Variable’s Type with the is Operator
Problem
Solution
Discussion
See Also
3.17. Polymorphism via Interfaces
Problem
Solution
Discussion
See Also
3.18. Calling the Same Method on Multiple Object Types
Problem
Solution
Discussion
See Also
3.19. Adding a Notification Callback Using an Interface
Problem
Solution
Discussion
See Also
3.20. Using Multiple Entry Points toVersion an Application
Problem
Solution
Discussion
See Also
3.21. Preventing the Creation of an Only Partially Initialized Object
Problem
Solution
Discussion
3.22. Returning Multiple Items from a Method
Problem
Solution
Discussion
3.23. Parsing Command-Line Parameters
Problem
Solution
Discussion
See Also
3.24. Retrofitting a Class to Interoperate with COM
Problem
Solution
Discussion
See Also
3.25. Initializing a Constant Field at Runtime
Problem
Solution
Discussion
See Also
3.26. Writing Code that Is Compatible with the Widest Range of Managed Languages
Problem
Solution
Discussion
See Also
3.27. Implementing Nested foreach Functionality in a Class
Problem
Solution
Discussion
See Also
3.28. Building Cloneable Classes
Problem
Solution
Discussion
See Also
3.29. Assuring an Object’s Disposal
Problem
Solution
Discussion
See Also
3.30. Releasing a COM Object ThroughManaged Code
Problem
Solution
Discussion
See Also
3.31. Creating an Object Cache
Problem
Solution
Discussion
See Also
3.32. The Single Instance Object
Problem
Solution
Discussion
See Also
3.33. Choosing a Serializer
Problem
Solution
Discussion
See Also
3.34. Creating Custom Enumerators
Problem
Solution
Discussion
See Also
3.35. Rolling Back Object Changes
Problem
Solution
Discussion
3.36. Disposing of Unmanaged Resources
Problem
Solution
Discussion
See Also
3.37. Determining Where Boxing and Unboxing Occur
Problem
Solution
Discussion
See Also
4. Enumerations
4.1. Displaying an Enumeration Value as a String
Problem
Solution
Discussion
See Also
4.2. Converting Plain Text to an Equivalent Enumeration Value
Problem
Solution
Discussion
See Also
4.3. Testing for a Valid Enumeration Value
Problem
Solution
Discussion
See Also
4.4. Testing for a Valid Enumeration of Flags
Problem
Solution
Discussion
See Also
4.5. Using Enumerated Members in a Bitmask
Problem
Solution
Discussion
4.6. Determining Whether One or More Enumeration Flags Are Set
Problem
Solution
Discussion
5. Exception Handling
5.1. Verifying Critical Parameters
Problem
Solution
Discussion
See Also
5.2. Indicating Where Exceptions Originate
Problem
Solution
Discussion
5.3. Choosing when to Throw a Particular Exception
Problem
Solution
Discussion
See Also
5.4. Handling Derived Exceptions Individually
Problem
Solution
Discussion
See Also
5.5. Assuring Exceptions are Not Lost when Using Finally Blocks
Problem
Solution
Discussion
See Also
5.6. Handling Exceptions Thrown from Methods Invoked via Reflection
Problem
Solution
Discussion
See Also
5.7. Debugging Problems whenLoading an Assembly
Problem
Solution
Discussion
See Also
5.8. HRESULT-Exception Mapping
Problem
Solution
Discussion
See Also
5.9. Handling User-Defined HRESULTs
Problem
Solution
Discussion
See Also
5.10. Preventing Unhandled Exceptions
Problem
Solution
Discussion
See Also
5.11. Displaying Exception Information
Problem
Solution
Discussion
See Also
5.12. Getting to the Root of a Problem Quickly
Problem
Solution
Discussion
See Also
5.13. Creating a New Exception Type
Problem
Solution
Discussion
See Also
5.14. Obtaining a Stack Trace
Problem
Solution
Discussion
See Also
5.15. Breaking on a First Chance Exception
Problem
Solution
Discussion
5.16. Preventing the Nefarious TypeInitializationException
Problem
Solution
Discussion
See Also
5.17. Handling Exceptions Thrown from an Asynchronous Delegate
Problem
Solution
Discussion
See Also
6. Diagnostics
6.1. Controlling Tracing Output inProduction Code
Problem
Solution
Discussion
See Also
6.2. Providing Fine-Grained Control Over Debugging/Tracing Output
Problem
Solution
Discussion
See Also
6.3. Creating Your Own Custom Switch Class
Problem
Solution
Discussion
See Also
6.4. A Custom Trace Class that Outputs Information in an XML Format
Problem
Solution
Discussion
See Also
6.5. Conditionally Compiling Blocks of Code
Problem
Solution
Discussion
See Also
6.6. Determining Whether a Process Has Stopped Responding
Problem
Solution
Discussion
See Also
6.7. Using One or More Event Logs in Your Application
Problem
Solution
Discussion
See Also
6.8. Changing the Maximum Size of a Custom Event Log
Problem
Solution
Discussion
See Also
6.9. Searching Event Log Entries
Problem
Solution
Discussion
See Also
6.10. Watching the Event Log for a Specific Entry
Problem
Solution
Discussion
See Also
6.11. Finding All Sources Belonging to a Specific Event Log
Problem
Solution
Discussion
See Also
6.12. Implementing a Simple Performance Counter
Problem
Solution
Discussion
See Also
6.13. Implementing Performance Counters that Require a Base Counter
Problem
Solution
Discussion
See Also
6.14. Enable/Disable Complex Tracing Code
Problem
Solution
Discussion
See Also
7. Delegates and Events
7.1. Controlling when and if a Delegate Fires Within a Multicast Delegate
Problem
Solution
Discussion
See Also
7.2. Obtaining Return Values from Each Delegate in a Multicast Delegate
Problem
Solution
Discussion
See Also
7.3. Handling Exceptions Individually for Each Delegate in a Multicast Delegate
Problem
Solution
Discussion
See Also
7.4. Converting a Synchronous Delegate to an Asynchronous Delegate
Problem
Solution
Discussion
See Also
7.5. Adding Events to a Sealed Class
Problem
Solution
Discussion
See Also
7.6. Passing Specialized Parameters to and from an Event
Problem
Solution
Discussion
See Also
7.7. An Advanced Interface Search Mechanism
Problem
Solution
Discussion
See Also
7.8. An Advanced Member Search Mechanism
Problem
Solution
Discussion
See Also
7.9. Observing Additions and Modifications to a Hashtable
Problem
Solution
Discussion
See Also
7.10. Using the Windows Keyboard Hook
Problem
Solution
Discussion
See Also
7.11. Using Windows Hooks to Manipulate the Mouse
Problem
Solution
Discussion
See Also
8. Regular Expressions
8.1. Enumerating Matches
Problem
Solution
Discussion
See Also
8.2. Extracting Groups from a MatchCollection
Problem
Solution
Discussion
See Also
8.3. Verifying the Syntax of a Regular Expression
Problem
Solution
Discussion
8.4. Quickly Finding Only the Last Match in a String
Problem
Solution
Discussion
See Also
8.5. Replacing Characters or Words in a String
Problem
Solution
Discussion
See Also
8.6. Augmenting the Basic String Replacement Function
Problem
Solution
Discussion
See Also
8.7. A Better Tokenizer
Problem
Solution
Discussion
See Also
8.8. Compiling Regular Expressions
Problem
Solution
Discussion
See Also
8.9. Counting Lines of Text
Problem
Solution
Discussion
See Also
8.10. Returning the Entire Line in Which a Match Is Found
Problem
Solution
Discussion
See Also
8.11. Finding a Particular Occurrence of a Match
Problem
Solution
Discussion
See Also
8.12. Using Common Patterns
Problem
Solution
Discussion
See Also
8.13. Documenting Your Regular Expressions
Problem
Solution
Discussion
9. Collections
9.1. Swapping Two Elements in an Array
Problem
Solution
Discussion
9.2. Quickly Reversing an Array
Problem
Solution
Discussion
See Also
9.3. Reversing a Two-Dimensional Array
Problem
Solution
Discussion
See Also
9.4. Reversing a Jagged Array
Problem
Solution
Discussion
See Also
9.5. A More Flexible StackTrace Class
Problem
Solution
Discussion
See Also
9.6. Determining the Number of Times an Item Appears in an ArrayList
Problem
Solution
Discussion
See Also
9.7. Retrieving All Instances of a Specific Itemin an ArrayList
Problem
Solution
Discussion
See Also
9.8. Inserting and Removing Items from an Array
Problem
Solution
Discussion
See Also
9.9. Keeping Your ArrayList Sorted
Problem
Solution
Discussion
See Also
9.10. Sorting a Hashtable’s Keys and/or Values
Problem
Solution
Discussion
See Also
9.11. Creating a Hashtable with Max and Min Size Boundaries
Problem
Solution
Discussion
See Also
9.12. Creating a Hashtable with Max and Min Value Boundaries
Problem
Solution
Discussion
See Also
9.13. Displaying an Array’s Data as a Delimited String
Problem
Solution
Discussion
See Also
9.14. Storing Snapshots of Lists in an Array
Problem
Solution
Discussion
See Also
9.15. Creating a Strongly Typed Collection
Problem
Solution
Discussion
See Also
9.16. Persisting a Collection Between Application Sessions
Problem
Solution
Discussion
See Also
10. Data Structures and Algorithms
10.1. Creating a Hash Code for a Data Type
Problem
Solution
The simple hash
The folding hash
The contained object cache
The CryptoHash method
The CryptoHash method using a nonstring
The shift and add hash
The calculated hash
The string concatenation hash
Discussion
See Also
10.2. Creating a Priority Queue
Problem
Solution
Discussion
See Also
10.3. Creating a More Versatile Queue
Problem
Solution
Discussion
See Also
10.4. Determining Where Characters or Strings Do Not Balance
Problem
Solution
Discussion
See Also
10.5. Creating a One-to-Many Map (MultiMap)
Problem
Solution
Discussion
See Also
10.6. Creating a Binary Tree
Problem
Solution
Discussion
See Also
10.7. Creating an n-ary Tree
Problem
Solution
Discussion
See Also
10.8. Creating a Set Object
Problem
Solution
Discussion
See Also
11. Filesystem I/O
11.1. Creating, Copying, Moving, and Deleting a File
Problem
Solution
Discussion
See Also
11.2. Manipulating File Attributes
Problem
Solution
Discussion
See Also
11.3. Renaming a File
Problem
Solution
Discussion
See Also
11.4. Determining Whether a File Exists
Problem
Solution
Discussion
See Also
11.5. Choosing a Method of Opening a File or Stream for Reading and/or Writing
Problem
Solution
Discussion
See Also
11.6. Randomly Accessing Part of a File
Problem
Solution
Discussion
See Also
11.7. Outputting a Platform-Independent EOL Character
Problem
Solution
Discussion
See Also
11.8. Create, Write to, and Read from a File
Problem
Solution
Discussion
See Also
11.9. Determining Whether a Directory Exists
Problem
Solution
Discussion
See Also
11.10. Creating, Moving, and Deleting a Directory
Problem
Solution
Discussion
See Also
11.11. Manipulating Directory Attributes
Problem
Solution
Discussion
See Also
11.12. Renaming a Directory
Problem
Solution
Discussion
See Also
11.13. Searching for Directories or FilesUsing Wildcards
Problem
Solution
Discussion
See Also
11.14. Obtaining the Directory Tree
Problem
Solution
Discussion
See Also
11.15. Parsing a Path
Problem
Solution
Discussion
See Also
11.16. Parsing Paths in Environment Variables
Problem
Solution
Discussion
See Also
11.17. Verifying a Path
Problem
Solution
Discussion
See Also
11.18. Using a Temporary File in Your Application
Problem
Solution
Discussion
See Also
11.19. Opening a File Stream with just aFile Handle
Problem
Solution
Discussion
See Also
11.20. Write to Multiple Output Files at One Time
Problem
Solution
Discussion
See Also
11.21. Launching and Interacting withConsole Utilities
Problem
Solution
Discussion
See Also
11.22. Locking Subsections of a File
Problem
Solution
Discussion
See Also
11.23. Watching the Filesystem for Specific Changes to One or More Files or Directories
Problem
Solution
Discussion
See Also
11.24. Waiting for an Action to Occurin the Filesystem
Problem
Solution
Discussion
See Also
11.25. Comparing Version Information of Two Executable Modules
Problem
Solution
Discussion
See Also
12. Reflection
12.1. Listing Imported Assemblies
Problem
Solution
Discussion
See Also
12.2. Listing Exported Types
Problem
Solution
Discussion
See Also
12.3. Finding Overridden Methods
Problem
Solution
Discussion
See Also
12.4. Finding Members in an Assembly
Problem
Solution
Discussion
See Also
12.5. Finding Members Within an Interface
Problem
Solution
Discussion
See Also
12.6. Obtaining Types Nested Within a Type
Problem
Solution
Discussion
See Also
12.7. Displaying the Inheritance Hierarchy for a Type
Problem
Solution
Discussion
See Also
12.8. Finding the Subclasses of a Type
Problem
Solution
Discussion
See Also
12.9. Finding All Serializable Types Within an Assembly
Problem
Solution
Discussion
See Also
12.10. Controlling Additions to an ArrayList Through Attributes
Problem
Solution
Discussion
See Also
12.11. Filtering Output when Obtaining Members
Problem
Solution
Discussion
See Also
12.12. Dynamically Invoking Members
Problem
Solution
Discussion
See Also
13. Networking
13.1. Converting an IP Address to a Hostname
Problem
Solution
Discussion
See Also
13.2. Converting a Hostname to an IP Address
Problem
Solution
Discussion
See Also
13.3. Parsing a URI
Problem
Solution
Discussion
See Also
13.4. Forming an Absolute URI
Problem
Solution
Discussion
See Also
13.5. Handling Web Server Errors
Problem
Solution
Discussion
See Also
13.6. Communicating with a Web Server
Problem
Solution
Discussion
See Also
13.7. Going Through a Proxy
Problem
Solution
Discussion
See Also
13.8. Obtaining the HTML from a URL
Problem
Solution
Discussion
See Also
13.9. Writing a TCP Server
Problem
Solution
Discussion
See Also
13.10. Writing a TCP Client
Problem
Solution
Discussion
See Also
13.11. Simulating Form Execution
Problem
Solution
Discussion
See Also
13.12. Downloading Data from a Server
Problem
Solution
Discussion
See Also
13.13. Using Named Pipes to Communicate
Problem
Solution
Discussion
See Also
14. Security
14.1. Controlling Access to Types in aLocal Assembly
Problem
Solution
Discussion
See Also
14.2. Encrypting/Decrypting a String
Problem
Solution
Discussion
See Also
14.3. Encrypting and Decrypting a File
Problem
Solution
Discussion
See Also
14.4. Cleaning Up Cryptography Information
Problem
Solution
Discussion
See Also
14.5. Verifying that a String Is Uncorrupted During Transmission
Problem
Solution
Discussion
See Also
14.6. Wrapping a String Hash for Ease of Use
Problem
Solution
Discussion
See Also
14.7. A Better Random Number Generator
Problem
Solution
Discussion
See Also
14.8. Securely Storing Data
Problem
Solution
Discussion
See Also
14.9. Making a Security Assert Safe
Problem
Solution
Discussion
See Also
14.10. Preventing Malicious Modifications to an Assembly
Problem
Solution
Discussion
See Also
14.11. Verifying that an Assembly Has Been Granted Specific Permissions
Problem
Solution
Discussion
See Also
14.12. Minimizing the Attack Surface of an Assembly
Problem
Solution
Discussion
See Also
15. Threading
15.1. Creating Per-Thread Static Fields
Problem
Solution
Discussion
See Also
15.2. Providing Thread Safe Access to Class Members
Problem
Solution
Discussion
See Also
15.3. Preventing Silent Thread Termination
Problem
Solution
Discussion
See Also
15.4. Polling an Asynchronous Delegate
Problem
Solution
Discussion
See Also
15.5. Timing Out an Asynchronous Delegate
Problem
Solution
Discussion
See Also
15.6. Being Notified of the Completionof an Asynchronous Delegate
Problem
Solution
Discussion
See Also
15.7. Waiting for Worker Thread Completion
Problem
Solution
Discussion
See Also
15.8. Synchronizing the Reading and Writingof a Resource Efficiently
Problem
Solution
Discussion
See Also
15.9. Determining Whether a Requestfor a Pooled Thread Will Be Queued
Problem
Solution
Discussion
See Also
15.10. Waiting for All Threads in theThread Pool to Finish
Problem
Solution
Discussion
See Also
15.11. Configuring a Timer
Problem
Solution
Discussion
See Also
15.12. Storing Thread-Specific Data Privately
Problem
Solution
Discussion
See Also
16. Unsafe Code
16.1. Controlling Changes to Pointers Passedto Methods
Problem
Solution
Discussion
See Also
16.2. Comparing Pointers
Problem
Solution
Discussion
See Also
16.3. Navigating Arrays
Problem
Solution
See Also
16.4. Manipulating a Pointer to a Fixed Array
Problem
Solution
Discussion
See Also
16.5. Returning a Pointer to a Particular Element in an Array
Problem
Solution
Discussion
See Also
16.6. Creating and Using an Array of Pointers
Problem
Solution
Discussion
See Also
16.7. Creating and Using an Array of Pointersto Unknown Types
Problem
Solution
Discussion
See Also
16.8. Switching Unknown Pointer Types
Problem
Solution
Discussion
See Also
16.9. Breaking Up Larger Numbers into Their Equivalent Byte Array Representation
Problem
Solution
Discussion
See Also
16.10. Converting Pointers to a Byte[ ], SByte[ ],or Char[ ] to a String
Problem
Solution
Discussion
See Also
17. XML
17.1. Reading and Accessing XML Datain Document Order
Problem
Solution
Discussion
See Also
17.2. Reading XML on the Web
Problem
Solution
Discussion
See Also
17.3. Querying the Contents of an XML Document
Problem
Solution
Discussion
See Also
17.4. Validating XML
Problem
Solution
Discussion
See Also
17.5. Creating an XML Document Programmatically
Problem
Solution
Discussion
See Also
17.6. Detecting Changes to an XML Document
Problem
Solution
Discussion
See Also
17.7. Handling Invalid Characters in anXML String
Problem
Solution
Discussion
See Also
17.8. Transforming XML to HTML
Problem
Solution
Discussion
See Also
17.9. Tearing Apart an XML Document
Problem
Solution
BillingEnvelope XML
ShippingEnvelope XML
FulfillmentEnvelope XML
Discussion
See Also
17.10. Putting Together an XML Document
Problem
Solution
BillingEnvelope XML
ShippingEnvelope XML
FulfillmentEnvelope XML
Discussion
See Also
Index
Colophon
Search in book...
Toggle Font Controls
Playlists
Add To
Create new playlist
Name your new playlist
Playlist description (optional)
Cancel
Create playlist
Sign In
Email address
Password
Forgot Password?
Create account
Login
or
Continue with Facebook
Continue with Google
Sign Up
Full Name
Email address
Confirm Email Address
Password
Login
Create account
or
Continue with Facebook
Continue with Google
Next
Next Chapter
C# Cookbook
Add Highlight
No Comment
..................Content has been hidden....................
You can't read the all page of ebook, please click
here
login for view all page.
Day Mode
Cloud Mode
Night Mode
Reset