Category Archives: extensions

Use string extension methods to validate Social Security numbers in C#

This example defines three extension methods that return true if strings contain Social Security numbers. Recall that you must add extension methods to a static class and that the methods must be static. This example begins by defining a Matches … Continue reading

Posted in extension methods, extensions, regular expressions, strings | Tagged , , , , , , , , , | 2 Comments

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

Make string extensions to URL encode and decode strings in C#

When you URL encode a string, you convert special characters into numeric sequences thath are safe to use in a URL. When you URL decode a string, you reverse the process. The following code creates extension methods for the string … Continue reading

Posted in extension methods, extensions, strings | Tagged , , , , , , , , , , | Leave a comment

Convert between byte arrays and hexadecimal strings in C#

This example uses extension methods to convert between byte arrays and hexadecimal strings. It adds a ToHex extension method to the byte[] type and a ToBytes extension method to the string class. To create an extension method, create a public … Continue reading

Posted in algorithms, cryptography, extensions, strings | Tagged , , , , , , , , , , , , , , , | 1 Comment

Use the BitConverter class to write extension methods to convert between byte arrays and strings of hexadecimal values in C#

This is a new version of the example Write extension methods to convert between byte arrays and strings of hexadecimal values in C# that takes a slightly different approach. The following code shows the new versions of the extension methods. … Continue reading

Posted in extension methods, extensions, strings | Leave a comment