关于c#中不可变字符串的困惑。 [英] Confusion about immutable strings in c#.

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

问题描述

因为c#中的字符串是不可变的,所以我们假设我们有以下代码



  string  str =  string  .Empty;  //  第一个实例 
for int i = 0 ; i< 5; i ++)
{
str = str + i.ToString(); // 接下来的五个实例
}





执行此代码后将创建6个实例。所以下面是一些混淆。

1)最后str将指向第6个实例,对吧?

2)谁将指向之前的5个实例?

3)我们可以按照以下方式访问前5个实例吗?



如果您有任何优惠链接,请提及。

解决方案

1)是

2)没有人

3)除非你保留,否则他们不会被垃圾收集你自己在列表中。


Mehdi Gholam是对的,就目前而言。



但请不要使用在正常的C#中指向 - 指针与参考文献非常不同,它不是一个让人困惑的好地方!



1)是的,之后循环, str 将引用循环内创建的最终字符串:01234

2)程序中没有变量将引用中间字符串值,除了第一个特殊单词在整个应用程序中共享的实例字符串。因此,如果任何其他字符串引用String.Empty(或,它们是相同的东西),将有一个对它的引用。

3)不...等等。实际上有一些方法可以在垃圾收集之前访问堆上变量的值( http://msdn.microsoft.com/en-GB/library/bb383561(v = vs.80).aspx [ ^ ]解释其中之一)甚至在垃圾收集后也可以访问如果你知道Windows内存管理是如何工作的。例如,暴力方法是使不同的程序分配足够的内存,以将整个应用程序数据区域分页到磁盘,然后检查磁盘内容。这是SecureString类存在的原因之一:它的内容始终被加密以防止内存检查生效。


你好,

如果你想拥有6个不同的字符串,你可以使用一个数组,并在每个循环计数中添加新值,或者你可以创建一个带有分隔符的长字符串|每个句子之间。



我希望我理解你的问题,

祝你好运,

z3ngew

As strings in c# are immutable and lets suppose we have below code

string str=string.Empty;               //First instance
for(int i=0;i<5;i++)
{
     str = str+i.ToString();           // Next five instances
}



There will be 6 instances created after this code executes. So below are some confusions.
1) finally str will be pointing to 6th instance, right?
2) who will be pointing to previous 5 instances?
3) Can we access previous 5 instances any how?

Also please mention some good links on this, if you have any.

解决方案

1) yes
2) no one
3) you can''t they will be garbage collected unless you keep the in a list yourself.


Mehdi Gholam is right, as far as it goes.

But please, do not use "pointing to" in normal C# - pointers are very different from references and it''s not a good area to become confused in!

1) Yes, after the loop, str will be a reference to the final string created inside the loop: "01234"
2) No variable in your program will be referencing the "intermediate" string values, except the first one which is a special single instance string that is "shared" across your whole application. So if any other string refers to String.Empty (or "", they are the same thing) there will be a reference to it.
3) No...sort of. There are in fact ways to access the value of the variables on the heap before they are garbage collected (http://msdn.microsoft.com/en-GB/library/bb383561(v=vs.80).aspx[^] explains one of them) and even after garbage collection it is possible to access the values if you know how Windows memory management works. For example, a "Brute Force" method is to cause a different program to allocate enough memory to get your entire application data area paged out to disk and then examine the disk content. This is one of the reasons that the SecureString class exists: it''s content is encrypted at all times to prevent memory examination from being effective.


Hello there,
If you want to have 6 different strings you may use an array, and add new value in each loop count, or you can make a long string with a separator "|" between every sentence.

I hope i understood your question right,
Good Luck,
z3ngew


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

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