I am using static readonly string arrays in my web application. Basically array is having error codes and i have kept all similar error codes in one array and checking this array instead of checking each one in different constant string.
like
public static readonly string[] myarray = string[] {"232132132","31232132","123123123"}
Please let me know is there any harm in using static readony string arrays ?
Note : I am not facing any error, wanted to know is there any harm or performance issue for using like this?
Try something like this instead:
public static readonly ReadOnlyCollection<string> ErrorList = new ReadOnlyCollection<string>(
new string[] {
"string1",
"string2",
"stringn",
}
);
You'll need to include
the namespace System.Collections.ObjectModel
in order to expose this object. ReadOnlyCollection only implements as getter and your array content cannot be changed.
Well, you should know that it is only the array reference that is readonly, not the array content. So if the array is public (and it sounds like it is), any part of the program could overwrite any or all of the messages, the only thing not possible is resizing the array.
Does that meet your definition of "harm"?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With