分配给对象和字符串变量时的字符串比较

编程入门 行业动态 更新时间:2024-10-23 07:39:21
本文介绍了分配给对象和字符串变量时的字符串比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是c#的新手。我读了关于==的文章,并且等于值类型和ref types的比较。我写了下面提到的小程序。

int a = 3 ; int b = 3 ; // string a =abc,b =abc; // 对象a = 1;对象b = 1; // object a =abc;对象b =abc; 如果(a == b) {控制台.WriteLine( true); } else { Console.WriteLine( false); } if (a.Equals(b)) { Console.WriteLine( true); } else { Console.WriteLine( false); } if ( object .Equals(a,b )) { Console.WriteLine( true); } else { Console.WriteLine( false); } if ( object .ReferenceEquals(a,b )) { Console.WriteLine( true); } else { Console.WriteLine( false); }

查看结果后我对这四种比较的区别感到困惑。请告诉我区别显然。 结果: 案例1:int a = 3; int b = 3; true true true false 案例2:字符串a =abc;字符串b =abc; true true true true 案例3:对象a = 1;对象b = 1; fasle true true false 案例4:对象a =abc;对象b =abc; true true true true

解决方案

似乎令人困惑,但这是因为你试图混合Value类型和Reference类型并假设结果将是相同。 ==对值类型进行值比较,并对参考类型进行参考比较。 等于对比值进行比较值类型和引用类型的引用比较 object.Equals是Equals的静态,null安全版本 ReferenceEquals总是比较引用。 int 是一个值类型,三个是三个 - 但是值类型的引用比较将始终返回false,因为它们需要被装箱以进行参考比较,并产生不同的参考。 string 是一个引用类型,但它是一个特殊情况 - 字符串是不可变的,因此常量字符串值将始终提供与它们存储在字符串库中相同的引用编译器可以重复使用来节省空间。 为了给对象赋一个值类型整数值,它必须被装箱成一个引用类型。结果,两个引用不一样,所以==和ReferenceEquals都给你错误。 最后一个案例是第二个只是稍微重写。

在所有情况下, a 在所有意义上都等于 b ,除了参考和另外一个案例。我确信所有平等案件都是明确的,不会引起你的困惑,也不需要解释。 唯一可能令人困惑的一点是只能是非平等案件。解释: 在第3种情况下,第一行:你将1与1进行比较。它们不相等,因为你比较了盒装的形式的整数,而不是整数本身。因此,您有两个单独的盒装对象,每个对象的整数字段为1.由于类型 System.Object 没有预定义的相等性(例如,基于某些成员) ,比较返回false。没有被覆盖的平等标准的不同对象不相等。 其他情况与 ReferenceEquals 有关。此方法仅适用于引用类型。对于值类型,它返回false。你可以把它想象成一些合理的后备。但理由是:堆中根本没有地址,因此有两个具有不同地址的独立值对象。 (如果您将 b 更改为2,,则将保持为1)从逻辑角度考虑它们是不同的。

-SA

i am new to c#. i read the article about == and equals comparison for value types and ref types.i wrote small program which is mentioned below.

int a = 3; int b = 3; //string a = "abc", b = "abc"; //object a = 1; object b = 1; // object a = "abc"; object b = "abc"; if (a == b) { Console.WriteLine("true"); } else { Console.WriteLine("false"); } if (a.Equals(b)) { Console.WriteLine("true"); } else { Console.WriteLine("false"); } if (object.Equals(a, b)) { Console.WriteLine("true"); } else { Console.WriteLine("false"); } if (object.ReferenceEquals(a, b)) { Console.WriteLine("true"); } else { Console.WriteLine("false"); }

after viewed the results i am confused difference for these four comparisons.Please tell me the difference clearly. Results: case 1: int a=3;int b=3; true true true false case 2: string a="abc";string b="abc"; true true true true case 3: object a=1; object b=1; fasle true true false case 4: object a="abc";object b="abc"; true true true true

解决方案

Seems confusing, but it's because you are trying to mix Value types and Reference types and assuming the results will be the same. == does a Value comparison for Value types, and a Reference comparison for reference types. Equals does a Value comparison for Value types, and a Reference comparison for reference types object.Equals is a static, null safe version of Equals ReferenceEquals always compares references. int is a value type, and three is three is three - but a reference comparison on a value type will always return false because they need to be "boxed" to do a reference comparison, and that produces different references. string is a reference type, but it's a "special case" - strings are immutable so constant string values will always give you the same reference as they are stored in a "string bank" that the compiler can reuse to save space. In order to assign a value type integer value to an object, it has to be "boxed" into a reference type. As a result, the two references aren't the same, so == and ReferenceEquals both give you false. The last case is the second just slightly rewritten.

In all your cases, a is equal to b in all senses, except referentual and one more case. I am sure that all equality cases are clear, could not cause your confusion and don't need explanation. The only potentially confusing point could only be non-equality cases. Explaining: In case 3, fist line: you compare 1 with 1. They are not equal because you compare the boxed form of integers, not integers themselves. As a result, you have two separate boxed objects, each having integer field of 1. As the type System.Object does not have predefined equality (say, based on some members), the comparison returns false. Different objects without overridden equality criteria are not equal. Other cases are related to ReferenceEquals. This method is only applicable to reference types. For value types, it returns false. You can think of it as of some reasonable fallback. But the rationale is: there are no addresses in the heap at all, so there are two separate value objects with different addresses. (If you change b to 2, a will remains 1) It's logical to consider them referenctually different.
—SA

更多推荐

分配给对象和字符串变量时的字符串比较

本文发布于:2023-10-27 03:23:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1532171.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   变量   对象

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!