For some look-up table calculation, I have an array of values which I'd initially thought to declare in the C/C++ tradition like:
class MyClass
{
UInt16[] _table =
{ ... };
In fact this is C code I am porting to C#. The issue is an array's contents can be modified even if the array is readonly
. I can see there are .Net classes to provide immutable arrays but I am not sure:
- If they can be initialized as easily/simply as a class member
- How much performance hit this might give compared to an array; the whole LUT approach is done for performance in the first place.
Is this a case where I should just trust that nobody will modify the array contents, or is there a neat way to drop in an immutable version?
This is my current thinking:
IList<UInt16> _table = Array.AsReadOnly(new UInt16[]
{ ... });