Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The C# compiler will give you the following error:</p> <blockquote> <p>Cannot modify the return value of 'System.Collections.Generic.List.this[int]' because it is not a variable</p> </blockquote> <p>The reason is that structs are value types so when you access a list element you will in fact access an intermediate copy of the element which has been returned by the indexer of the list. </p> <p>From <a href="http://msdn.microsoft.com/en-us/library/wydkhw2c(VS.71).aspx" rel="noreferrer">MSDN</a>:</p> <blockquote> <p><strong>Error Message</strong></p> <p>Cannot modify the return value of 'expression' because it is not a variable</p> <p>An attempt was made to modify a value type that was the result of an intermediate expression. Because the value is not persisted, the value will be unchanged.</p> <p>To resolve this error, store the result of the expression in an intermediate value, or use a reference type for the intermediate expression.</p> </blockquote> <p>Solutions:</p> <ol> <li>Use an array. This gives you direct access to the elements (you are not accessing a copy)</li> <li>When you make Map a class you can still use a List to store your element. You will then get a reference to a Map object instead of an intermediate copy and you will be able to modify the object.</li> <li>If you cannot change Map from struct to a class you must save the list item in a temporary variable:</li> </ol> <p>&nbsp;</p> <pre><code>List&lt;Map&gt; list = new List&lt;Map&gt;() { new Map(10), new Map(20), new Map(30), new Map(40) }; Map map = list[2]; map.Size = 42; list[2] = map; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload