I'm specifically thinking about a collection that fulfills the contract of a set, but I think the question can apply to any kind. Are there collections in the .NET framework that prevent null entries? The specific behavior I want is this:
var set = new HashSet<object>();
bool added = set.Add(null);
Console.WriteLine(added); // prints "False"
This isn't the behavior of the built-in HashSet<T>
. Are there any collections that do have this (or similar) behavior, or am I better off rolling my own? If the latter, what's the best way to go about it? Should I inherit directly from HashSet<T>
or just wrap it?
EDIT: To be clear, this is just idle wondering. Mostly because I can't think of any reason I'd ever want to allow null
into a set of objects. I don't have any particular need for this.