Search

Advertising

Home Downloads Components

Components

Folder Path: \ Components \

File: Read-Only Collections & Dictionary

file.png
Uploaded:
October.04.09
Modified:
October.04.09
File Size:
8 KB
Downloads:
597
Version
1.0

Read-Only wrappers for IList<>, ICollection<> and ICollection<>. Analogous to the ReadOnlyDictionary<> class. All wrappers fully implement all relevant generic and non-generic collection interfaces. Full unit test coverage for every single method.

Details

The .NET Framework provides several collection classes in the System.Collections.Generic namespace as well as some base classes for you to implement your own collection classes in the System.Collections.ObjectModel namespace. Between these also is a read-only wrapper for the ICollection<> interface. This read-only wrapper allows you to publish a collection of items from an object without allowing its users to modify that collection (even though the object itself can modify the collection).

Quite useful, for example, if a list just represents an internal state that must not be modified. But then you find out that the ReadOnlyCollection<> class actually wraps an IList<>, not an ICollection<>, requiring you to support index-based access any time you want expose a collection. And there's no class at all for wrapping a Dictionary<>. So now you could do a quick hack job, writing your own read-only wrapper for IDictionary<> or ICollection<> with just the handful of methods you use in your project.

Or use mine! I've created three wrappers, ReadOnlyList<> for indexable collections (which is what .NET's ReadOnlyCollection<> should have been called in my humble opinion), ReadOnlyCollection<> for non-indexable collections and ReadOnlyDictionary<> for associative collections. All three fully implement the relevant .NET collection interfaces in their generic and older non-generic form. And they've got 100% unit test coverage, so you can be sure there are no nasty surprises in any of the methods.

Example

// Create a test dictionary containing some useless items
Dictionary<int, string> test = new Dictionary<int, string>();
test.Add(1, "one");
test.Add(2, "two");
test.Add(3, "three");

// Now wrap the dictionary in a read-only wrapper
// This works exactly like System.Collections.ObjectModel.ReadOnlyCollection,
// but wraps dictionaries.
ReadOnlyDictionary<int, string> readOnlyTest =
  new ReadOnlyDictionary<int, string>(test);

// Check the .IsReadOnly property of the dictionary
Console.WriteLine("Dictionary.IsReadOnly: " + readOnlyTest.IsReadOnly);

// Try to change something through the read-only wrapper.
// Notice that we have to cast to IDictionary<> because the ReadOnlyDictionary's
// default interface omits the unsupported methods.
try {
  ((IDictionary<int, string>)readOnlyTest)[1] = "six";
  Console.WriteLine("Dictionary modification succeeded");
}
catch(NotSupportedException) {
  Console.WriteLine("Dictionary modification not supported");
}

Features

  • Individual read-only wrappers for IList<>, ICollection<> and IDictionary<>
  • Wrappers fully implement all relevant generic and non-generic collection interfaces
  • 100% unit test coverage for all code
  • No dependencies, all three wrappers can be plugged into any project
  • Default interface also omits all methods that modify the collection

These classes are part of the Nuclex.Support library from the Nuclex Framework. You can find the most recent release of the code on the framework's CodePlex site: http://nuclexframework.codeplex.com/



Joomla Template by Joomlashack