All pastes #440299 Raw Edit

KeeperOfTheSoul

public text v1 · immutable
·published 2007-04-14 22:58 UTC
rendered paste body
namespace Foo {
	class Program
	{

        static void Main(string[] args)
        {
            B a = new B();
            B b = new B();
            A x = a;
            A y = b;

            a.value = 1;
            b.value = 1;
            a.value2 = 1;
            b.value2 = 2;

            Console.WriteLine("a == b == {0}", a == b);
            Console.WriteLine("x == y == {0}", x == y);

            Console.ReadKey();
            //System.Windows.Forms.Application.Run(new Form1());           
        }
    }

    public class A {
        public int value;

        public override int GetHashCode()
        {
            return value.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            if (obj == null) return false;
            if (!(obj is A)) return false;

            A other = (A)obj;
            return this.value == other.value;
        }

        public static bool operator == (A x, A y) {
            return x.value == y.value;
        }

        public static bool operator !=(A x, A y)
        {
            return x.value != y.value;
        }
    }

    public class B : A {
        public int value2;


        public override int GetHashCode()
        {
            return value.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            if (obj == null) return false;
            if (!(obj is B)) return false;

            B other = (B)obj;
            return this.value == other.value && this.value2 == other.value2;
        }

        public static bool operator ==(B x, B y)
        {
            return x.value == y.value && x.value2 == y.value2;
        }

        public static bool operator !=(B x, B y)
        {
            return x.value != y.value && x.value2 == y.value2;
        }
    }
}