.NET Framework standard container type for multi-value property?

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
  • 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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật