Why there isn’t a ReadOnlyList class in the System.Collections library of C#?
There is ReadOnlyCollection<T>, which is the generic version of the above. You can create one from a List<T> directly by calling list.AsReadOnly().
There is ReadOnlyCollection<T>, which is the generic version of the above. You can create one from a List<T> directly by calling list.AsReadOnly().
Personally, I think this is better than any of the other answers: static readonly IList<T> EmptyList = new T[0]; Arrays implement IList<T>. You cannot add to an array. You cannot assign to an element in an empty array (because there is none). This is, in my opinion, a lot simpler than new List<T>().AsReadOnly(). You still … Read more
(I assume here that Bar and Baz are both subtypes of Foo.) List<? extends Foo> means a list of elements of some type, which is a subtype of Foo, but we don’t know which type. Examples of such lists would be a ArrayList<Foo>, a LinkedList<Bar> and a ArrayList<Baz>. As we don’t know which subtype is … Read more
You can use the IDictionary<TKey,TValue> interface which provides the Add(KeyValuePair<TKey,TValue>) method: IDictionary<int, string> dictionary = new Dictionary<int, string>(); dictionary.Add(new KeyValuePair<int,string>(0,”0″)); dictionary.Add(new KeyValuePair<int,string>(1,”1″));
This seemed related, but I didn’t understand it properly: c# Dictionary: making the Key case-insensitive through declarations It is indeed related. The solution is to tell the dictionary instance not to use the standard string compare method (which is case sensitive) but rather to use a case insensitive one. This is done using the appropriate … Read more