-1

I want to create a read only array in my c# code

 private readonly string[] students = new string[] { "Alice", "John" };

Compiler does not complain about it but I read on the following link that array fields should not be read only. what is the best way of creating a read only array?

https://learn.microsoft.com/en-us/visualstudio/code-quality/ca2105?view=vs-2019

How can I make a read only array? I just need a static array to check against other values

Learn AspNet
  • 1,192
  • 3
  • 34
  • 74

1 Answers1

2

You can have a look at ReadOnlyCollection<T> class

Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
  • An array is readonly in the sense that you can't change the pointer to another array. Since net core 2.1, you could use a `ReadOnlySpan`. – Jeremy Lakeman Dec 20 '19 at 00:49