I am trying to create a .NET API which wraps and interfaces with a third-party C API. As such the semantics of the API are as follows:
There is a property which represents a list of strings in a binary file. There is a distinction to be drawn between this property having a list with zero entries, in which case the property will be written to the file with an empty list; and the list being omitted altogether, in which case the property will be omitted from the file.
In my current design, I'm using something like this:
public class InfoWrapper
{
// Use an IList<T> to avoid running afoul of FxCop
// rule CA1002: Do not expose generic lists.
public IList<string> ItemsContainer { get; set; }
}
This would do the trick, of course, allowing me to distinguish between the "not set" and "empty list" cases, but the real problem is that it triggers another FxCop warning, CA2227: Collection properties should be read only. I need to be able to allow the user to set the property to a null
value, and potentially back to a list if they change their mind after setting it to null
. I could make the property read-only, with a pair of methods (say, DeleteItemsContainer()
and CreateNewItemsContainer()
), but in reality, I have several of these properties I need to handle the same way. Adding two methods per property is going to clutter up the API considerably. What would be a better approach here?
N.B. I realize I could just suppress the FxCop warning and be done with it, and that those rules are just one set of suggestions. However, I'd like to stay within those guidelines if possible.