What is a read only collection in C#?

Posted by acidzombie24 on Stack Overflow See other posts from Stack Overflow or by acidzombie24
Published on 2010-04-21T06:43:42Z Indexed on 2010/04/21 6:53 UTC
Read the original article Hit count: 289

Filed under:
|
|

I ran a security code analyst i found myself having a CA2105 warning. I looked at the grade tampering example. I didnt realize you can assign int[] to a readonly int. I thought readonly was like the C++ const and makes it illegal.

The How to Fix Violations suggest i clone the object (which i dont want to do) or 'Replace the array with a strongly typed collection that cannot be changed'. I clicked the link and see 'ArrayList' and adding each element one by one and it doesnt look like you can prevent something adding more.

So when i have this piece of code what is the easiest or best way to make it a read only collection?

public static readonly string[] example = { "a", "b", "sfsdg", "sdgfhf", "erfdgf", "last one"};

© Stack Overflow or respective owner

Related posts about c#

Related posts about readonly