为什么不使用 == 运算符(为具体类型定义)?

发布于 2024-11-03 14:29:51 字数 666 浏览 0 评论 0原文

我有一个定义为的列表:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

巴黎盛开的樱花 2024-11-10 14:29:51

由于您的列表是 List,我假设您有类似的内容:

var item1 = Items[0];

或其他;这里 item1 变量 的类型为 IItem。运算符解析在构建过程中通过静态分析进行(而不是在运行时通过多态性/RTTI),因此唯一可用的 == 是任何对象 的默认值,即引用相等。

为了支持您的自定义运算符,必​​须相应地键入变量,例如:

Consumable item1 = ..., item2 = ...;

您的强制转换实现了类似的效果。

另一种选择是确保 ==Equals (和 GetHashCode()) 一致,并使用:

if(Equals(item1, item2)) {...}

这将执行 < code>null 检查,然后使用重写的 Equals 方法。这样就支持多态性,因此类型是什么并不重要。

Since your list is List<IItem>, I'm assuming you have something like:

var item1 = Items[0];

or whatever; here item1 the variable is typed as IItem. Operator resolution happens during build via static analysis (not at runtime via polymorphism/RTTI), so the only == available is the default for any object, i.e. reference equality.

To support your custom operator, the variables must be typed accordingly, for example:

Consumable item1 = ..., item2 = ...;

Your cast achieves a similar thing.

Another option would be to make sure that == and Equals (and GetHashCode()) are in agreement, and use:

if(Equals(item1, item2)) {...}

which will do null checks and then use your overridden Equals method. This then supports polymorphism, so it doesn't matter what the types are.

佞臣 2024-11-10 14:29:51

通用语言运行时只知道您的两个对象实现了 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.

梦巷 2024-11-10 14:29:51

== 不检查类型相等性,但对于类它检查引用相等性。因此,如果变量指向同一个对象,则为 true。例如这样:

var temp = item1;
var temp2 = item1;

if( temp == temp2 )
{
  //this code will execute
}

== 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:

var temp = item1;
var temp2 = item1;

if( temp == temp2 )
{
  //this code will execute
}
有深☉意 2024-11-10 14:29:51

不应该是 IItem IComparable 吗?

Shouldn't be the IItem IComparable?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文