English 中文(简体)
为什么有类似于“StingBuilder”的其他不可改变类型?
原标题:Why isn t there an equivalent of StringBuilder for other immutable types?

斯特恩德纯粹因为扼杀而存在。 NET是不可改变的,即传统扼杀物可以使用许多资源(因为正在制造的许多强硬物体)。

因此,因为Int32也是不可更改的。 例如,为什么没有多加的班级?

最佳回答

A concatenated string gets longer, which requires heap memory allocations and memory copies.
These get more expensive the longer the string gets, ergo we ve a helper class (i.e. StringBuilder) to minimise the amount of copying that goes when when strings are concatenated.

斜体分解,因为你倍增,你不需要更多的记忆才能掌握两个多面的斜体的结果,你只需要另一个斜体(或者如果它有<条码>*:的话)。

如果您需要concatenate ints into some form of list . oh等,List<int>!

问题回答

有。 ,Uri

什么是<条码>Int32Builder? 通过使用单一分类,对单一分类进行哪些有意义的操作会更方便和(或)更多业绩?

为了让30Builder的阶层感到,必须持有以下规定:

  1. The class or struct is immutable.
  2. Changing the value by replacing it with one based on the previous (e.g. someString += "abc" or someDate = someDate.AddDays(1)) has to be relatively expensive (true in the former example more than the latter) and/or relatively convoluted to code.
  3. The requirement for such a XXXBuilder class is common enough that it makes sense to provide it rather than just letting those who do need it code their own.

以上各项均不适用于<代码>int。 这些规定适用于<条码>、<条码>和<条码>。 我不认为参考与价值类型特别相关,但第2点适合的情况也几乎肯定是比价值类型更好的设计选择。

事实上,第1点和第2点的结合在NET中相对不常见。 有些人认为不太常见(赞成大量使用不可改变的类型)。 如果我们能够避免第2点,那么我们会 t吗? 没有人会认为,“我这样说是昂贵的、cl的,可以提供一个建筑班”。 相反,他们有时会认为,“与我深思熟虑的不可调和性格不入之处在于,虽然这给我带来了许多好处,但却使一些业务费用高昂,而且cl,因此我也提供了建筑类”。

String is a reference type. StringBuilder exists because String is an immutable reference type. String is also a collection of Char - so many allocations happen when you concatenate strings - StringBuilder makes these allocations beforehand, making creation of concatenated strings much more efficient. This is not an issue with value types.

由于Int32是一种价值类型,通常分配在 st上(或放在 object子体内)。 汇编者将自动重新使用记忆地点,例如,在循环中增加许多价值类型。

The answer is basically "because of an implementation detail which means it is unnecessary".

严格分类的速度缓慢,导致对<代码>的要求。

Value types can have their lifetime tracked because they have value type semantics. Whether this occurs is an implementation detail. In practice it does, and that is the reason why there is no need for an IntBuilder class.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签