I've found a similar question
How to compare two distinctly different objects with similar properties
that may implicitly and/or in part reply to my question.
Suppose I want compare (without a lot of nested conditions) this object:
class ObjectA {
public string PropertyX { get; set; }
public char PropertyY { get; set; }
public long PropertyZ { get; set; }
}
to a System.String
. I'm interested only in equality or inequality (not a range of values about identity).
Implementing IEquatable<string>
in ObjectA
is a proper choice? I don't care of what simply works, I want to identify the proper pattern for such case.
As other information, please consider that ObjectA
will often be supplied as sequence of IEnumerable<ObjectA>
.
I don't need to know if "string"
==
or !=
objectA
instance; sorting is not involved.
Edit to clarify (and help)
Sorry but writing a good question is sometime difficult...
Suppose I can't represent ObjectA
as string for the purpose of comparison (violating encapsulation is not an option).
In context-1 I've to match it against
PropertyY
.In context-2 I've to match it against an algorithm applied to
PropertyY
/PropertyZ
.
@Oliver solution in the end of the question helps me again (and +1 again). I can simply define a custom interface:
interface IContextConverter {
string ToEquatableStringForContext1();
string ToEquatableStringForContext2();
}
Since I've also an ObjectB
with same logic but different properties, both will implement IContextConverter
(or maybe I find a better name) avoiding to violate RAP.
System.String
is"Hello mum!"
, and theObjectA
has x, y, z equal to"Cool"
,'Z'
, and42L
, how do I figure out if they are equal or not? – Nicol