Q:

C# Equals return true but == return false

Here's part of the data from my DataSet:

Take a look at the 16th row's data and the 17 row's data (partno column), the value is "CTS04", but when I do the checking later on, below scenario happens:

The partno column data type is string, and 17 row's data was created manually using below code:
DataRow newRow = ds.Tables[tableToAdd].Rows.Add();
newRow["partno"] = ds.Tables[tableToAdd].Rows[i - 1]["partno"];

So, logically, I'm just trying to copy the partno from the above row, but why when I use "==" operator to check the value, it returns false?

A:

DataRow[index] returns the content typed as an object. So when you do <object1> == <object2>, the equality comparison of the object type is used, and that will only be true if there is a reference equality between those two objects.
Since your values are actually strings though, you want to use a string based comparison. You can only do that using == if you cast them to strings. Or use .Equals() which is overloaded by the string type to make the comparison:
object a = "foo";
object b = new StringBuilder("foo").ToString(); // this is to make sure that we get a new string object instead of the constant "foo" that is created at compile-time

Console.WriteLine(a == b); // false
Console.WriteLine((string)a == (string)b); // true
Console.WriteLine(a.Equals(b)); // true

For more information see Why would you use String.Equals over ==? and C# difference between == and Equals().

