可变和不可变的字符串类 [英] Mutable and Immutable String Class

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

问题描述

可变和不可变的String类有什么区别?



为什么数据总是以字符串形式流动而不是数字?

What is the difference between mutable and immutable String class?

Why Data always flows as string but not as number?

推荐答案

1).NET中的所有字符串都是不可变的。你永远不能改变任何字符串的内容。所有字符串操作都会创建一个全新的字符串。使用字符串操作时请记住,特别是高度误用的连接(+)。



比较:

1) All strings in .NET are immutable. You never can change a content of any string. All string operations create a brand-new string. Keep it in mind when using string operations, especially highly misused concatenation ("+").

Compare:
string myString = "Immutable";
char charI = myString[0]; //you can do it
//myString[0] = ' '; myString[1] = ' '; no way; it is immutable

//array of char can be considered as some kind of string; it is mutable:
char[] myText = new char['M', 'u', 't', 'a', 'b', 'l', 'e', ' ', ];
myText[7] = '!'; //you can do it! The result will be "Mutable!"





如果要重复连接字符串,请始终使用 System.Text.StringBuilder 。更一般地说,每当您需要可变字符串数据的某些效果时,请使用此类。这个类不是字符串;它是一个包含字符数据的容器,可以通过 object.ToString()转换为字符串。



2)不正确。没有为什么。如果流动你如何定义它。



-SA



In you want to concatenate strings repeatedly, always use System.Text.StringBuilder. More generally, whenever you need some effects of mutable string data, use this class. This class is not a string though; it's a container of character data with operations which you can convert to string via object.ToString().

2) Not true. There is no "why". If flows how you define it.

—SA


我们现在是字符串是参考输入.net。由于这个事实,它们被分配在堆上,当它们在范围内时,它们的指针进入堆栈。当我们尝试为字符串变量分配一个新值时,我们可以获得分配给我们变量的新值,但是在后台,会分配一个新的内存位置,并将其引用复制到输出字符串变量,让我们感觉到值原来的一个改变了。此功能称为不可变。因此.net中的所有字符串都是不可变的。



由于字符串的这种行为,我们应该始终使用System.IO.Text命名空间中的StringBuilder类,我们需要它们很多字符串操作。这个类有很多方法可以用来操作字符串。



我们可以使用以下代码获取字符串

We are now that strings are reference type in .net. Due to this fact, they are allocated on heap and when in the scope their pointer come on to the stack. When we try and assign a new value to a string variable, we can get the new value assigned to our variable, but in the background, a new memory location is allocated and its reference is copied to out string variable making us feel that the value of the original one is changed. This feature is referred as immutable. Thus all string in .net are immutable.

Due to this behavior of string we should always use StringBuilder class present in System.IO.Text namespace where we thing we need lot of string manipulation. This class has lot of methods that we can use to manipulate the strings.

We can get our string using the following code
StringBuilder sb=new StringBuilder();
//do some stuff like string manipulation here
string s=db.ToString();





后端的数据主要以表格形式流动二进制或xml字符串,如果它在进程之间流动但如果它是inproc调用则以给定类型流动。



希望这有助于



谢谢



The data in the backend flows mostly in form of binary or xml strings if it flows between processes but flows as given type if it is inproc call.

Hope this helps

Thanks


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

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