静态只读字符串数组 [英] Static readonly string arrays
问题描述
我在我的web应用程序中使用静态只读字符串数组。
基本上阵列是有错误代码和我一直都类似的错误代码在一个阵列并检查该数组而不是检查每一个不同的常量字符串的。
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.
像
public static readonly string[] myarray = string[] {"232132132","31232132","123123123"}
请让我知道有没有在使用静态readony字符串数组危害?
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",
}
);
您需要包含
命名空间 System.Collections.ObjectModel
为了揭露这个对象。 ReadOnlyCollection还只是实现了一个getter和阵列的内容不能更改。
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.
这篇关于静态只读字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!