I have a value type Node and an array grid
private Node[,] grid;
public struct Node {
public byte occupant;
public Direction toTarget;
public float distance;
}
public enum Direction {
UP, LEFT, RIGHT, DOWN
}
How do I reference an element instead of copying it on assignment?
Example
Node node = grid[0,0];
node.occupant = 1;
Doing this copies the value of grid[0,0] into node. I basically want a pointer into the grid array at the point specified so I can modify the Node directly. I'm unable to use unsafe.
Is there some syntax sugar for this or do I have to modify directly? ex:
grid[0,0].occupant = 1;
classinstead ofstruct?structis not immutable, it is rather passed to methods by value, i.e. copied unless method argument isref. Value types are rarely a good choice, though. Examples are game programming, where array of reusable structs avoids garbage collection for entire lifetime of a game. New C# 7 syntax will address precisely that use.