VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > temp > C#教程 >
  • 2. C# 中的 string、String、StringBuffer 内存处理性能 和 应用场景(1223)

一、System.String

string 等价于 System.String

string 是 System.String 的别名。

 声明:

复制代码
 1 // 仅仅是一个声明,内存中并没有分配用来存储值的空间。
 2 string a1 = null;
 3 
 4 
 5 // 等价于 "",以新建字符串长度为零的 String 对象,可以减少 NullReferenceException 发生的可能性。
 6 string a1 = String.Empty;
 7 
 8 
 9 // 在内存中分配了一个连续性的不可变的储存空间。
10 System.String greeting = "Hello World!";
11 
12 
13 // 长字符串字面量拆分为较短的字符串,从而提高源代码的可读性。
14 // 以下代码将较短的字符串连接起来,以创建长字符串字面量。 
15 // 在编译时将这些部分连接成一个字符串。 无论涉及到多少个字符串,均不产生运行时性能开销。
16 string text = "Historically, the world of data " +
17 "and the world of objects have not been well " +
18 "integrated. Programmers work in C# or Visual " +
19 "Basic and also in SQL or XQuery. On the one " +
20 "side are concepts such as classes, objects, " +
21 "fields, inheritance, and .NET Framework APIs. " +
22 "On the other side are tables, columns, rows, " +
23 "nodes, and separate languages for dealing with " +
24 "them. ";
复制代码

 

System.String 是不可变类型。 也就是说,出现的用于修改对象的每个操作 System.String 实际上都会创建一个新的字符串。

如下举例:

复制代码
 1 // 初始化 s1
 2 // 内存为 s1 分配了存放字符的空间
 3 string s1 = "A string is more ";
 4 
 5 // 初始化 s2
 6 // 内存为 s2 分配了存放字符的空间
 7 string s2 = "than the sum of its chars.";
 8 
 9 // 改变 s1 的值:
10 // 内存为 s1 另外重新分配了新值的空间
11 // 并指向新空间的地址,所以 s1 有了新的值
12 // 无用的s1旧值及空间等待GC垃圾回收
13 s1 += s2;
14 
15 System.Console.WriteLine(s1);
复制代码

 以上,假设更多的+或+=等的多次赋值s1,那么 内存在不停的开辟新的空间存放新值赋予s1 ... ... 每次再需要GC分配、压缩、清理... ...

对于此不可变类型,若执行大量字符串操作的例程 (例如在循环中多次修改字符串的应用程序) ,重复修改字符串可能会显著降低性能。

 

 

二、System.Text.StringBuilder

System.Text.StringBuilder对象在缓冲区操作,是一个可变字符串类,缓冲区可随时增加或减少或删除或替换等的字符串的长度。

复制代码
StringBuilder strb = new StringBuilder();
strb.Append('*', 10).Append(" Adding Text to a StringBuilder Object ").Append('*', 10);
strb.AppendLine("\n");
strb.AppendLine("Some code points and their corresponding characters:");
for (int ctr = 50; ctr <= 60; ctr++) {
    strb.AppendFormat("{0,12:X4} {1,12}", ctr,Convert.ToChar(ctr));
    strb.AppendLine();
}
复制代码

 

 

三、适用场景

\ string、String StringBuilder

对比

连续性的不可变的内存空间长度;

每次开辟新内存空间,存放变更后的值,原空间等待释放;

借助IndexOf、Substring等进行字符搜索。

缓冲区可伸缩的空间长度;

原空间中维护字符长度,不创建新空间;

(Chars[])字符搜索方式复杂。

场景

字符更改次数很少;

固定数量的拼接字符串联操作;

大量的字符串搜索。

大量的未知的不确定的更改次数;

字符串非搜索场景。


相关教程