What is Spackle?

Spackle is a project that contains a number of helper methods I've used to supplement the core classes in .NET. Following is a brief list of code examples that illustrate what you can do with Spackle. Note that this list all-inclusive - you're encouraged to look through the unit tests to see what else Spackle has.

Collection Manipulation

You can perform rotations, shuffling, and element swapping with IList<T>-based objects:

var items = new List<string> { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j" };
items.Rotate(7, RotateDirection.Negative);
// items is now { "h", "i", "j", "a", "b", "c", "d", "e", "f", "g" }
items.Swap(2, 4);
// items is now { "h", "i", "b", "a", "j", "c", "d", "e", "f", "g" }
// items are now in a different order (hopefully!),
// something like { "c", "a", "b", "g", "d", "f", "e", "i", "j", "h" }

You can also create a read-only collection from an enumeration:

var collection = new HashSet<string> { "A", "B", "A" }.AsReadOnly();


This is a powerful way to change the value of a property, field, or local variable in a scope (via the use of the using keyword):

using(Cursors.Wait.Bind(() => someWindow.Cursor))
  // ...

Timing Extension

If you've ever wanted to do a quick performance test on a piece of code, look no further than the Time() extension method:

Console.Out.WriteLine(new Action<int, int, int>(
  (arg1, arg2, arg3) =>
    var x = arg1 + arg2 + arg3;
  }).Time(0, 0, 0).TotalMilliseconds);

String Extensions

Ever want to change a string to a Uri?


Random Value Generation

The RandomObjectGenerator class give you random data, which is especially nice for unit testing so your tests aren't dependent on specific values:

var generator = new RandomObjectGenerator();
var intValue = generator.Generate<int>();
var bigIntegerValue = generator.Generate<BigInteger>();
var stringValue = generator.Generate<string>();


Spackle defines a generic Range class to define and intersect other ranges:

var range = new Range<int>(3, 10);
var isInRange = range.Contains(5); // isInRange is true
var newRange = range.Intersect(new Range<int>(5, 12)); // newRange is (5, 10)

Secure, Random Generators

The SecureRandom class combines the simplicity of the Random class with the security of the RandomNumberGenerator.

Where can I get Spackle?

The latest release of Spackle is available through NuGet (search for Spackle). You can also download it via the CodePlex site.

Last edited Feb 14, 2011 at 12:16 AM by jasonrbock, version 3


No comments yet.