Reporter | Yuri Astrakhan (yurik) |
---|---|
Created | Dec 16, 2011 7:52:22 PM |
Updated | Feb 3, 2012 11:26:49 AM |
Resolved | Jan 10, 2012 5:09:38 PM |
Priority | Normal |
Type | Feature |
Fix versions | Next |
State | Fixed |
Assignee | Evgeny Pasynkov (pasynkov) |
Subsystem | No subsystem |
Affected versions | No Affected versions |
Fixed in build | No Fixed in build |
The "==" operator when used between two interfaces is almost always incorrect, as it performs reference equality, not equivalence as frequently assumed by most novice and even advanced developers, and often overlooked during the code reviews. If reference equality is needed, it should be done with ReferenceEquals(), whereas interfaces should always use Equals(i1,i2), or i1.Equals(i2) if i1 is known not to be null and the interface implements IEquatable<T>.
The only exception should be comparing to null - as the intention is always clear.
When comparing two references where one side being an interface, and the other an object, allow it only if the object side defines an operator== between itself and that interface (or derived). Otherwise it will still use a reference equality, and needs a warning.
The only exception should be comparing to null - as the intention is always clear.
When comparing two references where one side being an interface, and the other an object, allow it only if the object side defines an operator== between itself and that interface (or derived). Otherwise it will still use a reference equality, and needs a warning.