2

I have a class that has these 2 fields:

private readonly Memory<byte> data;
public Memory<byte> Reserved1 => data.Slice(0, 2);

ReSharper is giving me a warning with the Slice: Impure method is called for readonly field of value type

Removing readonly from the data declaration fixes the warning, but I'd like to understand if this warning is a false positive in this situation or if the warning is valid and I should avoid setting data readonly. Any advice?

Endel
  • 21
  • 1

1 Answers1

1

If i understand this correctly,

Memory<T> is a readonly struct anyway, and its fields are immutable. Though the memory it points to is not.

Mutations on the slice will be reflected in the memory it points to in data, which is what i assume you want. The warnings in this case can be ignored, with

 // ReSharper disable once ImpureMethodCallOnReadonlyValueField

Also there is a relatively famous question answered by Jon Skeet, and Eric Lippert talking about impure methods and their ramifications on structs

Impure method is called for readonly field

TheGeneral
  • 79,002
  • 9
  • 103
  • 141