I’d like to create a simple class property which can contain multiple values set from the outside. (Values are of the same type.)
Example of property name and contained items:
KnownHidScanners
"\?ACPI#IDEA0100#4&b74a345&0#{884b96c3-56ef-11d1-bc8c-00a0c91405dd}"
"\?HID#VID_045E&PID_071D&MI_00#9&e6a1767&0&0000#{884b96c3-56ef-11d1-bc8c-00a0c91405dd}"
Is there a standard approach in .NET for data type of such a multi-value class property?
When searching MSDN for multi-value property, I can find Properties with Multiple Values concept article which suggests PropertyValueCollection data type. But if you check the data type closer, it is too tightly related to ActiveDirectory.
Off hand I can implement property as
- array of type
- collection of type
- list of type
but which one is the most used approach in out-of-the-box .net objects when they need multiple-value properties? I’m not long enough with .NET ecosystem to observe what is generally used.
Note: this is not an opinion-based question. I’m asking developers who are long enough with the framework which approach is already used so I can get consistent with standards.
Edit: clarity was improved after first answerer wrote he misread the question
4
For a property that with string values like that, I would have:
List<string> KnownHidScanners = new List<string>();
However, if you’re going to expose this property externally as an API, there is:
ICollection<string> KnownHidScanners = new List<string>();
or more appropriately is to create your own type that inherits from ICollection like:
public class ScannerCollection : ICollection<string>
{
//Class definition and implementation of ICollection<T>.
}
See ICollection(T) Interface documentation.
If you are exposing the property externally, but you don’t want the user to be able to modify the collection, you should then use the ReadOnlyCollection(T)
class like:
ReadOnlyCollection<string> KnownHidScanners = new ReadOnlyCollection<string>(myPrivateListOfScanners);
4
With modern .NET / C# we try to use generics. Check out System.Collections.Generic namespace
I recommend List<T>
until you find something it doesn’t do for you.
If you need random access of more than 3 values, go with a Dictionary<K,T>
For a small number of values where you always know where they are, you can use Tuples (such as a 3D coordinate, or a complex key, if there wasn’t another data structure to use)
1
Okay, in the meantime I’ve invested additional time and made my own study on the topic.
Findings:
-
checking through Object inspector how properties containing word “Items” are declared in existing libraries reveals somewhat a surprise – I found every form: array, collection, list and custom types
-
dominant types are these two: collections (generic or inherited) and lists (mostly generic)
-
answers in this question shed light when to prefer collection and when list. The key is in character of the class: collections are recommended for exposed classes (API etc.), lists are more preferred for internal implementation stuff
- here must be noted that
Collection<T>
is no longer part of System.Collections.Generic since .NET .3.5., it’s been moved to System.Collections.ObjectModel
- here must be noted that
-
if property will have some obvious benefit from some specific type, then ignore the above recommendation and use that type. Example is out-of-the-box
String.Chars
which is an array. -
defining custom multi-value container type (with item list based on some of the above) has its place if I want to add some custom properties/methods to items or for manipulation with the list etc.
-
some Property design guidelines are also available from the Microsoft, but for this topic they are not very helpful
2