Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'d like to create a simple class property which can contain multiple values se

ID: 639832 • Letter: I

Question

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.

Explanation / Answer

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);

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote