运算符重载(==和!=)和null [英] Operator overloading (== and !=) and null

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

问题描述

为==和!=

实现运算符重载的最佳实践是什么,它在lhs或rhs上处理null。

Andreas: - )

What is the best practice to implement operator overloading for == and !=
that handles null on either lhs or rhs.
Andreas :-)

推荐答案

你的意思是我应该在lhs或rhs上处理null吗?。答案:无论对象是什么,都要付出
; =="运算符(它在任何一方都处理null)。


或者你的意思是如何编码?。我会有点惊讶,这显然不是很明显,但也许你很想避免使用蛮力方法。

只需使用蛮力方法......


if(l == null)

if(r == null)

返回true;

其他

返回false;

else


etc..etc ..


其他任何事情都可能出错,或者可能会让读者感到困惑。


Wilhelm Heramb写道:
Do you mean "Should I handle null on either lhs, or rhs?". Answer: do
whatever the Object "==" operator does (it handles null on either side).

Or do you mean "How do code it?". I''d be a bit surprised it this isn''t
obvious, but perhaps you are feeling tempted to avoid the brute force method.
Just use the brute force method...

if (l == null)
if (r == null)
return true;
else
return false;
else

etc..etc..

Anything else could go wrong, or could confuse a reader.

"Wilhelm Heramb" wrote:
实现运算符重载的最佳实践是什么==和!=
处理lhs或rhs上的null。

Andreas :-)
What is the best practice to implement operator overloading for == and !=
that handles null on either lhs or rhs.
Andreas :-)



嗨Andreas,


首先,我想确认我对你的问题的理解。从

您的描述中,我了解您需要最佳实践来进行

运算符重载。如果有任何误解,请随时告诉我




要重载处理空值的运算符,你需要检查

运算符方法。您可以查看以下链接,了解

运算符重载的教程。

http://msdn.microsoft.com/library/de...us/csref/html/

vcwlkoperatoroverloadingtutorial.asp

HTH。


Kevin Yu

=======

此帖子已提供按原样没有保证,并且不授予

权利。

Hi Andreas,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to the best practice for
operator overloading. If there is any misunderstanding, please feel free to
let me know.

To overload operators that handles nulls, you need to check for null in the
operator method. You can take a look at the following link for tutorial on
operator overloading.

http://msdn.microsoft.com/library/de...us/csref/html/
vcwlkoperatoroverloadingtutorial.asp

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."


感谢支持人员:-)


如果ie,则以下内容不起作用。 rhs为null;


public static bool operator ==(PreparePhone lhs,PreparePhone rhs)

{

if(lhs。数字== rhs.Number&& lhs.NumberType == rhs.NumberType)

返回true;

else

返回false;

}


public static bool operator!=(PreparePhone lhs,PreparePhone rhs)

{

return(lhs.Number!= rhs.Number || lhs.NumberType!= rhs.NumberType);

}


public override bool Equals(object电话)

{

return((this.Number.Equals(((PreparePhone)电话).Number))

&& (this.NumberType.Equals(((PreparePhone)电话).Numb erType)));

}


" Javaman59" < JA ******* @ discussions.microsoft.com>在消息中写道

新闻:CA ********************************** @ microsof t.com ...
Thanks for the support guys :-)

The following does not work if ie. rhs is null;

public static bool operator ==(PreparePhone lhs, PreparePhone rhs)
{
if(lhs.Number == rhs.Number && lhs.NumberType == rhs.NumberType)
return true;
else
return false;
}

public static bool operator !=(PreparePhone lhs, PreparePhone rhs)
{
return (lhs.Number != rhs.Number || lhs.NumberType != rhs.NumberType);
}

public override bool Equals(object phone)
{
return ((this.Number.Equals(((PreparePhone)phone).Number) )
&& (this.NumberType.Equals(((PreparePhone)phone).Numb erType)));
}

"Javaman59" <Ja*******@discussions.microsoft.com> wrote in message
news:CA**********************************@microsof t.com...
你的意思是我应该在lhs或rhs上处理null吗?。答案:做
无论对象" =="运算符(它在任何一方都处理null)。

或者你的意思是如何编码?。我有点意外,这显然不是很明显,但也许你很想避免使用蛮力的方法。
只需使用蛮力方法......

if(l == null)
if(r == null)
返回true;

返回false;
else

等等..等等。

其他任何事情都可能出错,或者会让读者感到困惑。

Wilhelm Heramb写道:
Do you mean "Should I handle null on either lhs, or rhs?". Answer: do
whatever the Object "==" operator does (it handles null on either side).

Or do you mean "How do code it?". I''d be a bit surprised it this isn''t
obvious, but perhaps you are feeling tempted to avoid the brute force
method.
Just use the brute force method...

if (l == null)
if (r == null)
return true;
else
return false;
else

etc..etc..

Anything else could go wrong, or could confuse a reader.

"Wilhelm Heramb" wrote:
实现运算符重载的最佳实践是什么==和!=
处理lhs或rhs上的null。

Andreas :-)
What is the best practice to implement operator overloading for == and !=
that handles null on either lhs or rhs.
Andreas :-)



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

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