什么是C#中的可变和不可变的字符串之间的区别? [英] What is the difference between a mutable and immutable string in C#?

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

问题描述

什么是C#中的可变和不可变的字符串之间的区别?

What is the difference between a mutable and immutable string in C#?

推荐答案

可变和不可变的英文单词,意思是可以改变及无法改变。这些词的意思是在IT方面是相同的;即。

Mutable and immutable are English words meaning "can change" and "cannot change" respectively. The meaning of the words is the same in the IT context; i.e.


  • 一个可变的字符串可以改变的,而

  • 不可变的字符串不能改变的。

这句话的含义是在C#中相同/ .NET在其他编程语言/环境,虽然(明显)的类型的名称可能会有所不同,因为可以在其他细节。

The meanings of these words are the same in C# / .NET as in other programming languages / environments, though (obviously) the names of the types may differ, as may other details.

有关记录:


  • 字符串是标准的C#/ .NET一成不变的字符串类型

  • 的StringBuilder 是标准的C#/ .NET可变字符串类型

  • String is the standard C# / .Net immutable string type
  • StringBuilder is the standard C# / .Net mutable string type

要效应变上psented为C#中的字符串重新$ P $ 字符串时,实际上创建一个新的字符串对象。原来字符串不会改变...因为它是不可改变的。

To "effect a change" on a string represented as a C# String, you actually create a new String object. The original String is not changed ... because it is unchangeable.

在大多数情况下,最好使用字符串,因为它是关于他们更容易的原因;例如你并不需要考虑一些其他线程可能会改变我的字符串的可能性。但是,当你需要构建或修改使用的操作顺序的字符串,它可能是更有效的使用的StringBuilder

In most cases it is better to use String because it is easier reason about them; e.g. you don't need to consider the possibility that some other thread might "change my string". However, when you need to construct or modify a string using a sequence of operations, it may be more efficient to use a StringBuilder.

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

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