Nayana Adassuriya
Nayana Adassuriya

Reputation: 24766

Compare two objects is string and is equal

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?

Upvotes: 2

Views: 1227

Answers (6)

Yael
Yael

Reputation: 1600

The if statement checks if an object is compatible with a given type. For example, the following code can determine if an object is an instance of the MyObject type, or a type that derives from MyObject:

  if (obj is MyObject)
  {}

If that is the case, use for more clear code (c# compiler will take care of efficiency)

Use:

 Object.Equals(a, b)

it checks the equality of the string content, rather than the reference (since the string is immutable, there really cannot be two string objects which are referring to the same memory, correct if i'm wrong). It calls "under the hood" to String.Equals because Equals is virtual.

It is a very obscure way to compare strings, i admit, bad more clear and in the same price.

Upvotes: 1

Sandip Bantawa
Sandip Bantawa

Reputation: 2880

Adding to previous answers

return value[2] != null && value[2] is string && value[2].Equals(value[3]);

From eric lipperts post: http://blog.coverity.com/2014/01/13/inconsistent-equality/#.Vfp63d-qqko

In scenario three we must first solve an overload resolution problem to determine what Equals means. The receiver is of type int and it has three methods named Equals:

Equals(object, object) // static method from object
Equals(object)         // virtual method from object
Equals(int)            // Implements IEquatable<int>.Equals(int)

The first one we can eliminate because there are not enough arguments. Of the other two, the unique best method is the one that takes an int; it is always better to convert the short argument to int than to object. Therefore we call Equals(int), which then compares the two integers again using value equality, so this is true.

From MSDN

IEquatable.Equals Method (T) Indicates whether the current object is equal to another object of the same type.

Upvotes: 0

Robby Cornelissen
Robby Cornelissen

Reputation: 97152

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.

Upvotes: 2

Razack
Razack

Reputation: 1896

Use .Net String.Compare Method from these examples: 1,2

 return String.Compare(values[2], values[3], true) == 0;

Upvotes: 0

Resley Rodrigues
Resley Rodrigues

Reputation: 2288

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.

Upvotes: 2

Ian
Ian

Reputation: 1504

You only need to check if one of them is a string, because you are checking them both for equality. If they are equal and one is a string, then both are.

return (values[2] is string) && (values[2] == values[3]);

Upvotes: 1

Related Questions