Monthly Archives: February 2017

Understand string equality testing in C#

To really understand this example, you need to know about string interning, equality testing, and operator overloading. Interning First, interning. To save space, .NET uses an intern pool to store literal strings at compile time. If the program contains a … Continue reading

Posted in strings, syntax | Tagged , , , , , , , , , , , , | Leave a comment

Use a Dictionary to store and retrieve items in C#

This example demonstrates a Dictionary. A Dictionary is basically a strongly typed Hashtable. 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 … Continue reading

Posted in controls, strings | Tagged , , , , , , , , , , | Leave a comment

Compare the performance of string comparison methods 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 | Tagged , , , , , , , , , | Leave a comment

Compare the performance of switch and if statements in C#

This example compares the performance of switch and if-else statements. Both do roughly the same thing: they check a series of conditions until they find one that is true and then skip the rest. This example compares the speeds of … Continue reading

Posted in performance, syntax | Tagged , , , , , , , , , , , , , , | Leave a comment

Draw, move, and delete line segments in VB.NET

This progra lets you draw, move, and delete line segments from a drawing. It’s a Visual Basic .NET version of the example Draw, move, and delete line segments in C# made by request. Click and drag to draw line segments. … Continue reading

Posted in drawing, graphics | Tagged , , , , , , , , , , , , , , , , , , , , , | 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 into a RectangleF is easy. That makes sense because converting from Rectangle to RectangleF is a … Continue reading

Posted in drawing, graphics | Tagged , , , , , , , , , , | 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 words of certain lengths from a file in … Continue reading

Posted in algorithms, files, games, graphics, strings | Tagged , , , , , , , , , , , , | Leave a comment

Use LINQ to select words of certain lengths from a file in C#

This example uses LINQ to read a file, remove unwanted characters, select words of a specified length, and save the result in a new file. Recently I needed a big word list so I searched around for public domain dictionaries. … Continue reading

Posted in algorithms, files, LINQ | Tagged , , , , , , , , , , , | 2 Comments

Draw a golden spiral in C#

This example shows how to draw a golden spiral (or phi spiral) in C#. The example Draw a nested series of golden rectangles in C# draws nested rectangles and connects their corners to make a square “spiral.” This example makes … Continue reading

Posted in algorithms, drawing, graphics, mathematics | Tagged , , , , , , , , , , , , , , | 3 Comments

Draw a nested series of golden rectangles in C#

This program draws golden rectangles, rectangles whose side ratio is equal to φ. For information on φ, see Examine the relationship between the Fibonacci sequence and phi in C#. The program first draws a golden rectangle. It then removes the … Continue reading

Posted in algorithms, drawing, graphics, mathematics | Tagged , , , , , , , , , , , , | 2 Comments