I have the following code in order to compare a value read from a DB with some expected values:
object o = row.get_Value(fieldIndex);
bool equal = o == "mytext";
When I debug that o
has the value "mytext"
. However the comparison yields false
. If on the other hand I cast o
to string
, the comparison works:
bool equal = (string)o == "mytext";
While row
is a COM-object, o.GetType()
returns string
.
Unfortunately I cannot provide what get_Value
does, as I don´t have its sourcecode.
So why does the second comparison work while the first one does not?
The minimal reproducible example would be
object o = "mymy";
string s = "my";
Console.WriteLine(o == s+s);
This gives you the compiler warning CS0252, which basically says that you get the equality comparison of object
and not the equality comparison of string
.
string
changes the meaning of ==
to that of .Equals()
, which tests for character by character equality.