English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Hi all,

In the book I read there is a function definition like this:

public override bool Equals(object obj)

{

if (!(obj is Point2))

{

return false;

}

return this == (Point2) obj;

}



but I do it like this:

public override bool Equals(object obj)

{

if (obj is Point2)

{

return this == (Point2) obj;

}

return false;

}



I know they are all the same, but is there a particular reason to go with #1?





Thanks!

2007-03-23 06:26:33 · 3 answers · asked by Léon 1 in Computers & Internet Programming & Design

3 answers

In general, testing for non-equality is quicker than testing for equality. This is because once it finds one difference it knows it can quit. However, testing for equality requires that a full comparison be made. I don't think that it makes a difference in this specific case, as the NOT is done after the 'is' anyway. But, it is a good coding practice.

2007-03-23 08:02:51 · answer #1 · answered by Chris J 6 · 2 0

Yes, they do the same. however I like better (2) because seems clearer for me and has one operation less (the negation:
if (!(obj is Point2)) is evaded). So this is a performance issue, specially if you will call very often this function. So my choice is 2

2007-03-23 14:02:51 · answer #2 · answered by Anonymous · 0 0

Hopefully the book explains their preference, because to me if the function does the same thing, unless performance is an issue (one function performs more efficiently than the other), or consistency (one function adheres closer to the usage of other functions in the program as a whole), the choice seems arbitrary so long as the desired outcome is achieved.

2007-03-23 13:37:55 · answer #3 · answered by Anonymous · 0 0

fedest.com, questions and answers