0

How can I change a first value in this collection?

ICollection<string> vals = new List<string>();

This does not work for the collection:

vals[0] = "xxx";
Banana Cake
  • 1,172
  • 1
  • 12
  • 31
  • It is because there is no indexer property created on ICollection – Vikas Gupta Apr 15 '19 at 09:54
  • you can get first object in your collection with Linq `vals.First()` but strings are immutable, so you can't change them – vasily.sib Apr 15 '19 at 09:55
  • 1
    Honestly, while the duplicate does answer your _specific_ question I'd **strongly** consider not using `ICollection`. `ICollection` says **I don't care about indexing or order** - and yet the very nature of your question shows you **do** care about indexing and order. The short answer is - leave it as a `IList` / `List` rather than a `ICollection`. – mjwills Apr 15 '19 at 09:57

0 Answers0