1

Why do I get the CS1612 error on a list of structs with value types as properties but not on an array?

For example:

        List<VertexPositionColorTexture> y = new List<VertexPositionColorTexture>();
        y[0].Position = Vector3.Left; // CS1612
        VertexPositionColorTexture[] x = new VertexPositionColorTexture[4];
        x[0].Position = Vector3.Down; // fine

Both try to modify a value type so why does the array works?

shinzou
  • 5,850
  • 10
  • 60
  • 124
  • Is `VertexPositionColorTexture` struct? – Roman Jan 22 '17 at 21:24
  • @Roma yes. https://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.vertexpositioncolortexture.aspx – shinzou Jan 22 '17 at 21:28
  • yes it is struct thus the error, @kuhaku structs are value type and immutable so you can not change them, i believe when you change field of struct in array it will return a new one, so that has no effect after all. Interesting btw – M.kazem Akhgary Jan 22 '17 at 21:29
  • @M.kazemAkhgary "structs are value type and *immutable*"? Any chance to provide link explaining that? – Alexei Levenkov Jan 22 '17 at 21:33
  • @M.kazemAkhgary so this `x[0].Position = Vector3.Down;` creates a completely new `VertexPositionColorTexture`? – shinzou Jan 22 '17 at 21:34

0 Answers0