Category Archives: extension methods

Use an improved extension method to calculate standard deviation in C#

The example Calculate standard deviation in C# makes an extension method that calculates standard deviation for an IEnumerable of integers. Unfortunately because that example works with integers, it cannot work with other data types. Rian Meier showed me this method, … Continue reading

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

Remove non-printable ASCII characters from a string in C#

The following TrimNonAscii extension method removes the non-printable ASCII characters from a string. public static string TrimNonAscii(this string value) { string pattern = “[^ -~]+”; Regex reg_exp = new Regex(pattern); return reg_exp.Replace(value, “”); } In ASCII, the printable characters lie … Continue reading

Posted in extension methods, strings | Tagged , , , , , , , , , , , , | 7 Comments

Sort ListView controls by all columns or clicked columns in C#

The ListView control provides features to sort its values but by default it doesn’t use them. This example shows how you can add extension methods to the control to make it sort on all columns or on clicked columns. This … Continue reading

Posted in controls, extension methods | Tagged , , , , , , , , , , , , | 1 Comment

Add “extension properties” to classes in C#

Extension methods allow you to add new methods to existing classes even if you don’t have access to the classes’ source code. For example, the Randomize two-dimensional arrays in C# example shows how to add an extension method to two-dimensional … Continue reading

Posted in algorithms, classes, extension methods | Tagged , , , , , , , , , | 1 Comment

Randomize two-dimensional arrays in C#

This example uses the following extension method to randomize two-dimensional arrays. // Randomize a 2D array. public static void Randomize<T>(this T[,] values) { // Get the dimensions. int num_rows = values.GetUpperBound(0) + 1; int num_cols = values.GetUpperBound(1) + 1; int … Continue reading

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

Add an extension method to the Graphics class in C#

For some strange reason, the Graphics class’s DrawRectangle method cannot take a RectangleF as a parameter. It can take a Rectangle or four float values, and the FillRectangle method can take a RectangleF as a parameter, but the DrawRectangle method … Continue reading

Posted in drawing, extension methods, extensions, graphics | Tagged , , , , , , , , , , , , | Leave a comment

Display map distance with limited significant digits in C#

The post Measure distance on a map with a scale in C# lets you measure a distance on a map. However, it displays too many significant digits in the results to look nice. For example, the sample picture displays the … Continue reading

Posted in algorithms, extension methods, graphics, mathematics, strings | Tagged , , , , , , , , , , , , , , , , | Leave a comment

Display significant digits in C#

The number of significant digits in a number is the number of digits that carry meaningful information. These include all of the digits displayed except leading and trailing zeros. A decimal point and trailing zeros after the decimal point imply … Continue reading

Posted in algorithms, extension methods, mathematics, strings | Tagged , , , , , , , , , , | Leave a comment

Make extension methods that convert to and from Roman numerals in C#

The example Convert to and from Roman numerals in C# uses the RomanToArabic and ArabicToRoman methods to convert between Arabic and Roman numerals. See that example for a description of those methods. This example creates extension methods to make the … Continue reading

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

Make a ComboBox display images with text in C#

The example Make a ComboBox display colors or images in C# shows how to make an owner-drawn ComboBox that displays either a series of color samples or a list of images. This example shows how to make a ComboBox that … Continue reading

Posted in controls, extension methods, graphics | Tagged , , , , , , , , , , , , | 6 Comments