Is it possible to somehow mark a System.Array as immutable. When put behind a public-get/private-set they can\'t be added to, since it requires re-allocation a
The Framework Design Guidelines suggest returning a copy of the Array. That way, consumers can't change items from the array.
// bad code
// could still do Path.InvalidPathChars[0] = 'A';
public sealed class Path {
public static readonly char[] InvalidPathChars =
{ '\"', '<', '>', '|' };
}
these are better:
public static ReadOnlyCollection GetInvalidPathChars(){
return Array.AsReadOnly(InvalidPathChars);
}
public static char[] GetInvalidPathChars(){
return (char[])InvalidPathChars.Clone();
}
The examples are straight from the book.