为什么不使用 == 运算符(为具体类型定义)?
我有一个定义为的列表:
var Items = new List<IItem>();
现在有许多不同的类具有该接口,其中之一是 Consumable。 Consumable 类还重载了 == 运算符。现在我有以下代码并且不起作用:
if(item1 == item2)
{
//code...
}
这不起作用。我在 == 运算符重载中放置了一个断点,但它永远不会到达它。当我进行逐行调试时,item1和item2都是Consumable类型,GetType都返回Consumable。我什至尝试了这段代码:
var temp = item1.GetType();
var temp2 = item2.GetType();
if (temp == temp2)
{
//code...
}
这个相等结果是正确的。现在,如果我尝试这样做:
if(((Consumable)item1) == ((Consumable)item2))
{
//code...
}
这会触发 == 运算符重载中的断点。如果当逐行调试显示它已经认为它们都是可用的时,为什么我必须手动转换变量?是因为我从 IItems 列表中提取它们吗?
I have a list defined as:
var Items = new List<IItem>();
Now there are a number of different classes that have that interface and one of them is Consumable. The Consumable class also has the == operator overloaded. Now I have the following code and is not working:
if(item1 == item2)
{
//code...
}
This does not work. I put a break point in the == operator overload and it never gets to it. When I go through line-by-line debugging, both item1 and item2 are of type Consumable, both GetType returns Consumable. I even try this code:
var temp = item1.GetType();
var temp2 = item2.GetType();
if (temp == temp2)
{
//code...
}
and this equality results is true. Now if I try this:
if(((Consumable)item1) == ((Consumable)item2))
{
//code...
}
and this triggers the break point in the == operator overload. Why would I have to manually cast the variable if when line-by-line debugging show it already thinks they are both consumable? Is it because I am pulling them from a list of IItems?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于您的列表是
List
,我假设您有类似的内容:或其他;这里
item1
变量 的类型为IItem
。运算符解析在构建过程中通过静态分析进行(而不是在运行时通过多态性/RTTI),因此唯一可用的==
是任何对象
的默认值,即引用相等。为了支持您的自定义运算符,必须相应地键入变量,例如:
您的强制转换实现了类似的效果。
另一种选择是确保
==
和Equals
(和GetHashCode()
) 一致,并使用:这将执行 < code>null 检查,然后使用重写的
Equals
方法。这样就支持多态性,因此类型是什么并不重要。Since your list is
List<IItem>
, I'm assuming you have something like:or whatever; here
item1
the variable is typed asIItem
. Operator resolution happens during build via static analysis (not at runtime via polymorphism/RTTI), so the only==
available is the default for anyobject
, i.e. reference equality.To support your custom operator, the variables must be typed accordingly, for example:
Your cast achieves a similar thing.
Another option would be to make sure that
==
andEquals
(andGetHashCode()
) are in agreement, and use:which will do
null
checks and then use your overriddenEquals
method. This then supports polymorphism, so it doesn't matter what the types are.通用语言运行时只知道您的两个对象实现了 IItem 接口。对象层次结构中最小的公共部分是System.Object。并且您没有重载 System.Object 的 == 运算符。
要使用正确的重载,您必须声明对象的类型。
The common langauge runtime does only know that your two objects implement the interface IItem. The smallest common part in object hierarchy is System.Object. And you did not overload the == operator of System.Object.
To use the correct overload you have to state the type of the object.
== 不检查类型相等性,但对于类它检查引用相等性。因此,如果变量指向同一个对象,则为 true。例如这样:
== does not check type equality, but for classes it checks reference equality. So if the variables are pointing to the same object it will be true. For example like this:
不应该是 IItem IComparable 吗?
Shouldn't be the IItem IComparable?