C#:哪个使用更多的内存开销?包含单词序列的字符串或字符?
我知道一个char基本上是每个char组成一个单词的数组(如果不止一个char),但这最终会导致你更多的内存开销,就像使用字符串而不是char来保存单词的长度一样?
这将取决于您使用该问题的次数..Net执行字符串的方式是,如果您有多个具有相同值的字符串,则该值仅存储在内存中一次,并且每个字符串变量指向该值.但是,即使char数组的内容相同,使用的每个char数组也都有自己的内存分配.
例如
//.Net Framework allocates memory space for "word" only once, all the three variables refer to this chunk of memory
String s1, s2, s3;
s1 = "word";
s2 = "word";
s3 = "word";
//.Net Framework allocates memory for each array
char[] c1 = new char[] { 'w','o','r','d'};
char[] c2 = new char[] { 'w','o','r','d'};
char[] c3 = new char[] { 'w','o','r','d'};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1951 次 |
| 最近记录: |