www.gibmonks.com




Previous Page
Next Page

Book Cover
C# Cookbook, 2nd Edition
By Jay Hilyard, Stephen Teilhet
...............................................
Publisher: O'Reilly
Pub Date: January 2006
Print ISBN-10: 0-596-10063-9
Print ISBN-13: 978-0-59-610063-6
Pages: 1184
 



Table of Contents  | Index

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

Previous Page
Next Page