I have an interface IStat which inherits IEquatable and IComparable
various stats will primarily be referred to as IStat as opposed to their actual type, and the implementation of IEquatable and IComparable will be identical regardless of the actual type.
IStat.cs
public interface IStat : IEquatable<IStat>, IComparable<IStat>{
string GetKey();
string GetValue();
public new bool Equals(IStat stat) { // I'm aware I shouldn't use new
return stat is not null && GetKey() == stat.GetKey();
}
public new int CompareTo(IStat stat) {
return stat is null?1:GetKey.CompareTo(stat.GetKey());
}
}
public interface IStat<TKey, TValue> : IStat {
TKey Key{get;}
TValue Value{get;}
}
ExampleStat.cs
// still requires implementation of Equals and CompareTo, and ExampleStat must be a struct
public struct ExampleStat : IStat<string, double> {
private string m_Key;
private double m_Value;
public string Key=>m_Key;
public double Value=>m_Value;
public ExampleStat(string key, double value) {
m_Key = key;
m_Value = value;
}
}
is there a way to properly implement the IEquatable and IComparable methods inside of IStat? And if there isn't is there a way around this without having to write identical methods in every implementation?
EDIT
I added the implementations for Equals, CompareTo and the Constructor. I'm aware that using classes is the best way to handle my problem, but due to software constraints, for this I need to use a struct, which is where the issue of default behavior in my inheriting classes comes in.