Monthly Archives: April 2012

Use a Dictionary object to store and look up items in C#

A Hashtable stores key/value pairs in a way that makes looking up values extremely fast. For example, suppose you have an employee database that uses EmployeeId values to look up Employee object. If you use a Hashtable to store the … Continue reading

Posted in controls, strings | Leave a comment

Compare the speeds of various methods for comparing strings in C#

This example uses the following code to make four strings: string value0 = “AAAAAAAAAAAAAAAAAAAAAA”; string value1 = “AAAAAAAAAAAAAAAAAAAAAA”; string value2 = “AAAAAAAAAAAAAAAAAAAAAB”; string value3 = “BAAAAAAAAAAAAAAAAAAAAA”; It then uses several methods for comparing the first string with the others. For … Continue reading

Posted in performance, strings | Leave a comment

Compare the speeds of a switch statement and a series of if-then statements in C#

A switch statement and a series of if then statements have the same effect: both check a series of conditions until they find one that it true and then skip the rest. This example compares the speeds of these two … Continue reading

Posted in performance, syntax | Leave a comment

Convert a Rectangle into a RectangleF and vice versa in C#

The RectangleF structure has an overloaded assignment operator = that lets you simply set a RectangleF equal to a Rectangle so converting a Rectangle to a RectangleF is easy. Converting a RectangleF to a Rectangle is only slightly harder. The … Continue reading

Posted in graphics | Leave a comment

Make a hangman game in C#

Special thanks to Jeff Scarterfield for the skeleton drawing used by the program. This example builds a simple hangman game that uses the dictionary created by the example Use LINQ to select specific words from a file to build a … Continue reading

Posted in algorithms, games, strings | Leave a comment

Use LINQ to select specific words from a file to build a dictionary in C#

Recently I needed a big word list so I searched around for public domain dictionaries. I found one that was close to what I needed in the file 6of12.txt in the 12Dicts package available here. That file has several problems … Continue reading

Posted in algorithms, files, LINQ | Leave a comment

Draw a nested series of golden rectangles in C#

This program draws a golden rectangle, one whose side ratio is equal to ?. For information on ?, see Examine the relationship between the Fibonacci sequence and phi in C#. The program then removes the square from one end of … Continue reading

Posted in algorithms, graphics, mathematics | 2 Comments

Examine the relationship between the Fibonacci sequence and phi in C#

The golden ratio (also called the golden mean, golden section, or divine proportion) is a number that pops up in a surprising number of places in geometry and nature. This number is also called ? (phi, pronounced either “fee” or … Continue reading

Posted in algorithms, graphics, mathematics | Leave a comment

Use the TextFieldParser class to easily read a file containing delimited data in C#

This example includes a data file that contains name and address data. The data contains fields delimited by commas and semi-colons. Some records also have ZIP+4 format ZIP codes (for example, 08109-2120) and the program should discard the +4 part … Continue reading

Posted in classes, files, parsing | Leave a comment

Make the computer suspend or hibernate in C#

The Application.SetSuspendState lets an application make the system suspend or hibernate. A suspended system enters a low power mode. A hibernating system saves its memory contents to disk and then enters low power mode. Because it doesn’t need to restore … Continue reading

Posted in system | Leave a comment