关于string概念的一个问题......初次发帖,请多多关照!

feifan731 2003-08-20 10:42:48
有个问题想请教:
书上的一段引言:"每个string对象都是一个不变的Unicode字符序列,也就是说,看似要改变字符串的方法实际上返回的是一个修改后的副本,原字符串是不变的..."

问:string string1 = "the first value";string1 = "the changed value";在这段代码中,原字符串是"the first value"吗?当string1="che changed value"执行后,原字符串"the first value"就释放了吗?

对于这段引言我一点也看不明白,请仁兄们详细的解释一下!....
...全文
52 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
CMIC 2003-08-20
  • 打赏
  • 举报
回复
看看这个帖子
http://expert.csdn.net/Expert/topic/1880/1880675.xml?temp=.9158747
feifan731 2003-08-20
  • 打赏
  • 举报
回复
to chagel(.NET-一切到此为止):
希望听一下你的看法。。。。。。
robin_xin_xin 2003-08-20
  • 打赏
  • 举报
回复
up
jjcccc 2003-08-20
  • 打赏
  • 举报
回复
是这意思:
string s1="some string";
string s2=s1;//拷贝s1
s2="another string";
则s1不会改变。若:
string s1="some string";
s1=="another string";
则s1改变。
chagel 2003-08-20
  • 打赏
  • 举报
回复
因为String是引用类型,所以有Jesse Liberty的那段话
建议先去看看类型有关细节
jiezhi 2003-08-20
  • 打赏
  • 举报
回复
它指的是如:
string s="abc";
string s1=s.Substring(1);//s沒有改變,s1得到的是s的副本。
s="cdefg";//s改變了。
jlhdlj 2003-08-20
  • 打赏
  • 举报
回复
Strings in managed applications are instances of System.String, but System.Strings are immutable, meaning once defined, they can’t be changed. The following code works but is inefficient because each concatenation operation results in a memory allocation and a memory copy:

string s = "";
for (int i=1; i<=99; i++) {
s += i.ToString ();
s += ", ";
}
The following code produces the same string, but it does so in a fraction of the time. Why? Because it builds the string in a buffer large enough to hold 512 characters:

StringBuilder sb = new StringBuilder (512);
for (int i=1; i<=99; i++) {
sb.Append (i.ToString ());
sb.Append (", ");
}
string s = sb.ToString ();

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧