关于C#中字符串的困惑 [英] Confusion about strings in c#
本文介绍了关于C#中字符串的困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
关于如何在C#中处理字符串,我感到非常困惑.
- 当我们声明变量
Hi,
I have a big confusion regarding how strings are treated in c#.
- when we declare a variable
string str1
时,它被视为引用类型还是值类型?
- 如果问题一的旁听者是引用类型,那么我们如何能够比较两个这样的字符串
Is it treated as Reference Type or Value Type?
- If awnser to question one is Reference Type then how are we able to compare two string like this
if(str1==str2)
在这里,字符串的工作方式类似于值类型.怎么了? - 在其他情况下,类似于字符串数组,其行为与值类型完全相同,而不是引用类型.那么为什么我们称字符串为引用类型呢?
Here the string is working like value types. How come??? - when we declare a variable
- Similarly in other situtions like an array of strings behaves exactly like value type rather than reference type. Then why do we call strings are reference type???
- —SA
- —SA
推荐答案
快速答案是:类型System.String
是该类,即紧密模拟值类型语义的引用类型.
按值比较引用类型(第二个问题)不是奇迹.
您可以在自己的课堂上轻松实现这种事情.这是通过实现自定义运算符"=="和!="来完成的.对于引用相等,您应该使用静态(当然,非虚拟的,不要被覆盖)方法System.Object.ReferenceEquals
.覆盖相等时,需要使用此方法.算法是这样的:1)检查一个或两个操作数是否为null,并返回false和true为等于(使用上述方法); 2)如果不是非null,请执行语义测试(如果是字符串,则按内容).
但是,在System.String
的情况下,它变得更加复杂和有趣!相同内容的引用不同的字符串可以引用完全相同的物理内存,并且将值分配给字符串通常会更改其引用标识.这样做是为了以对用户透明的方式重用不同字符串使用的内存.这称为"字符串实习".
请参阅:
http://en.wikipedia.org/wiki/String_interning [ http://msdn.microsoft.com/en-us/library/system.string. intern.aspx [^ ].
关于您的第三个问题:字符串仍然是引用类型.我在上面解释过了.
有趣,不是吗?
The quick answer is: the typeSystem.String
is the class, that is, a reference type closely simulating value type semantics.
Comparison of reference type by value (your second question) is not a miracle.
You can easily implement such thing in your own class. This is done by implementing of the custom operators "==" and "!=". For referential equality, you should use the static (and of course, non-virtual, not to be overridden) methodSystem.Object.ReferenceEquals
. You need to use this method when overriding equality. The algorithm is this: 1) check up if one or both operands for null and return false and true for equal (use the method mentioned above); 2) if not are non-null, perform semantic test (in case of string, by content).
But, in case ofSystem.String
, it''s much more complex and interesting! The referentially different strings of the same content can reference the exact same physical memory, and assignment of the value to the string generally changes its referential identity; this is done to reuse memory used by different string in a way transparent to the user. This is called "string interning".
Please see:
http://en.wikipedia.org/wiki/String_interning[^];
see this method in MSDN and code samples explaining string behavior:
http://msdn.microsoft.com/en-us/library/system.string.intern.aspx[^].
As to your third question: still, string is a reference type. I explained it above.
Interesting, isn''t it?
尽管字符串是引用类型,但定义了相等运算符(==和!=)来比较字符串对象的值.
因此,==适用于字符串. 可能 [
Although string is a reference type, the equality operators (== and !=) are defined to compare the values of string objects.
Thus the == works on strings. This may[^] help you as well.
An array of strings is not a value type - so I''m not sure what scenario you could be talking about.
字符串是引用类型,但确实很特殊.当像示例中那样对两个字符串值进行操作时,
C = A,在此操作中没有任何东西可与普通班级行为类似.因此,C没有对A的引用.在该操作中,创建了一个新的字符串类,并将字符串A中的值复制到字符串C.字符串C和字符串A指向完全不同的内存地址.这样,当您有许多字符串操作(例如循环)并且在每次迭代中都具有字符串操作时(由于生成了新的字符串类),该操作就是性能泄漏.在这种情况下,建议使用StringBuilder.为什么,因为StringBuilder不会为任何字符串操作创建新字符串
有关更多信息,请访问: http://programmingincsharp.com/is-ac -string-a-value-type-or-a-reference-type/ [
string is reference type but really special one. When you do operation of both string values like in your example
C = A, there is nothing in this operation simular to normal class behavior. So C doesn''t have a reference of A. In that operation, a new string class is created and the value from string A is copied to string C. String C and String A are pointing in completly different memory address. That is way, when you have many string operation, for example loop and in every iteration you have a string manipulation, because of new string class generation, that operation is performance leak. In that situation it is preffered to use StringBuilder. Why, because StringBuilder doesn''t create new string for any string operation
more information in : http://programmingincsharp.com/is-a-c-string-a-value-type-or-a-reference-type/[^]
这篇关于关于C#中字符串的困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文