I often use IReadOnlyList<double>
for arrays of doubles (or other value types) to indicate that sure data is not to be modified:
class X {
public void ProcessSomePreviouslyCalculatedResults(IReadOnlyList<double> data) {
// do something with data
}
I know that there is ImmutableArray<T>
, which guarantees that the content cannot be modified (unlike IReadOnlyList<double>
which could be casted to double[]
) but introduces another level of indirection. I wonder if ReadOnlyMemory<T>
would be a better choice. In the documentation and the guidelines the motivation for using (ReadOnly
)Memory<T>
is always the replacement for (ReadOnly
)Span<T>
at places where the latter cannot be used.
Are there any drawbacks for using ReadOnlyMemory<T>
at places where I want to pass a readonly array around?
One thing I notice is that one has to use
MemoryMarshal.ToEnumerable(readOnlyMemoryInstance).Select(...)
instead of
ireadOnlyListInstance.Select(...)