With two immutable classes Base and Derived (which derives from Base) I want to define Equality so that
equality is always polymorphic - that is ((Base)derived1).Equals((Base)derived2) will call Derived.Equals
operators == and != will call Equals rather than ReferenceEquals (value equality)
What I did:
class Base: IEquatable<Base> {
public readonly ImmutableType1 X;
readonly ImmutableType2 Y;
public Base(ImmutableType1 X, ImmutableType2 Y) {
this.X = X;
this.Y = Y;
}
public override bool Equals(object obj) {
if (object.ReferenceEquals(this, obj)) return true;
if (obj is null || obj.GetType()!=this.GetType()) return false;
return obj is Base o
&& X.Equals(o.X) && Y.Equals(o.Y);
}
public override int GetHashCode() => HashCode.Combine(X, Y);
// boilerplate
public bool Equals(Base o) => object.Equals(this, o);
public static bool operator ==(Base o1, Base o2) => object.Equals(o1, o2);
public static bool operator !=(Base o1, Base o2) => !object.Equals(o1, o2); }
Here everything ends up in Equals(object) which is always polymorphic so both targets are achieved.
I then derive like this:
class Derived : Base, IEquatable<Derived> {
public readonly ImmutableType3 Z;
readonly ImmutableType4 K;
public Derived(ImmutableType1 X, ImmutableType2 Y, ImmutableType3 Z, ImmutableType4 K) : base(X, Y) {
this.Z = Z;
this.K = K;
}
public override bool Equals(object obj) {
if (object.ReferenceEquals(this, obj)) return true;
if (obj is null || obj.GetType()!=this.GetType()) return false;
return obj is Derived o
&& base.Equals(obj) /* ! */
&& Z.Equals(o.Z) && K.Equals(o.K);
}
public override int GetHashCode() => HashCode.Combine(base.GetHashCode(), Z, K);
// boilerplate
public bool Equals(Derived o) => object.Equals(this, o);
}
Which is basically the same except for one gotcha - when calling base.Equals I call base.Equals(object) and not base.Equals(Derived) (which will cause an endless recursion).
Also Equals(C) will in this implementation do some boxing/unboxing but that is worth it for me.
My questions are -
First is this correct ? my (testing) seems to suggest it is but with C# being so difficult in equality I'm just not sure anymore .. are there any cases where this is wrong ?
and Second - is this good ? are there better cleaner ways to achieve this ?
Well I guess there are two parts to you problem:
executing equals at nested level
restricting to the same type
Would this work? https://dotnetfiddle.net/eVLiMZ
(I had to use some older syntax as it didn't compile in dotnetfiddle otherwise)
using System;
public class Program
{
public class Base
{
public string Name { get; set; }
public string VarName { get; set; }
public override bool Equals(object o)
{
return object.ReferenceEquals(this, o)
|| o.GetType()==this.GetType() && ThisEquals(o);
}
protected virtual bool ThisEquals(object o)
{
Base b = o as Base;
return b != null
&& (Name == b.Name);
}
public override string ToString()
{
return string.Format("[{0}#{1} Name:{2}]", GetType(), VarName, Name);
}
public override int GetHashCode()
{
return Name.GetHashCode();
}
}
public class Derived : Base
{
public int Age { get; set; }
protected override bool ThisEquals(object o)
{
var d = o as Derived;
return base.ThisEquals(o)
&& d != null
&& (d.Age == Age);
}
public override string ToString()
{
return string.Format("[{0}#{1} Name:{2} Age:{3}]", GetType(), VarName, Name, Age);
}
public override int GetHashCode()
{
return base.GetHashCode() ^ Age.GetHashCode();
}
}
public static void Main()
{
var b1 = new Base { Name = "anna", VarName = "b1" };
var b2 = new Base { Name = "leo", VarName = "b2" };
var b3 = new Base { Name = "anna", VarName = "b3" };
var d1 = new Derived { Name = "anna", Age = 21, VarName = "d1" };
var d2 = new Derived { Name = "anna", Age = 12, VarName = "d2" };
var d3 = new Derived { Name = "anna", Age = 21, VarName = "d3" };
var all = new object [] { b1, b2, b3, d1, d2, d3 };
foreach(var a in all)
{
foreach(var b in all)
{
Console.WriteLine("{0}.Equals({1}) => {2}", a, b, a.Equals(b));
}
}
}
}
This method of comparison using Reflection which, other than the extension methods, is simpler. It also keeps private members private.
All of the logic is in the IImmutableExtensions class. It simply looks at what fields are readonly and uses them for the comparison.
You don't need methods in the base or derived classes for the object comparison. Just call the extension method ImmutableEquals when you are overriding ==, !=, and Equals(). Same with the hashcode.
public class Base : IEquatable<Base>, IImmutable
{
public readonly ImmutableType1 X;
readonly ImmutableType2 Y;
public Base(ImmutableType1 X, ImmutableType2 Y) => (this.X, this.Y) = (X, Y);
// boilerplate
public override bool Equals(object obj) => this.ImmutableEquals(obj);
public bool Equals(Base o) => this.ImmutableEquals(o);
public static bool operator ==(Base o1, Base o2) => o1.ImmutableEquals(o2);
public static bool operator !=(Base o1, Base o2) => !o1.ImmutableEquals(o2);
private int? _hashCache;
public override int GetHashCode() => this.ImmutableHash(ref _hashCache);
}
public class Derived : Base, IEquatable<Derived>, IImmutable
{
public readonly ImmutableType3 Z;
readonly ImmutableType4 K;
public Derived(ImmutableType1 X, ImmutableType2 Y, ImmutableType3 Z, ImmutableType4 K) : base(X, Y) => (this.Z, this.K) = (Z, K);
public bool Equals(Derived other) => this.ImmutableEquals(other);
}
And the IImmutableExtensions class:
public static class IImmutableExtensions
{
public static bool ImmutableEquals(this IImmutable o1, object o2)
{
if (ReferenceEquals(o1, o2)) return true;
if (o2 is null || o1.GetType() != o2.GetType() || o1.GetHashCode() != o2.GetHashCode()) return false;
foreach (var tProp in GetImmutableFields(o1))
{
var test = tProp.GetValue(o1)?.Equals(tProp.GetValue(o2));
if (test is null) continue;
if (!test.Value) return false;
}
return true;
}
public static int ImmutableHash(this IImmutable o, ref int? hashCache)
{
if (hashCache is null)
{
hashCache = 0;
foreach (var tProp in GetImmutableFields(o))
{
hashCache = HashCode.Combine(hashCache.Value, tProp.GetValue(o).GetHashCode());
}
}
return hashCache.Value;
}
private static IEnumerable<FieldInfo> GetImmutableFields(object o)
{
var t = o.GetType();
do
{
var fields = t.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public).Where(field => field.IsInitOnly);
foreach(var field in fields)
{
yield return field;
}
}
while ((t = t.BaseType) != typeof(object));
}
}
Old answer: (I will leave this for reference)
Based on what you were saying about having to cast to object it occurred to me that the methods Equals(object) and Equals(Base) were too ambiguous when calling them from a derived class.
This said to me that the logic should be moved out of both of the classes, to a method that would better describe our intentions.
Equality will remain polymorphic as ImmutableEquals in the base class will call the overridden ValuesEqual. This is where you can decide in each derived class how to compare equality.
This is your code refactored with that goal.
Revised answer:
It occurred to me that all of our logic in IsEqual() and GetHashCode() would work if we simply supplied a tuple that contained the immutable fields that we wanted to compare. This avoids duplicating so much code in every class.
It is up to the developer that creates the derived class to override GetImmutableTuple(). Without using reflection (see other answer), I feel this is the least of all evils.
public class Base : IEquatable<Base>, IImmutable
{
public readonly ImmutableType1 X;
readonly ImmutableType2 Y;
public Base(ImmutableType1 X, ImmutableType2 Y) =>
(this.X, this.Y) = (X, Y);
protected virtual IStructuralEquatable GetImmutableTuple() => (X, Y);
// boilerplate
public override bool Equals(object o) => IsEqual(o as Base);
public bool Equals(Base o) => IsEqual(o);
public static bool operator ==(Base o1, Base o2) => o1.IsEqual(o2);
public static bool operator !=(Base o1, Base o2) => !o1.IsEqual(o2);
public override int GetHashCode() => hashCache is null ? (hashCache = GetImmutableTuple().GetHashCode()).Value : hashCache.Value;
protected bool IsEqual(Base obj) => ReferenceEquals(this, obj) || !(obj is null) && GetType() == obj.GetType() && GetHashCode() == obj.GetHashCode() && GetImmutableTuple() != obj.GetImmutableTuple();
protected int? hashCache;
}
public class Derived : Base, IEquatable<Derived>, IImmutable
{
public readonly ImmutableType3 Z;
readonly ImmutableType4 K;
public Derived(ImmutableType1 X, ImmutableType2 Y, ImmutableType3 Z, ImmutableType4 K) : base(X, Y) =>
(this.Z, this.K) = (Z, K);
protected override IStructuralEquatable GetImmutableTuple() => (base.GetImmutableTuple(), K, Z);
// boilerplate
public bool Equals(Derived o) => IsEqual(o);
}
The code can be simplified using a combination of an extension method and some boilercode. This takes almost all of the pain away and leaves classes focused on comparing their instances without having to deal with all the special edge cases:
namespace System {
public static partial class ExtensionMethods {
public static bool Equals<T>(this T inst, object obj, Func<T, bool> thisEquals) where T : IEquatable<T> =>
object.ReferenceEquals(inst, obj) // same reference -> equal
|| !(obj is null) // this is not null but obj is -> not equal
&& obj.GetType() == inst.GetType() // obj is more derived than this -> not equal
&& obj is T o // obj cannot be cast to this type -> not equal
&& thisEquals(o);
}
}
I can now do:
class Base : IEquatable<Base> {
public SomeType1 X;
SomeType2 Y;
public Base(SomeType1 X, SomeType2 Y) => (this.X, this.Y) = (X, Y);
public bool ThisEquals(Base o) => (X, Y) == (o.X, o.Y);
// boilerplate
public override bool Equals(object obj) => this.Equals(obj, ThisEquals);
public bool Equals(Base o) => object.Equals(this, o);
public static bool operator ==(Base o1, Base o2) => object.Equals(o1, o2);
public static bool operator !=(Base o1, Base o2) => !object.Equals(o1, o2);
}
class Derived : Base, IEquatable<Derived> {
public SomeType3 Z;
SomeType4 K;
public Derived(SomeType1 X, SomeType2 Y, SomeType3 Z, SomeType4 K) : base(X, Y) => (this.Z, this.K) = (Z, K);
public bool ThisEquals(Derived o) => base.ThisEquals(o) && (Z, K) == (o.Z, o.K);
// boilerplate
public override bool Equals(object obj) => this.Equals(obj, ThisEquals);
public bool Equals(Derived o) => object.Equals(this, o);
}
This is good, no casting or null checks and all the real work is clearly separated in ThisEquals.(testing)
For immutable classes it is possible to optimize further by caching the hashcode and using it in Equals to shortcut equality if the hashcodes are different:
namespace System.Immutable {
public interface IImmutableEquatable<T> : IEquatable<T> { };
public static partial class ExtensionMethods {
public static bool ImmutableEquals<T>(this T inst, object obj, Func<T, bool> thisEquals) where T : IImmutableEquatable<T> =>
object.ReferenceEquals(inst, obj) // same reference -> equal
|| !(obj is null) // this is not null but obj is -> not equal
&& obj.GetType() == inst.GetType() // obj is more derived than this -> not equal
&& inst.GetHashCode() == obj.GetHashCode() // optimization, hash codes are different -> not equal
&& obj is T o // obj cannot be cast to this type -> not equal
&& thisEquals(o);
public static int GetHashCode<T>(this T inst, ref int? hashCache, Func<int> thisHashCode) where T : IImmutableEquatable<T> {
if (hashCache is null) hashCache = thisHashCode();
return hashCache.Value;
}
}
}
I can now do:
class Base : IImmutableEquatable<Base> {
public readonly SomeImmutableType1 X;
readonly SomeImmutableType2 Y;
public Base(SomeImmutableType1 X, SomeImmutableType2 Y) => (this.X, this.Y) = (X, Y);
public bool ThisEquals(Base o) => (X, Y) == (o.X, o.Y);
public int ThisHashCode() => (X, Y).GetHashCode();
// boilerplate
public override bool Equals(object obj) => this.ImmutableEquals(obj, ThisEquals);
public bool Equals(Base o) => object.Equals(this, o);
public static bool operator ==(Base o1, Base o2) => object.Equals(o1, o2);
public static bool operator !=(Base o1, Base o2) => !object.Equals(o1, o2);
protected int? hashCache;
public override int GetHashCode() => this.GetHashCode(ref hashCache, ThisHashCode);
}
class Derived : Base, IImmutableEquatable<Derived> {
public readonly SomeImmutableType3 Z;
readonly SomeImmutableType4 K;
public Derived(SomeImmutableType1 X, SomeImmutableType2 Y, SomeImmutableType3 Z, SomeImmutableType4 K) : base(X, Y) => (this.Z, this.K) = (Z, K);
public bool ThisEquals(Derived o) => base.ThisEquals(o) && (Z, K) == (o.Z, o.K);
public new int ThisHashCode() => (base.ThisHashCode(), Z, K).GetHashCode();
// boilerplate
public override bool Equals(object obj) => this.ImmutableEquals(obj, ThisEquals);
public bool Equals(Derived o) => object.Equals(this, o);
public override int GetHashCode() => this.GetHashCode(ref hashCache, ThisHashCode);
}
Which is not too bad - there is more complexity but it is all just boilerplate which I just cut&paste .. the logic is clearly separated in ThisEquals and ThisHashCode
(testing)
Another method would be to use Reflection to automatically compare all of your fields and properties. You just have to decorate them with the Immutable attribute and AutoCompare() will take care of the rest.
This will also use Reflection to build a HashCode based on your fields and properties decorated with Immutable, and then cache it to optimize the object comparison.
public class Base : ComparableImmutable, IEquatable<Base>, IImmutable
{
[Immutable]
public ImmutableType1 X { get; set; }
[Immutable]
readonly ImmutableType2 Y;
public Base(ImmutableType1 X, ImmutableType2 Y) => (this.X, this.Y) = (X, Y);
public bool Equals(Base o) => AutoCompare(o);
}
public class Derived : Base, IEquatable<Derived>, IImmutable
{
[Immutable]
public readonly ImmutableType3 Z;
[Immutable]
readonly ImmutableType4 K;
public Derived(ImmutableType1 X, ImmutableType2 Y, ImmutableType3 Z, ImmutableType4 K)
: base(X, Y)
=> (this.Z, this.K) = (Z, K);
public bool Equals(Derived o) => AutoCompare(o);
}
[AttributeUsage(validOn: AttributeTargets.Field | AttributeTargets.Property)]
public class ImmutableAttribute : Attribute { }
public abstract class ComparableImmutable
{
static BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly;
protected int? hashCache;
public override int GetHashCode()
{
if (hashCache is null)
{
hashCache = 0;
var type = GetType();
do
{
foreach (var field in type.GetFields(flags).Where(field => Attribute.IsDefined(field, typeof(ImmutableAttribute))))
hashCache = HashCode.Combine(hashCache, field.GetValue(this));
foreach (var property in type.GetProperties(flags).Where(property => Attribute.IsDefined(property, typeof(ImmutableAttribute))))
hashCache = HashCode.Combine(hashCache, property.GetValue(this));
type = type.BaseType;
}
while (type != null);
}
return hashCache.Value;
}
protected bool AutoCompare(object obj2)
{
if (ReferenceEquals(this, obj2)) return true;
if (obj2 is null
|| GetType() != obj2.GetType()
|| GetHashCode() != obj2.GetHashCode())
return false;
var type = GetType();
do
{
foreach (var field in type.GetFields(flags).Where(field => Attribute.IsDefined(field, typeof(ImmutableAttribute))))
{
if (field.GetValue(this) != field.GetValue(obj2))
{
return false;
}
}
foreach (var property in type.GetProperties(flags).Where(property => Attribute.IsDefined(property, typeof(ImmutableAttribute))))
{
if (property.GetValue(this) != property.GetValue(obj2))
{
return false;
}
}
type = type.BaseType;
}
while (type != null);
return true;
}
public override bool Equals(object o) => AutoCompare(o);
public static bool operator ==(Comparable o1, Comparable o2) => o1.AutoCompare(o2);
public static bool operator !=(Comparable o1, Comparable o2) => !o1.AutoCompare(o2);
}
I want objects deriving certain class A to also somehow derive the implementation of the Equals(A other) that would do the following: if types of this and other are different, return false, otherwise return this.value == other.value.
My attempt looks like this:
public class A<T> : IEquatable<A<T>>
where T: A<T>
{
protected string Value { get; }
public A(string value)
{
Value = value;
}
public bool Equals(A<T> other)
{
var concrete = other as T;
if (concrete == null)
{
return false;
}
return concrete.Value == Value;
}
}
public class B : A<B>
{
public B(string value)
: base(value)
{
}
}
public class C : A<C>
{
public C(string value)
: base(value)
{
}
}
class Program
{
static void Main(string[] args)
{
var b1 = new B("val");
var b2 = new B("val");
var c = new C("val");
Console.WriteLine(b1.Equals(b1));
Console.WriteLine(b1.Equals(b2));
Console.WriteLine(b2.Equals(b1));
Console.WriteLine(b1.Equals(c));
Console.WriteLine(b2.Equals(c));
Console.WriteLine(c.Equals(b1));
Console.WriteLine(c.Equals(b2));
}
}
This works fine until we derive more:
public class D : C
{
public D(string value)
: base(value)
{
}
}
then it breaks:
var d = new D("val");
Console.WriteLine(d.Equals(c)); // prints "True"
and now I am stuck. How do I make it work?
Both fixing the implementation to work with more than one level of inheritance and preventing more than one level of inheritance are acceptable.
I understand though that I just have to declare all first level descendants of A<T> as sealed, but that's the last resort unless it can be somehow enforced (so non-sealed descendants of A<T> would cause compilation error).
Or maybe my approach is wrong completely?
This is all because the as operator can convert subclasses to superclasses without problems.
What you want to do is to check the types and see whether they are equal:
if (this.GetType() == other.GetType()) {
return false;
}
This question is kind of related, about the behaviours of GetType, typeof and is, which works similar to as.
I am trying to get list of unique elements for a custom datatype. I seriously couldn't figure out why this doesn't work. The control never reaches the Equals implementation in the below code. Could someone please help with this?
public class customobj : IEqualityComparer<customobj>
{
public string str1;
public string str2;
public customobj(string s1, string s2)
{
this.str1 = s1; this.str2 = s2;
}
public bool Equals(customobj obj1, customobj obj2)
{
if ((obj1 == null) || (obj2 == null))
{
return false;
}
return ((obj1.str1.Equals(obj2.str1)) && (obj2.str2.Equals(obj2.str2)));
}
public int GetHashCode(customobj w)
{
if (w != null)
{
return ((w.str1.GetHashCode()) ^ (w.str2.GetHashCode()));
}
return 0;
}
}
And below is the part where i am trying to retrieve distinct elements of list.
List<customobj> templist = new List<customobj> { };
templist.Add(new customobj("10", "50"));
templist.Add(new customobj("10", "50"));
List<customobj> dist = templist.Distinct().ToList();
Your class does not override base Equals() from object class, and Distinct() is using it.
Try overriding base Equals, and calling your custom Equals(Rectangle obj1, Rectangle obj2) from there.
Also, if you want to inherit from typed comparer, use IEquatable<T> , but not IEqualityComparer<Rectangle>
bool Equals(Rectangle obj1, Rectangle obj2)
is a static method of Object, so it can't be overridden.
You must override the instance Equals instead.
public override bool Equals(Object obj) {
...
}
If you want to implement IEqualityComparer in Rectangle's class you should to write something this:
List<Rectangle> dist = templist.Distinct(new Reclangle("","")).ToList();
Usually it is implements through an RectangleComparer class:
class RectangleComparer : IEqualityComparer<Rectangle>
{
public static IEqualityComparer<Rectangle> Instance { get {...} }
...
}
List<Rectangle> dist = templist.Distinct(RectangleComparer.Instance).ToList();
Or override GetHashCode and Equals =)
You are implementing the wrong interface. Your class implements IEqualityComparer<Rectangle>, not IEquatable<Rectangle>. Unless you pass in an IEqualityComparer to Distinct, it will use either IEquatable.Equals (if youe class implements it) or Object.Equals.
public class Rectangle : IEquatable<Rectangle>
{
public string width;
public string height;
public Rectangle(string s1, string s2)
{
this.width = s1; this.height = s2;
}
`IEquatable.Equals
public bool Equals(Rectangle obj2)
{
if (obj2 == null)
{
return false;
}
return ((this.width.Equals(obj2.width)) && (this.height.Equals(obj2.height)));
}
`override of object.Equals
public override bool Equals(Object(o2)
{
if(typeof(o2) == typeof(Rectangle))
return ((Rectangle)this.Equals((Rectangle)o2);
return false;
}
'override of object.GetHashCode
public override int GetHashCode()
{
return ((this.width.GetHashCode()) ^ (thisw.height.GetHashCode()));
}
}
Also, is there a particular reason why your width and height are strings and not numeric types? It seems very odd, and could lead to weird bugs such as assuming that "100" and "0100" and " 100 " are equal, when in fact they are distinct strings and will have different hash codes.
I created a struct
public struct MyCalender : IComparable<MyCalender>
{
public int CompareTo(PersianDate other)
{
return DateTime.Compare(this, other);
}
.
.
.
.
.
}
I new two object of this in a other UserControl, and i want compare they.
I use this code but i get error.
MyCalender value = new MyCalender(2010,11,12);
MyCalender value2 = new MyCalender(2010,11,12);
if (value < value2) ==> geterror
IComparable exposes CompareTo. < and > must be overloaded separately:
class Foo : IComparable<Foo>
{
private static readonly Foo Min = new Foo(Int32.MinValue);
private readonly int value;
public Foo(int value)
{
this.value = value;
}
public int CompareTo(Foo other)
{
return this.value.CompareTo((other ?? Min).value);
}
public static bool operator <(Foo a, Foo b)
{
return (a ?? Min).CompareTo(b) < 0;
}
public static bool operator >(Foo a, Foo b)
{
return (a ?? Min).CompareTo(b) > 0;
}
}
I edited the code so that it does not fail when comparing against null. To keep it brief I used a shortcut that works unless value is Int32.MinValue for a proper Foo. Strictly speaking you'd have to check for null explicitly to get the contract right:
By definition, any object compares greater than (or follows) null, and
two null references compare equal to each other.
Besides, implementing IComparable<T> means that CompareTo(T value) takes a parameter of T. Therefore MyCalendar : IComparable<MyCalender> should implement a method CompareTo(MyCalendar other) rather than PersianDate (or implement IComparable<PersianDate>).
You should either use CompareTo method that you already implemented instead of > in the line you posted or you need to overload > and < operators for your specific class. For instance:
public static bool operator >(MyCalendar c1, MyCalendar c2)
{
return c1.CompareTo(c2) > 0;
}
public static bool operator <(MyCalendar c1, MyCalendar c2)
{
return c1.CompareTo(c2) < 0;
}
But keep in mind that you have to overload both of them.
if comparing just a datetime object,
would something like
DateTime A = DateTime.Now, B = DateTime.Now.AddMinutes(1);
var isqual = A.Date.CompareTo(B.Date);
do the trick?
or something like:
class Calender
{
public DateTime datetime { get; set;}
}
class DateComparer : Calender, IComparable<Calender>
{
public int CompareTo(Calender other)
{
return other.datetime.Date.CompareTo(this.datetime.Date);
}
}
(how) can I Inherit from Boolean?
(Or make my class comparable to Boolean with '=' Operator)
class MyClass : Boolean
{
public MyClass()
{
this = true;
}
}
class Program
{
public Program()
{
MyClass myClass = new MyClass();
if(myClass == true)
//do something...
else
//do something else...
}
}
You can't. System.Boolean is a struct, and you can't derive from structs.
Now, why do you want to do so, exactly? What's the bigger purpose?
You could include an implicit conversion operator from your class to bool, but personally I wouldn't. I would almost always prefer to expose a property, so you'd write:
if (myValue.MyProperty)
... I think that keeps things clear. But if you could give us more of the real context, we may be able to give more concrete advice.
Simple example:
public class MyClass {
private bool isTrue = true;
public static bool operator ==(MyClass a, bool b)
{
if (a == null)
{
return false;
}
return a.isTrue == b;
}
public static bool operator !=(MyClass a, bool b)
{
return !(a == b);
}
}
somewhere in code you can compare your object with boolean value:
MyClass a = new MyClass();
if ( a == true ) { // it compares with a.isTrue property as defined in == operator overloading method
// ...
}
You can use an implicit conversion operator to have this code:
class MyClass {
public bool Value { get; set; }
public MyClass() {
Value = true;
}
public static implicit operator bool(MyClass m) {
return m != null && m.Value;
}
}
class Program {
public static void Main() {
var myClass = new MyClass();
if (myClass) { // MyClass can be treated like a Boolean
Console.WriteLine("myClass is true");
}
else {
Console.WriteLine("myClass is false");
}
}
}
It can be used as above:
if (myClass) ...
Or like this:
if (myClass == true) ...
while your example wouldnt work, you can do something similar for your own classes to test if one equals the values of another.
http://msdn.microsoft.com/en-us/library/ms173147(v=vs.80).aspx
public static bool operator ==(ThreeDPoint a, ThreeDPoint b)
{
// If both are null, or both are same instance, return true.
if (System.Object.ReferenceEquals(a, b))
{
return true;
}
// If one is null, but not both, return false.
if (((object)a == null) || ((object)b == null))
{
return false;
}
// Return true if the fields match:
return a.x == b.x && a.y == b.y && a.z == b.z;
}
public static bool operator !=(ThreeDPoint a, ThreeDPoint b)
{
return !(a == b);
}
You can ("or make my class comparable..."), by overriding the == operator. I presume Jon Skeet overlooked that part of the question.
If you want to be able to use your value in 'if' statements, define operator true and operator false (along with the & and | operators if you want to use && and ||.) (VB equivalents)
To answer more, I would have to know what you're trying to do (in other words, why not just use bool directly?)