I agree with Dru's comment on my post yesterday about overriding Equals(). I now want to implement a static function into my objects that will compare two objects. Again, I'm stuck between two ways of doing it. I could accept specific object types (static bool Equals(MyObject a, MyObject b)) or just accept an object type and cast it (static bool Equals(object a, object b)). I'm leaning towards the latter way as it allows me to define that method into the interface that all my objects already implement. But as I look through the .NET framework, Microsoft used the first way.
Assuming I have an object called Audit, here are the ways I'm trying to implement it.
The first way:
public static bool Equals(Audit a, Audit b)
{
bool rv = true;
try
{
//compare
}
catch (Exception)
{
rv = false;
}
return rv;
}
The second way:
public new static bool Equals(object a, object b)
{
bool rv = true;
try
{
Audit first = (Audit)a;
Audit second = (Audit)b;
//compare
}
catch (Exception)
{
rv = false;
}
return rv;
}
I guess I could implement both ways and let the consumer decide which one to use, but I don't have a way to enforce that with an interface. Any thoughts?
Technorati tags:
Equals,
C#,
.NET