比较c#中的字符串。 [英] compare the string in c# .
问题描述
static void Main( string [] args)
{
Object obj = Int32;
字符串 s = Int32跨度>;
字符串 s1 = typeof ( int 跨度>)名称。
Console.WriteLine( {0},obj == s); // true
Console.WriteLine( {0},s == s1); // True
< big> Console.WriteLine( {0},obj == s1); // False *为什么???< / big>
Console.ReadLine();
}
i我无法理解为什么 s1 == obj是假的
什么是规则或他们比较的基础。
这是一个非常奇怪的外向违反平等传递属性的案例。
我想这是由于C#字符串的性质。
对象 obj = Int32;此处创建字符串并内化。对is的引用分配给
obj
。
字符串 s = Int32;这里,分配给
obj
的相同引用被分配给s
(它们'指向'相同的内化字符串)。
Console.WriteLine( {0}跨度>,OBJ == S); // true
此处
obj
和s
通过引用进行比较。结果是正确的,因为它们都保持相同的引用。
Console.WriteLine( {0},s == s1); // True此处
s
和s1
比较按值,因为它们都是string
。
Console.WriteLine( {0},obj == s1); // False *为什么???此处
obj
和s1
与按引用进行比较和结果为false,因为它们没有相同的引用。
除了解决方案2之外,由于
运算符的不同实现而发生这种情况==
。对于其他参考类型,它的行为类似于 ReferenceEquals ,对于String
类, Equality Operator 实现使用等于方法。
使用对
对象$ c的引用比较字符串时$ c>,编译器调用
Object
的相等运算符(并比较引用)。使用对String
的引用比较字符串时,编译器调用String
的相等运算符(并比较值) )。
数据类型对象与字符串不同。
这就像比较车辆和汽车。汽车是一种车辆,但反过来并不一定如此。
编程同样如此。一个String继承自Object,所以String是一个Object,但一个Object基本上可以是任何类型。
试试这个。
< pre lang =c#> Console.WriteLine( {0},obj.ToString() == s1);
我不是100%肯定,但我认为原因是对于前两个变量,一个常数值Int32的字符串在堆上分配。由于此字符串仅分配一次,因此比较
obj == s 为真,因为它是两个不同指针指向同一数据的相同字符串。
这个结构导致在堆栈上分配一个值。
字符串 s1 = typeof ( int )。名称;
比较 s == s1 有效,因为现在比较两种字符串类型。
比较 obj == s1 ,你试着比较两种不同的类型。
这更好地解释了它。 String.Intern Method [ ^ ]
(我的互联网连接速度很慢,所以我可能错过了一些描述这个的评论)
static void Main(string[] args)
{
Object obj = "Int32";
String s = "Int32";
String s1 = typeof (int).Name;
Console.WriteLine("{0}",obj==s); //true
Console.WriteLine("{0}", s == s1); //True
<big> Console.WriteLine("{0}", obj == s1);//False *why ???</big>
Console.ReadLine();
}
i am not able to understand clearly why the "s1 == obj" is false
what is rule or which basis they are comparing.
This is a very curious case of outward violation of the transitive property of equality.
I suppose it is due to the nature of C# strings.
Object obj = "Int32";Here the string is created and 'internalized'. A reference to is is assigned to
obj
.
String s = "Int32";Here, the same reference assigned to
obj
is assigned tos
(they 'points' to the same internalized string).
Console.WriteLine("{0}",obj==s); //trueHere
obj
ands
are compared by reference. The result is true because they both hold the same reference.
Console.WriteLine("{0}", s == s1); //TrueHere
s
ands1
are compare by value because both arestring
.
Console.WriteLine("{0}", obj == s1);//False *why ???Here
obj
ands1
are compared by reference and the result is false because they don't hold the same reference.
In addition to Solution 2, that happens because of a different implementation of the
operator ==
. While for other reference types, it behaves like ReferenceEquals, for theString
class, the Equality Operator is implemented to use the Equals method instead.
When you compare the strings using a reference to an
Object
, the compiler calls the equality operator ofObject
(and compares references). When you compare the strings using a reference to aString
, the compiler calls the equality operator ofString
(and compares values).
The data type Object is not the same as a String.
It is like comparing a vehicle and a car. A car is a vehicle, but the other way around is not necessarily true.
The same goes for programming. A String inherits from Object, so a String is an Object but an Object could be basically any type.
Try this instead.
Console.WriteLine("{0}", obj.ToString() == s1);
I am not 100% sure, but I think the reason is that for the first two variables, a constant string with the value "Int32" is allocated on the heap. As this string is only allocated once, the comparison
obj == s is true because it is the same string with two different pointers pointing to the same data.
This construct results in a value allocated on the stack.
String s1 = typeof (int).Name;
The comparison s == s1 works because now you compare two string types.
When comparing obj == s1, you try to compare two different types.
This explains it better. String.Intern Method[^]
(My internet connection has been slow, so I might have missed some comments that describes this)
这篇关于比较c#中的字符串。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!