What speaks against doing this?:
public struct T
{
private float[] Elements { get; set; }
public T(params float[] elements)
{
Elements = elements;
}
}
Could this possibly lead to undefined behaviour? Or will the garbage collector keep the array alive since it is beeing used?
Just to provide an answer and add some details.
This is perfectly legal. The compiler will in effect transform
To
Since arrays are reference types the constructor will just assign a reference. And since references are tracked by the garbage collector there is no risk for a memory leak or other issues that may affect c++.
From the language specification (Emphasis mine)