C# operator overloading: Object.Equals(object o) & Object.GetHashCode() - c#

So I am creating a BST and want it to be a generic tree, right now I am developing the Node<T> class. I need some help with some of the operator overloading specifics. Class is below:
public class Node<T> where T : IComparable
{
//Private member data
private T data;
private Node<T> left;
private Node<T> right;
//private readonly IComparer<T> _comparer;
//Node constructor
public Node()
{
data = default(T); //
left = null;
right = null;
}
//Setters/getters for node private data members
public T Data
{
get { return data; }
set { data = value; }
}
public Node<T> Left
{
get { return left; }
set { left = value; }
}
public Node<T> Right
{
get { return right; }
set { right = value; }
}
public static bool operator ==(Node<T> lhs, Node<T> rhs)
{
if((lhs.Data).CompareTo(rhs.Data) == 0)
{
return true;
}
else
{
return false;
}
}
public static bool operator !=(Node<T> lhs, Node<T> rhs)
{
if (lhs.Data.CompareTo(rhs.Data) != 0)
{
return true;
}
else
{
return false;
}
}
}
But Visual Studio (and sources I have seen online) say I need to overload the Object.Equals(object o) method and also the Object.GetHashCode.
From what I know about .Equals just through using C#, it's like value type semantics, or will compare the values of 2 objects, instead of their references, aka checking if they are actually the same object, which I think is what == usually does when comparing objects. So this is what I tried to do:
public override bool Equals(Object obj)
{
if ((lhs.Data).CompareTo(rhs.Data) == 0)
{
return true;
}
else
{
return false;
}
}
It's basically the same as my == operator, but it doesn't work because the lhs/rhs parameters don't exist. I have no idea how do accomplish this for my case since how I believe it will be called is n1.Equals(n2) and will check to see if the data values are the same for the nodes. Examples online are unclear to me. I also have no idea why I have to involve this hash method at all, but I am still trying to do research on that. Mostly curious about the Equals override for now.

Make sure you implement the Equals method according to the MS guide. Here is my snippet. I use all the time:
// override object.Equals
public override bool Equals(object obj)
{
// shortcut
if (object.ReferenceEquals(this, obj))
{
return true;
}
// check for null and make sure we do not break oop / inheritance
if (obj == null || GetType() != obj.GetType())
{
return false;
}
// TODO: write your implementation of Equals() here
// do not call base.equals !
throw new NotImplementedException();
return false;
}
public static bool operator ==(Class lobj, Class robj)
{
// users expect == working the same way as Equals
return object.Equals(lobj, robj);
}
public static bool operator !=(Class lobj, Class robj)
{
return !object.Equals(lobj, robj);
}
// override object.GetHashCode
public override int GetHashCode()
{
// TODO: write your implementation of GetHashCode() here
// return field.GetHashCode() ^ field2.GetHashCode() ^ base.GetHashCode();
// or simply return the unique id if any
throw new NotImplementedException();
}

Related

How to make Service Fabric Reliable collections case-insensitive?

I have a Stateful Service Fabric service and create, update or read data using IReliableDictionary created with the following code:
var dictionary = await StateManager.GetOrAddAsync<IReliableDictionary<string, Entry>>(ReliableDictionaryName);
// Read
using (ITransaction tx = StateManager.CreateTransaction())
{
ConditionalValue<Entry> result = await dictionary.TryGetValueAsync(tx, name);
return result.HasValue ? result.Value : null;
}
// Create or update
using (ITransaction tx = StateManager.CreateTransaction())
{
await dictionary.AddOrUpdateAsync(tx, entry.Name, entry, (key, prev) => entry);
await tx.CommitAsync();
}
It works, but it is case-sensitive.
Is there any way to make Reliable collection store and get data in a case-insensitive way, except for applying .ToLower() to the keys, which is kind of hacky?
This behavior you see is mostly a property of how strings are compared by default in C#. Reliable dictionaries use a key's implementation of IEquatable and IComparable to perform lookups. If the default behavior of string doesn't work for you, you can implement a type that performs string comparisons the way you want. Then, use the new type as the key for your reliable dictionary. You could implement implicit operators to convert between raw strings and the custom type to make usage painless. Here's an example:
using System.Runtime.Serialization;
[DataContract]
public class CaseInsensitiveString : IEquatable<CaseInsensitiveString>,
IComparable<CaseInsensitiveString>
{
#region Constructors
public CaseInsensitiveString(string value)
{
this.Value = value;
}
#endregion
#region Instance Properties
[DataMember]
public string Value
{
get;
set;
}
#endregion
#region Instance Methods
public override bool Equals(object obj)
{
if (ReferenceEquals(null,
obj))
{
return false;
}
if (ReferenceEquals(this,
obj))
{
return true;
}
if (obj.GetType() != this.GetType())
{
return false;
}
return this.Equals((CaseInsensitiveString)obj);
}
public override int GetHashCode()
{
return this.Value != null
? this.Value.GetHashCode()
: 0;
}
public int CompareTo(CaseInsensitiveString other)
{
return string.Compare(this.Value,
other?.Value,
StringComparison.OrdinalIgnoreCase);
}
public bool Equals(CaseInsensitiveString other)
{
if (ReferenceEquals(null,
other))
{
return false;
}
if (ReferenceEquals(this,
other))
{
return true;
}
return string.Equals(this.Value,
other.Value,
StringComparison.OrdinalIgnoreCase);
}
#endregion
#region Class Methods
public static bool operator ==(CaseInsensitiveString left,
CaseInsensitiveString right)
{
return Equals(left,
right);
}
public static implicit operator CaseInsensitiveString(string value)
{
return new CaseInsensitiveString(value);
}
public static implicit operator string(CaseInsensitiveString caseInsensitiveString)
{
return caseInsensitiveString.Value;
}
public static bool operator !=(CaseInsensitiveString left,
CaseInsensitiveString right)
{
return !Equals(left,
right);
}
#endregion
}

HashSet<T>.CreateSetComparer() cannot specify IEqualityComparer<T>, is there an alternative?

In the internal source there is such a constructor public HashSetEqualityComparer(IEqualityComparer<T> comparer) but it's internal so I can't use it.
By default, HashSet<T>.CreateSetComparer() just uses the parameterless constructor which will apply EqualityComparer<T>.Default.
Is there a way to get a HashSetEqualityComparer<T> with a IEqualityComparer<T> of choice, without copying out the code from the source?
I think best solution is using SetEquals. It does the job you need and exactly in the same way that HashSetEqualityComparer does but it will account for any custom comparers defined in the sets its comparing.
So, in your specific scenario where you want to use a HashSet<T> as a key of a dictionary, you need to implement an IEqualityComparer<HashSet<T>> that makes use of SetEquals and "borrows" the reference source of HashSetEqualityComparer.GetHashCode():
public class CustomHashSetEqualityComparer<T>
: IEqualityComparer<HashSet<T>>
{
public bool Equals(HashSet<T> x, HashSet<T> y)
{
if (ReferenceEquals(x, null))
return false;
return x.SetEquals(y);
}
public int GetHashCode(HashSet<T> set)
{
int hashCode = 0;
if (set != null)
{
foreach (T t in set)
{
hashCode = hashCode ^
(set.Comparer.GetHashCode(t) & 0x7FFFFFFF);
}
}
return hashCode;
}
}
But yes, its a small pain that there is not way to directly create a SetEqualityComparer that leverages custom comparers but this unfortunate behavior is due, IMHO, more to a bug of the existing implementation than a lack of the needed overload; there is no reason why CreateSetComparer() can't return an IEqualityComparer that actually uses the comparers of the sets its comparing as the code above demonstrates.
If I had a voice in it, CreateSetComparer() wouldn't be static method at all. It would then be obvious, or at least predictable, that whatever comparer was returned would be created with the current set's comparer.
I agree #InBetween, using SetEquals is the best way. Even if add the constructor still can not achieve what you want.
please see this code:
http://referencesource.microsoft.com/#System.Core/System/Collections/Generic/HashSet.cs,1360
Here is I try to do:
class HashSetEqualityComparerWrapper<T> : IEqualityComparer<HashSet<T>>
{
static private Type HashSetEqualityComparerType = HashSet<T>.CreateSetComparer().GetType();
private IEqualityComparer<HashSet<T>> _comparer;
public HashSetEqualityComparerWrapper()
{
_comparer = HashSet<T>.CreateSetComparer();
}
public HashSetEqualityComparerWrapper(IEqualityComparer<T> comparer)
{
_comparer = HashSet<T>.CreateSetComparer();
if (comparer != null)
{
FieldInfo m_comparer_field = HashSetEqualityComparerType.GetField("m_comparer", BindingFlags.NonPublic | BindingFlags.Instance);
m_comparer_field.SetValue(_comparer, comparer);
}
}
public bool Equals(HashSet<T> x, HashSet<T> y)
{
return _comparer.Equals(x, y);
}
public int GetHashCode(HashSet<T> obj)
{
return _comparer.GetHashCode(obj);
}
}
UPDATE
I took 5 mins to implement another version form HashSetEqualityComparer<T> source code. And rewrite the bool Equals(HashSet<T> x, HashSet<T> y) method. It is not complex. All code just copy and paste from source, I just revise a bit.
class CustomHashSetEqualityComparer<T> : IEqualityComparer<HashSet<T>>
{
private IEqualityComparer<T> m_comparer;
public CustomHashSetEqualityComparer()
{
m_comparer = EqualityComparer<T>.Default;
}
public CustomHashSetEqualityComparer(IEqualityComparer<T> comparer)
{
if (comparer == null)
{
m_comparer = EqualityComparer<T>.Default;
}
else
{
m_comparer = comparer;
}
}
// using m_comparer to keep equals properties in tact; don't want to choose one of the comparers
public bool Equals(HashSet<T> x, HashSet<T> y)
{
// http://referencesource.microsoft.com/#System.Core/System/Collections/Generic/HashSet.cs,1360
// handle null cases first
if (x == null)
{
return (y == null);
}
else if (y == null)
{
// set1 != null
return false;
}
// all comparers are the same; this is faster
if (AreEqualityComparersEqual(x, y))
{
if (x.Count != y.Count)
{
return false;
}
}
// n^2 search because items are hashed according to their respective ECs
foreach (T set2Item in y)
{
bool found = false;
foreach (T set1Item in x)
{
if (m_comparer.Equals(set2Item, set1Item))
{
found = true;
break;
}
}
if (!found)
{
return false;
}
}
return true;
}
public int GetHashCode(HashSet<T> obj)
{
int hashCode = 0;
if (obj != null)
{
foreach (T t in obj)
{
hashCode = hashCode ^ (m_comparer.GetHashCode(t) & 0x7FFFFFFF);
}
} // else returns hashcode of 0 for null hashsets
return hashCode;
}
// Equals method for the comparer itself.
public override bool Equals(Object obj)
{
CustomHashSetEqualityComparer<T> comparer = obj as CustomHashSetEqualityComparer<T>;
if (comparer == null)
{
return false;
}
return (this.m_comparer == comparer.m_comparer);
}
public override int GetHashCode()
{
return m_comparer.GetHashCode();
}
static private bool AreEqualityComparersEqual(HashSet<T> set1, HashSet<T> set2)
{
return set1.Comparer.Equals(set2.Comparer);
}
}
Avoid this class if you use custom comparers. It uses its own equality comparer to perform GetHashCode, but when performing Equals(Set1, Set2) if Set1 and Set2 have the same equality comparer, the the HashSetEqualityComparer will use the comparer of the sets. HashsetEqualityComparer will only use its own comparer for equals if Set1 and Set2 have different comparers
It gets worse. It calls HashSet.HashSetEquals, which has a bug in it (See https://referencesource.microsoft.com/#system.core/System/Collections/Generic/HashSet.cs line 1489, which is missing a if (set1.Count != set2.Count) return false before performing the subset check.
The bug is illustrated by the following program:
class Program
{
private class MyEqualityComparer : EqualityComparer<int>
{
public override bool Equals(int x, int y)
{
return x == y;
}
public override int GetHashCode(int obj)
{
return obj.GetHashCode();
}
}
static void Main(string[] args)
{
var comparer = HashSet<int>.CreateSetComparer();
var set1 = new HashSet<int>(new MyEqualityComparer()) { 1 };
var set2 = new HashSet<int> { 1, 2 };
Console.WriteLine(comparer.Equals(set1, set2));
Console.WriteLine(comparer.Equals(set2, set1)); //True!
Console.ReadKey();
}
}
Regarding other answers to this question (I don't have the rep to comment):
Wilhelm Liao: His answer also contains the bug because it's copied from the reference source
InBetween: The solution is not symmetric. CustomHashSetEqualityComparer.Equals(A, B) does not always equals CustomHashSetEqualityComparer.Equals(B, A). I would be scared of that.
I think a robust implementation should throw an exception if it encounters a set which has a different comparer to its own. It could always use its own comparer and ignore the set comparer, but that would give strange and unintuitive behaviour.
Additional to the original solution, we can simplify GetHashCode with HashCode.Combine function:
public int GetHashCode(HashSet<T> set) {
int hashCode = 0;
foreach (var item in set) {
hashCode ^= HashCode.Combine(item);
}
return hashCode;
}

How to merge methods with the same body but different signatures together?

This is a refactoring question.
How to merge all these Check() methods into one single Generic Check() method since their method bodies are the same?
ppublic class ChangeDetector : IChangeDetector
{
private readonly IEqualityHelper _equalityHelper;
public ChangeDetector(IEqualityHelper equalityHelper)
{
_equalityHelper = equalityHelper;
}
public bool ChangeDetected { get; private set; }
public void Check<T>(IList<T> existingList, IList<T> newList) where T : IdentifiedActiveRecordBase<T>, new ()
{
if (!this._equalityHelper.Equals(existingList, newList))
{
NotifyChange();
}
}
public void CheckEntities<T>(IdentifiedActiveRecordBase<T> existingObj, IdentifiedActiveRecordBase<T> newObj) where T : IdentifiedActiveRecordBase<T>, new()
{
if (!this._equalityHelper.Equals(existingObj, newObj))
{
NotifyChange();
}
}
public void Check(string existing, string newVal)
{
if (!this._equalityHelper.Equals(existing, newVal))
{
NotifyChange();
}
}
public void Check<T>(T existing, T newVal) where T : struct
{
if (!this._equalityHelper.Equals(existing, newVal))
{
NotifyChange();
}
}
public void Check<T>(T? existing, T? newVal) where T : struct
{
if (!this._equalityHelper.Equals(existing, newVal))
{
NotifyChange();
}
}
private void NotifyChange()
{
ChangeDetected = true;
}
}
My EqualityHelper class members have different body though which is fine:
public class EqualityHelper : IEqualityHelper
{
public bool Equals<T>(IList<T> existingList, IList<T> newList) where T : IdentifiedActiveRecordBase<T>, new()
{
if (existingList == null || existingList.Count == 0)
{
if (newList != null && newList.Count > 0)
{
return false;
}
}
else
{
if (newList == null
|| existingList.Count != newList.Count
|| newList.Any(newListItem => existingList.Any(existingListItem => existingListItem.Id == newListItem.Id)))
{
return false;
}
}
return true;
}
public bool Equals<T>(IdentifiedActiveRecordBase<T> existingObj, IdentifiedActiveRecordBase<T> newObj) where T : IdentifiedActiveRecordBase<T>, new()
{
if (existingObj == null)
{
if (newObj != null)
{
return false;
}
}
else
{
if (newObj == null || existingObj.Id != newObj.Id)
{
return false;
}
}
return true;
}
public bool Equals(string existing, string newVal)
{
return string.Equals(existing, newVal);
}
public bool Equals<T>(T existing, T newVal) where T : struct
{
return !existing.Equals(newVal);
}
public bool Equals<T>(T? existing, T? newVal) where T : struct
{
if ((existing.HasValue && !newVal.HasValue)
|| (!existing.HasValue && newVal.HasValue)
|| existing.Equals(newVal))
{
return false;
}
return true;
}
}
The method bodies aren't really the same, since they're all calling different Equals() methods. What you're intending to do would (if I understand the question correctly) finish up with one Handle<T>() method where T could be any type. Thinking about what you're trying to express in the code, it seems fair that if you have one Handle<T>() method, that ought to be able to call one Equals<T>() method. That way, you can implement your handling logic once (and potentially this becomes more complex later but you only need to write it once) and you delegate the tricky business of comparing objects to your equality comparer class.
Just because the method bodies are looking similar doesn't mean the method signatures can be merged. Each of your five Handle methods calls five different Equals-methods, so unless you can merge the five Equals methods, you can't merge the Handle methods. You can't do that of course because the Equals method implementations are different. Remember that which of the Equals-method are to be called is decided compile-time, not runtime.
Edit: What you could do is change the signature of both Handle/Check and Equals to Check(object existing, object equals) and Equals(object existing, object equals). Then in the Equals-method perform a runtime type-check which results in a switch-case to the five Equals-method you already have with the help of type casting. This would make the implementation slower and only arguably more maintainable. I'm not sure I would go down that route.

Implementing == operator in C# on explicit-interface implementation

I have defined a struct Coords that explicitly implements three interfaces, each defined like this:
public partial struct Coords
{
int ICoordsUser.X { get { return VectorUser.X; } }
int ICoordsUser.Y { get { return VectorUser.Y; } }
IntVector2D ICoordsUser.Vector { get { return VectorUser; }
set { VectorUser=value; } }
ICoordsCanon ICoordsUser.Canon { get { return this; } }
ICoordsUser ICoordsUser.Clone() { return NewUserCoords(VectorUser); }
string ICoordsUser.ToString() { return VectorUser.ToString(); }
IEnumerable<NeighbourCoords> ICoordsUser.GetNeighbours(Hexside hexsides) {
return GetNeighbours(hexsides);
}
int ICoordsUser.Range(ICoordsUser coords) { return Range(coords.Canon); }
}
with the names ICoordsCanon, ICoordsUser, and ICoordsCustom. Then I have defined Value Equality on the struct like so:
#region Value Equality
bool IEquatable<Coords>.Equals(Coords rhs) { return this == rhs; }
public override bool Equals(object rhs) {
return (rhs is Coords) && this == (Coords)rhs;
}
public static bool operator == (Coords lhs, Coords rhs) {
return lhs.VectorCanon == rhs.VectorCanon;
}
public static bool operator != (Coords lhs, Coords rhs) { return ! (lhs == rhs); }
public override int GetHashCode() { return VectorUser.GetHashCode(); }
bool IEqualityComparer<Coords>.Equals(Coords lhs, Coords rhs) { return lhs == rhs; }
int IEqualityComparer<Coords>.GetHashCode(Coords coords) {
return coords.GetHashCode();
}
#endregion
However, when I perform an equality comparison with the == operator between values of one of the interface types, as
if (coordsUser1 == userCoords2) { /* whatever */ }
a reference comparison using object.== is always performed. Does anyone know how I can force value equality onto the == operator in such a circumstance, using C#?
Thank you in advance for any thoughts or suggestions.
[Edit]
In the example above, both coordsUser1 and userCoords2 are instances of Coords stored in variables of type ICoordsUser, so it is not clear to me why the defined override of == is not being used.
Since you cannot define static members for an interface, you cannot define operators on an interface. Because operator overloading is resolved at compile-time, the compiler will not see any == operator that matches the signature given an interface type. Since Object defines the == operator for objects, and there is no better match at compile time, it will use that one.
There are three ways to solve this:
Cast the left-hand object to the type that defines the == operator.
return (Coords)coordsUser1 == userCoords2;
Use the normal Equals(object) method. However, this will box your struct.
return coordsUser1.Equals(userCoords2);
Force your interfaces to implement IEquatable<T>, and use the generic Equals(T) (which reduces boxing).
return coordsUser1.Equals(userCoords2);

Handling comparisons with a custom boolean type?

I have a custom object that maps a boolean value from a legacy database to a C# bool (and back again).
My custom bool object looks like this:
public class S2kBool : IUserDefinedType {
public bool Value { get; set; }
public Type SupportedType { get { return typeof(string); } }
// These are the values used to represent booleans in the database
public const string TrueValue = "Y";
public const string FalseValue = "N";
public static S2kBool True {
get { return new S2kBool(true); }
}
public static S2kBool False {
get { return new S2kBool(false); }
}
public S2kBool() : this(false) { }
public S2kBool(bool value) {
this.Value = value;
}
// Called when a property of this type is populated from the database
public void FromSimpleDataType(object value) {
this.Value = value.ToString() == TrueValue;
}
// Called when a property of this type is inserted into the database
public object ToSimpleDataType() {
return this.Value ? TrueValue : FalseValue;
}
}
I would like to be able to do something like this:
public class TestObject {
public S2kBool IsActive = S2kBool.True;
}
TestObject tObj = new TestObject();
if (tObj.IsActive == S2kBool.True) {
// the above would evaluate to true
}
I've seen a few different methods for doing comparisons between objects, but I'm not sure of which one to use.
EDIT: Better yet, would it be possible to do something like the following and have C# treat the S2kBool object as an actual Boolean during comparison? It should also allow comparisons with other S2kBool objects, as well.
if (tObj.IsActive == true) { ... }
There are 2 things to look at; an implicit conversion operator (in S2kBool) to bool, or the true/false operators themselves...
true/false operators (note I prefer the implicit bool conversion myself):
public static bool operator true(S2kBool x) {
return x.Value;
}
public static bool operator false(S2kBool x) {
return !x.Value;
}
then you can use if(tObj.IsActive)
conversion operator:
public static implicit operator bool(S2kBool x) {
return x.Value;
}
works likewise
You might also add a conversion in the other direction:
public static implicit operator S2kBool(bool x)
{
return new S2kBool(x);
}
Then you can assign IsActive = false; etc
Finally, I wonder if this should be an immutable struct? It might be confusing if you expect this to behave like a value. For example, look at the last line here:
TestObject obj1 = new TestObject(),
obj2 = new TestObject();
obj1.IsActive = obj2.IsActive = S2kBool.True;
Console.WriteLine(obj1.IsActive);
Console.WriteLine(obj2.IsActive);
obj1.IsActive.Value = false;
Console.WriteLine(obj1.IsActive);
Console.WriteLine(obj2.IsActive); // what does this print?
This prints false, because both IsActive fields point to the same instance of S2kBool. If that was the intent, then fine. But if it was me, I'd make it immutable (whether class or struct). But since it doesn't really have any state other than a bool, I'd argue that this fits well as a struct.
To be honest, I'm not entirely sure why it is needed at all, when all the functionality could be done via static methods / etc.
Yes, you can do that. You would need to define equality operators and override the Equals method.
Here is an article about operator overloading:
http://www.csharphelp.com/archives/archive135.html
Here is an example of a type with overridden equality operators. You can do the same with assignment and conversion operators, making your type work seamlessly with the built-in bool type. (I took your example, shortened it a bit to keep the example short, and added the equality operators).
public struct S2kBool : IEquatable<bool>
{
public bool Value { get; set; }
public bool Equals(bool other)
{
return Value == other;
}
public override int GetHashCode()
{
return Value.GetHashCode();
}
public static bool operator ==(bool left, S2kBool right)
{
return right.Equals(left);
}
public static bool operator !=(bool left, S2kBool right)
{
return !(left == right);
}
public static bool operator ==(S2kBool left, bool right)
{
return left.Equals(right);
}
public static bool operator !=(S2kBool left, bool right)
{
return !(left == right);
}
}

Categories

Resources