.net字符串缓存/池 [英] .net string cache/pool

查看:233
本文介绍了.net字符串缓存/池的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请问.NET刺痛级支持缓存/游泳池mechansism减少分配开销?

Does .net sting class support cache/pool mechansism to reduce allocation overhead?

推荐答案

字符串字面拘留,如果是这样的你在说什么:

String literals are interned, if that's what you're talking about:

string x = "hello";
string y = "hel" + "lo";
string z = "h" + "ello";

bool a = object.ReferenceEquals(x, y);
bool b = object.ReferenceEquals(y, z);



两者 A 保证是真实的。

您可以调用的 中的String.intern 自己了。

You can call string.Intern yourself, too.

但是只要正常去字符串(例如,通过返回的字符串对象的String.Format ),答案是否定的,他们是默认情况下不缓存。但是,你既可以创建自己的缓存或实习生他们,如果你绝对必须。由于许多字符串是短暂的,我怀疑,在大多数情况下,这是不值得缓存。

However as far as normal strings go (for example, the string object returned by string.Format), the answer would be no, they're not cached by default. But you could either create your own cache or intern them if you absolutely must. As many strings are short-lived, I suspect that in most cases it's not worth caching.

这篇关于.net字符串缓存/池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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