Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare String and Object in C#

Tags:

string

c#

.net

See this code:

object x = "mehdi emrani"; string y = "mehdi emrani"; Console.WriteLine(y == x); 

that returns true.

But this code:

object x = "mehdi emrani"; string y = "mehdi "; y += "emrani"; Console.WriteLine(y == x); 

returns false.

So when I compare String and Object in first code I get true.
But when I compare them in second code I get false.

Both strings are same but why when I append to the string, my result returns false?

like image 689
Mehdi Emrani Avatar asked Jan 22 '14 08:01

Mehdi Emrani


People also ask

How do you compare objects and strings?

String Comparison With Objects ClassThe method returns true if two Strings are equal by first comparing them using their address i.e “==”. Consequently, if both arguments are null, it returns true and if exactly one argument is null, it returns false.

What are the 3 ways to compare two string objects?

There are three ways to compare String in Java: By Using equals() Method. By Using == Operator. By compareTo() Method.

Can I use == to compare strings in C?

In C, string values (including string literals) are represented as arrays of char followed by a 0 terminator, and you cannot use the == operator to compare array contents; the language simply doesn't define the operation.

What is string compare in C?

strcmp() in C/C++ The function strcmp() is a built-in library function and it is declared in “string. h” header file. This function is used to compare the string arguments. It compares strings lexicographically which means it compares both the strings character by character.


2 Answers

In each case, the second operand of == is x, which is of type object. That means you're using the normal reference equality operator.

Now in your first case, you're using two string constants with the same contents. The C# compiler will use a single object for those two references. In the second case, x and y refer to distinct string objects with the same contents. The two references will be different, so == will return false.

You can fix the comparison by:

  • Use Equals instead - that's overridden by string (as opposed to the == operator which is only overloaded:

    Console.WriteLine(y.Equals(x)); // or x.Equals(y), or Equals(y, x) 

    The use of the static Equals(object, object) method can be useful if either of the arguments can be null; it means you don't need to worry about a NullReferenceException.

  • Make both variables of type string, at which point the == overload within string will be picked at compile-time, and that overload compares the contents of the strings, not just the references

It's worth noting that it's not just a matter of the string literals itself being noticed by the C# compiler - it's about compile-time constant expressions. So for example:

object x = "mehdi emrani"; string y = "mehdi " + "emrani"; Console.WriteLine(y == x); // True 

Here y is initialized using two string literals which aren't the same as the one used to initialize x, but the string concatenation is performed by the compiler, which realizes it's the same string it's already used for x.

like image 132
Jon Skeet Avatar answered Sep 30 '22 07:09

Jon Skeet


When you initialized

object x = "mehdi emrani";  //pointer(x) 

It initialized it in memory and assign reference to x. After this when you initialized

string y = "mehdi emrani"; //pointer(x) 

ref

compiler find that this value is already in memory so it assign same reference to y.

Now == equal operator which actually compares the addresses instead of value find the same address for both variable which results true:

x==y  //actually compares pointer(x)==pointer(x) which is true 

In second case when you initialized x and y that get assigned different addresses.

object x = "mehdi emrani";  //Pointer(x) string y = "mehdi ";        //not found in memory y += "emrani";              //Pointer(y) 

Now comparison find different addresses which results false:

x == y  //is actually Pointer(x) == Pointer(y) which is false 

So to overcome this you need to use .Equals() which instead of reference compares the value and object type.

Console.WriteLine(y.Equals(x));   //compares "mehdi emrani" == "mehdi emrani" results true 
like image 26
Zaheer Ahmed Avatar answered Sep 30 '22 06:09

Zaheer Ahmed