Ref<T>
Compared to C++, GetComponent and similar functions that return a DataComponent in C# actually return a Ref<T>.
The reason for this is that the memory of the components are purposefully managed by ERS.
The bindings get a reference to this memory and operate on it directly.
In the case of C# it was necessary to add the Ref<T> wrapper to prevent accidentally copying the component.
If the component were to be copied, the program would be operating on the copy, which would not be used in the simulation.
You can access the component inside the Ref<T> via the Value member variable:
Ref<T> transform = entity.GetComponent<TransformComponent>();
Vector2 pos = transform.Value.GetPosition2D();
Do not store the component!
Do not store the component returned from Value, because it will create a copy.
Ref<T> transform = entity.GetComponent<TransformComponent>();
TransformComponent copy = transform.Value; // Creates a copy!
Using the ref keyword does preserve the reference, but we recommend always using the Ref<T> to access the component.
Ref<T> transform = entity.GetComponent<TransformComponent>();
// NOT recommended:
ref TransformComponent reference = ref transform.Value; // Reference preserved
reference.SetPositionX(1);
reference.SetPositionY(2);
// Recommended:
transform.Value.SetPositionX(1);
transform.Value.SetPositionY(2);