We need to dispose native collections(e.g. NativeArray,NativeList) when we finished to use them.
And then, C# has language feature for this kind of context, “using”.
But currently, “using” doesn’t work well for when we are writing into their properties…(CS1654 error appears)
But in C#8 with Unity2020.2,we have solution for this issue.
struct NativeCollectionWithReadOnlyProperties :IDisposable
{
public int NonReadOnlyProperty
{
get => default;
set { }
}
public readonly int ReadOnlyProperty
{
get => default;
set { }
}
public readonly int this[int i]
{
get => default;
set { }
}
public readonly int Length
{
get => default;
set { }
}
public readonly int Capacity
{
get => default;
set { }
}
public void Dispose()
{
}
public static void Sample()
{
using (var a = new NativeCollectionWithReadOnlyProperties())
{
a.NonReadOnlyProperty = 0; //Error:CS1654
a.ReadOnlyProperty = 0; //OK!
a[0] = 0; // OK!
a.Length = 0; // OK!
a.Capacity = 0; // OK!
}
}
}
By just marking their properties “readonly”!
This will definitely improve usability of native collections.
Please.