Click here to Skip to main content
15,924,318 members
Home / Discussions / .NET (Core and Framework)
   

.NET (Core and Framework)

 
AnswerRe: Collection.IndexOf and overloaded == Pin
freakyit28-Sep-09 3:42
freakyit28-Sep-09 3:42 
GeneralRe: Collection.IndexOf and overloaded == Pin
Adriaan Davel28-Sep-09 3:49
Adriaan Davel28-Sep-09 3:49 
AnswerRe: Collection.IndexOf and overloaded == Pin
Adriaan Davel28-Sep-09 3:45
Adriaan Davel28-Sep-09 3:45 
GeneralRe: Collection.IndexOf and overloaded == Pin
Gideon Engelberth28-Sep-09 10:53
Gideon Engelberth28-Sep-09 10:53 
GeneralRe: Collection.IndexOf and overloaded == Pin
Adriaan Davel28-Sep-09 20:08
Adriaan Davel28-Sep-09 20:08 
GeneralRe: Collection.IndexOf and overloaded == Pin
supercat929-Sep-09 5:43
supercat929-Sep-09 5:43 
GeneralRe: Collection.IndexOf and overloaded == Pin
Gideon Engelberth29-Sep-09 7:07
Gideon Engelberth29-Sep-09 7:07 
GeneralRe: Collection.IndexOf and overloaded == Pin
Gideon Engelberth29-Sep-09 7:02
Gideon Engelberth29-Sep-09 7:02 
There are so many ways to check equality because there are so many situations that need to be handled. Jared Parsons wrote a nice set of articles that explain some of this topic. The link is to the last article in the set, each of which links to the article before (in typical blog fashion).

One reason to have more than just the operator is because the operator must be a static method. This means that which operator to call depends on the compile-time type of the variable instead of the using the runtime-type like a virtual method (such as Equals) would. Also, because they are static, operators cannot be defined on interfaces. So if you need equality methods on an interface, you have to have instance methods. An advantage of the IEqualityComparer interface is that you can have a different condition for "equality" in different situations for the same type. In one case, you may only check the name, but in another you may want to check both the name and the age. This would be impossible with just the operator or overriden virtual method, but is possible to do with the extra interface. (A similar reasoning can be given for having both IComparer and IComparable for sorting.)

C# has a particularly nasty problem when it comes to == vs .Equals(). In C#, if you write if (a == b) { ... } when there is no overloaded operator for the types of variables a and b, the compiler will check to see if the are both references to the same object. In VB however, writing If a = b Then ... when there is no overloaded operator, a compile error will occur. To see if a and b are references to the same object, you use the Is and IsNot operators.

Collection<Derived>.IndexOf has to use IEquatable<Derived> because it was written without any knowledge of what your classes will be. The alternative would involve a bunch of messy reflection code that would cause horrible performance and would probably not be useful in most situations. All the other places where you are using == and Equals, the compiler knows about and will use your methods since they are the best match. Out of curiosity, did you both implement the Equals for IEquatable<Base> and override the Equals from object?

public class Base : IEquatable<Base>
{
    public static bool operator==(...)
    {
        ...
    }
    
    //this method is for IEquatable
    public bool Eqauls(Base other)
    {

    }
    //this method overrides Equals from object
    public override bool Equals(object obj)
    {

    }
}


If you did, I would expect that before you implemented IEquatable<Derived>, the default algorithm used by IndexOf should have called the override and gotten what you wanted.
GeneralRe: Collection.IndexOf and overloaded == Pin
Luc Pattyn29-Sep-09 7:21
sitebuilderLuc Pattyn29-Sep-09 7:21 
GeneralRe: Collection.IndexOf and overloaded == Pin
Adriaan Davel29-Sep-09 19:18
Adriaan Davel29-Sep-09 19:18 
QuestionGet all specific language characters Pin
Sharon Eden27-Sep-09 0:18
Sharon Eden27-Sep-09 0:18 
AnswerRe: Get all specific language characters Pin
N a v a n e e t h27-Sep-09 0:37
N a v a n e e t h27-Sep-09 0:37 
GeneralRe: Get all specific language characters Pin
Sharon Eden27-Sep-09 1:30
Sharon Eden27-Sep-09 1:30 
GeneralRe: Get all specific language characters Pin
N a v a n e e t h28-Sep-09 2:53
N a v a n e e t h28-Sep-09 2:53 
GeneralRe: Get all specific language characters Pin
Sharon Eden30-Sep-09 5:05
Sharon Eden30-Sep-09 5:05 
GeneralRe: Get all specific language characters Pin
lihtnes5-Nov-09 1:30
lihtnes5-Nov-09 1:30 
GeneralRe: Get all specific language characters Pin
Sharon Eden5-Nov-09 1:40
Sharon Eden5-Nov-09 1:40 
GeneralDoes anyone have another idea? Pin
Sharon Eden28-Sep-09 1:50
Sharon Eden28-Sep-09 1:50 
AnswerAny nice way to get 'equivalent' ASCII characters Pin
supercat930-Sep-09 11:13
supercat930-Sep-09 11:13 
Questionany body can help me Pin
rashid200926-Sep-09 22:49
rashid200926-Sep-09 22:49 
AnswerRe: any body can help me Pin
Abhijit Jana27-Sep-09 3:06
professionalAbhijit Jana27-Sep-09 3:06 
QuestionpLEASE DO REPLY ! Pin
Swastik Das26-Sep-09 22:24
Swastik Das26-Sep-09 22:24 
AnswerRe: pLEASE DO REPLY ! Pin
N a v a n e e t h27-Sep-09 0:01
N a v a n e e t h27-Sep-09 0:01 
AnswerRe: pLEASE DO REPLY ! Pin
Pete O'Hanlon27-Sep-09 9:06
mvePete O'Hanlon27-Sep-09 9:06 
GeneralRe: pLEASE DO REPLY ! Pin
Richard MacCutchan27-Sep-09 12:14
mveRichard MacCutchan27-Sep-09 12:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.