Search code examples
c#liststructimmutabilitymutable

Is it possible to access a reference of a struct from a List<T> to make changes?


I have a struct which I put in a List<T>, I want to edit some value in that struct at a specific position. Is this at all possible without making a copy of the struct, editing the copy, and replacing the entry in the List?


Solution

  • No, to be able to do it you need reference to element of inner array which is not provided by List/IList.

    You can do that with unsafe code and arrays if you have to.