为什么我可以重载!=运算符? [英] why can I overload the != operator??

查看:61
本文介绍了为什么我可以重载!=运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对我而言,这似乎相当多余。编译器要求如果你使用==运算符重载
,你还必须重载!=运算符。我为@ =运算符做的所有事情都是这样的:


public static bool operator!=(MyType x,MyType y)

{

return!(x == y);

}


那样==运算符处理一切,额外的比较逻辑不需要b $ b。我认为C#编译器不应该允许重载!=

运算符,而只是发出我刚刚显示的代码。


我可以完全关闭我的摇滚歌手,但这已经困扰了我一段时间

现在,我不得不问是否有人知道允许!=

运算符超载的目的。


Chris

解决方案

我猜其中一个原因可能是更有效的实施

!=运算符可能不仅仅是否定了平等的结果

运算符。


-

真诚的,

Dmitriy Lapshin [C#/ .NET MVP]

立即将单元测试的强大功能带到VS .NET IDE中!
http://www.x-unity.net/teststudio.aspx


" Chris" <克拉****** @ hotmail.com>在留言中写道

新闻:%2 **************** @ TK2MSFTNGP11.phx.gbl ...

给我,这似乎相当多余。编译器要求如果你重载==运算符,你还必须重载!=运算符。所有I
为!=运算符都是这样的:

公共静态bool运算符!=(MyType x,MyType y)
{
返回!( x == y);
}

这样,==运算符处理所有内容,并且不需要额外的比较逻辑
。我认为C#编译器不应该允许重载!=
运算符,而只是发出我刚才显示的代码。

我可能完全脱离我的摇杆,但这让我感到困惑现在已经有一段时间了,我只是想问一下是否有人知道允许!=
运算符过载的目的。

Chris



>我猜其中一个原因可能是更有效的实现

!=运算符是可能的,而不仅仅是否定了平等运算符的结果。




我想你是对的。在许多情况下,更容易确定

是否不等于它。我认为它至少应该是可选的b $ b b。如果它没有超载,那么编译器只会发出我之前提到的



Chris

克里斯 -


我多次想知道同样的事情。正如Dmitriy所暗示的那样,有一个可能的实现是!=更有效的实现!(尽管

,想出一个例子会很有趣)。也就是说,你会认为

编译器只是提供一个实现,你没有指定

一个。 (<,> =)和(>,< =)相同的goies。


困扰我的另一件事是,如果你定义:


静态公共bool运算符==(MyType x,MyOtherType o)

{

//你的实现在这里

}


您还必须定义


静态公共bool运算符==(MyOtherType o,MyType x)

{

return(x == o);

}


因为相等应始终是对称的,所以顺序这个论点应该是没有关系的。那么,为什么我必须定义两个运算符?


Ken

" Chris" <克拉****** @ hotmail.com>在留言中写道

新闻:%2 **************** @ TK2MSFTNGP11.phx.gbl ...

给我,这似乎相当多余。编译器要求如果你重载==运算符,你还必须重载!=运算符。所有I
为!=运算符都是这样的:

公共静态bool运算符!=(MyType x,MyType y)
{
返回!( x == y);
}

这样,==运算符处理所有内容,并且不需要额外的比较逻辑
。我认为C#编译器不应该允许重载!=
运算符,而只是发出我刚才显示的代码。

我可能完全脱离我的摇杆,但这让我感到困惑现在已经有一段时间了,我只是想问一下是否有人知道允许!=
运算符过载的目的。

Chris


To me, this seems rather redundant. The compiler requires that if you
overload the == operator, you must also overload the != operator. All I do
for the != operator is something like this:

public static bool operator !=(MyType x, MyType y)
{
return !(x == y);
}

That way the == operator handles everything, and extra comparing logic isn''t
needed. I think the C# compiler should NOT allow overloading of the !=
operator, and instead just emit the code I just showed.

I could be completely off my rocker, but this has puzzled me for some time
now, and I just had to ask if someone knows the purpose of allowing the !=
operator to be overloaded.

Chris

解决方案

I guess one of the reasons might be that a more efficient implementation of
the != operator is possible than just negating the outcome of the equality
operator.

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

"Chris" <ct******@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...

To me, this seems rather redundant. The compiler requires that if you
overload the == operator, you must also overload the != operator. All I do for the != operator is something like this:

public static bool operator !=(MyType x, MyType y)
{
return !(x == y);
}

That way the == operator handles everything, and extra comparing logic isn''t needed. I think the C# compiler should NOT allow overloading of the !=
operator, and instead just emit the code I just showed.

I could be completely off my rocker, but this has puzzled me for some time
now, and I just had to ask if someone knows the purpose of allowing the !=
operator to be overloaded.

Chris




> I guess one of the reasons might be that a more efficient implementation
of

the != operator is possible than just negating the outcome of the equality
operator.



I suppose you''re right. In many situations it is easier to determine if
it''s not equal than if it is. I do think that it should at least be
optional though. And if it isn''t overloaded then the compiler just emits
what I mentioned earlier.

Chris


Chris --

I''ve wondered the same thing many times. As Dmitriy suggests, one
possibility is that there''s a more efficient implementation for != (although
it would be interesting to come up with an example). That said, you''d think
the compiler would simply provide an implementation is you don''t specify
one. The same goies for (<, >=) and (>, <=).

The other thing that bothers me is that, if you define:

static public bool operator ==(MyType x, MyOtherType o)
{
// Your implementation here
}

you also have to define

static public bool operator ==(MyOtherType o, MyType x)
{
return (x == o);
}

Since equality should always be symmetric, the order of the argument should
never matter. So, why must I define both operators?

Ken
"Chris" <ct******@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...

To me, this seems rather redundant. The compiler requires that if you
overload the == operator, you must also overload the != operator. All I do for the != operator is something like this:

public static bool operator !=(MyType x, MyType y)
{
return !(x == y);
}

That way the == operator handles everything, and extra comparing logic isn''t needed. I think the C# compiler should NOT allow overloading of the !=
operator, and instead just emit the code I just showed.

I could be completely off my rocker, but this has puzzled me for some time
now, and I just had to ask if someone knows the purpose of allowing the !=
operator to be overloaded.

Chris



这篇关于为什么我可以重载!=运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆