Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read-only list of strings

Is it good to use such approach for keeping read-only list of string, for example, a list of fields in ADO.NET.

var list = new System.Collections.ObjectModel.ReadOnlyCollection<string>(
           new List<string>(4) { "type", "currency", "date", "amount" });

Or this is a superfluous solution?

like image 224
abatishchev Avatar asked Jul 06 '09 21:07

abatishchev


1 Answers

Depends on your position. ReadOnlyCollection works very well when you have an internal modifiable list that needs to be given to some other code that isn't allowed to change the list. For simple code, using ReadOnlyCollection is probably overkill. If the code is large and there's a risk some other programmer (or you, down the road) might accidentally attempt to modify the list, then it might be worth it.

In short: don't use ReadOnlyCollection idiomatically, use it when it makes sense.

like image 176
John Calsbeek Avatar answered Sep 20 '22 18:09

John Calsbeek