C#中字符串的不可变 [英] Immutable of string in c#

查看:130
本文介绍了C#中字符串的不可变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi
任何人都可以向我解释字符串在c#中是不可变的"吗?

hi
Can any one explain me "string immutable in c#"

推荐答案

所有.NET字符串都是不可变的-这意味着一旦创建,它们就不能以任何方式更改.
每次在字符串中添加或删除字符时,都会创建一个新副本,并且以前的版本保持不变.
例子:
All .NET Strings are immutable - which means that once they are created, they cannot be changed in any way.
Every time you add or remove a character from a string, a new copy is created, and the previous version is unchanged.
An example:
string orig = "Hello there!";
string copy = orig;
string replaced = orig.Replace("there", "World");
orig += " This is a test.";
Console.WriteLine(replaced);
Console.WriteLine(orig);
Console.WriteLine(copy);

输出为:

Hello World!
Hello there! This is a test.
Hello there!


阅读,

http://stackoverflow.com/questions/2365272/why-net-string-is-immutable [ ^ ]

还有这个.

http://channel9.msdn.com/Forums/TechOff/58729-Why-are-string-types-immutable-in-C [
Read this,

http://stackoverflow.com/questions/2365272/why-net-string-is-immutable[^]

and this.

http://channel9.msdn.com/Forums/TechOff/58729-Why-are-string-types-immutable-in-C[^]


A nice google search would solve all your doubts.


Cheers


这篇关于C#中字符串的不可变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆