How do I send out simple debug messages to help with my debugging?

Visual Studio offers tons of useful debugging features and allows you to step through your code line-by-line. However, there are times when you don’t want to step through your application, but want to make it output simple text strings with variable values, etc. Enter the System.Diagnostics.Debug class and its Write* methods. By using the Debug… Read more

How do I calculate a MD5 hash from a string?

It is a common practice to store passwords in databases using a hash. MD5 (defined in RFC 1321) is a common hash algorithm, and using it from C# is easy. Here’s an implementation of a method that converts a string to an MD5 hash, which is a 32-character string of hexadecimal numbers.public string CalculateMD5Hash(string input)… Read more

How do I play default Windows sounds?

Sometimes, you might want to make your application a bit more audible. If you are using .NET 2.0, you can utilize the new System.Media namespace and its SystemSound and SystemSounds classes. The SystemSounds class contains five static properties that you can use to retrieve instances of the SystemSound class. This class in turn contains the… Read more

How can I easily log a message to a file for debugging purposes?

Often, you need a way to monitor your applications once they are running on the server or even at the customer site — away from your Visual Studio debugger. In those situations, it is often helpful to have a simple routine that you can use to log messages to a text file for later analysis…. Read more

How can I speed up hashtable lookups with struct object as keys?

When you have struct objects as the key in a hashtable, the lookup operation of the hashtable performs miserably. This can be attributes to the GetHashCode() function which is used internally to do the lookup. If a struct contains only simple value types (int, short, etc.), the algorithm which computes the GetHashCode creates hashes which… Read more