C#中string和StingBuilder内存中的区别实例分析
C#中string和StingBuilder内存中的区别实例分析
发布时间:2016-12-28 来源:查字典编辑
摘要:本文实例分析了C#中string和StingBuilder内存中的区别,有助于更好的掌握C#程序设计中string和StingBuilder...

本文实例分析了C#中string和StingBuilder内存中的区别,有助于更好的掌握C#程序设计中string和StingBuilder的用法。分享给大家供大家参考。具体方法如下:

关于 string和StringBuilder的区别参考MSDN。本文用程序演示它们在内存中的区别,及其因此其行为不同。

先来看看下面这段代码:

//示例: string 的内存模型 namespace ConsoleApplication2 { class Program { static void Main(string[] args) { string a = "1234"; string b = a;//a,and b point to the same address Console.WriteLine(a); Console.WriteLine(b); a = "5678"; Console.WriteLine(a); Console.WriteLine(b);//That b's value is not changed means string's value cann't be changed Console.ReadKey(); } } }

输出:

1234

1234

5678;change a's value,b's value is not changed

1234

//示例: StringBuilder 的内存模型 namespace ConsoleApplication3 { class Program { static void Main(string[] args) { StringBuilder a = new StringBuilder("1234"); StringBuilder b = new StringBuilder(); b = a; a.Clear(); a.Append("5678"); Console.WriteLine(a); Console.WriteLine(b); Console.ReadKey(); } } }

输出:

5678

5678

希望本文所述对大家的C#程序设计有所帮助。

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新C#教程学习
热门C#教程学习
编程开发子分类