values is an object array. I need to check whether the 3rd and 4th elements are strings. If this is the case, I need to check whether they're equal.
Here is how I did it:
if ( values[2] is string &&
values[3] is string &&
((values[2] as string) == (values[3] as string)))
{
return false;
}
Is there a simpler or shorter way to do this?
I think the string.Equals(object) method is the simplest to use here.
result = string.Equals(value[2], value[3]);
From MSDN
returns true if obj is a String and its value is the same as this instance; otherwise, false. If obj is null, the method returns false.
Here's how I would do it:
return values[2] is string && values[2].Equals(values[3]);
It's sufficient to know that one of both objects is a string. If it then equals the other object, this guarantees that the other object is also a string.
Also, note the use of Equals() instead of == to guarantee a comparison of the string contents as opposed to the object references.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With