Is there any way (possibly a dirty hack) to create an ImmutableArray which will just use a specified array, instead of copying it over?
I have an array which I know won't change, and I want to create an ImmutableArray to allow clients to access my array safely.
Looking at the source code for ImmutableArray, I see the Create method will not help:
public static ImmutableArray<T> Create<T>(params T[] items)
{
if (items == null)
{
return Create<T>();
}
// We can't trust that the array passed in will never be mutated by the caller.
// The caller may have passed in an array explicitly (not relying on compiler params keyword)
// and could then change the array after the call, thereby violating the immutable
// guarantee provided by this struct. So we always copy the array to ensure it won't ever change.
return CreateDefensiveCopy(items);
}
Edit: There is a request for exactly this feature on GitHub, which also gives the quickest hack to use: https://github.com/dotnet/corefx/issues/28064