Tag Archives: example program

Make a dice-rolling simulation in C#

This example performs a simulation of rolling two six-sided dice. It’s related to my book Interview Puzzles Dissected, which includes several examples that involve rolling dice. When you enter a number of trials and click Roll, the program uses the … Continue reading

Posted in algorithms, graphics, mathematics | Tagged , , , , , , , , , , , , | Leave a comment

Tile a PictureBox in C#

If you set a form’s BackgroundImageLayout property Tile, then the control fills itself with copies of its background image. Strangely the PictureBox control does not have a corresponding Tile property. This example shows one way you can tile images on … Continue reading

Posted in drawing, graphics, image processing | Tagged , , , , , , , , , , , | Leave a comment

Use a symbiote to graphically select hours in C#

This example modifies the previous example Graphically select hours in C# so it uses a symbiote class. That example shows how to use a PictureBox to let the user select a range of hours. If you want to let the … Continue reading

Posted in controls, graphics, user interface | Tagged , , , , , , , , , , , , | 1 Comment

Graphically select hours in C#

The DateTimePicker control lets you select dates and date ranges, but there’s no standard control that lets you select hours. This example shows one way to do this with a PictureBox. A big part of this program is the DrawHours … Continue reading

Posted in controls, graphics, user interface | Tagged , , , , , , , , , , , | 1 Comment

Remove unnecessary using directives in C#

The top of a C# code file usually includes series of using directives to tell Visual Studio which namespaces are used by the code. When you first create a project, the code files include lots of using directives that might … Continue reading

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

Draw the Weierstrass function in C#

The Weierstrass function was discovered by Karl Weierstrass in 1872. It’s an odd function that is continuous everywhere but differentiable nowhere. I’ll explain what that means shortly. Here’s the function: Here: The following two sections explain what it means to … Continue reading

Posted in algorithms, drawing, fractals, graphics, mathematics | Tagged , , , , , , , , , , , , , , , | Leave a comment

Determine whether a file or directory exists in C#

It’s usually more efficient to check whether a file or directory exists before you try to access it. For example, if you try to read a file that doesn’t exist, the system needs to create error handling objects and a … Continue reading

Posted in files | Tagged , , , , , , , | Leave a comment

Calculate Fibonacci numbers in several ways in C#

For some background on Fibonacci numbers and φ, see Examine the relationship between the Fibonacci sequence and phi in C#. This example shows several ways to calculate Fibonacci numbers. While this is mostly for curiosity’s sake, this example does demonstrate … Continue reading

Posted in algorithms, mathematics | Tagged , , , , , , , , , , | Leave a comment

Recursively draw equations in C#

The basic idea for recursively drawing equations is simple. Well, sort of simple. Classes represent different kinds of equations. A particular class knows how to draw its kind of equation. It uses other classes to draw its pieces. For example, … Continue reading

Posted in algorithms, drawing, fonts, graphics, recursion | Tagged , , , , , , , , , , , | 2 Comments

Efficiently see if a string is blank in C#

This example uses three strings to compare different methods for determining whether a string is blank. One of the methods, the string class’s IsNullOrWhiteSpace method, only works in .NET Framework 4.5 or later, so this example targets that version of … Continue reading

Posted in performance, strings | Tagged , , , , , , , , , , , , , , | 5 Comments