1

I have the following code which does not compile cause of this error:

Error 5 Cannot modify the return value of 'System.Collections.Generic.List.this[int]' because it is not a variable

The code looks like this:

List<myStruct> myList = new List<myStruct> { new myStruct { a = 3 } };
myList[0].a = 5;

struct myStruct
{
    public double a;
}

However when myStruct is a class this compiles. I assume it has something to do with struct being an immutable object whereas a class-instance is per se a "dynamic, mutable" object, maybe there are some better explenations for this.

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
  • 1
    @Sriram beat me by two seconds. Also see [this](http://stackoverflow.com/questions/1747654/cannot-modify-the-return-value-error-c-sharp) – Yuval Itzchakov Aug 28 '15 at 09:31

0 Answers0