1

I have two identical files with different names that are used to populate two different Deedle dataframes. Yet when I try to compare identical values in the two dataframes the program sees them as not equal to eachother.

var Df1 = Frame.ReadCsv("C:/File1.csv");
var Df2 = Frame.ReadCsv("C:/File2.csv");

if (Df1["Header1", 3] == Df2["Header1",3])
{
  Console.WriteLine("The computer sees them as equal");
}
else
{
  Console.WriteLine("The computer sees them as not equal");
}

If I cast both values, then the computer sees them as equal. Is there anyway to determine if the two values are equal without casting them?

var Df1 = Frame.ReadCsv("C:/File1.csv");
var Df2 = Frame.ReadCsv("C:/File2.csv");

if ((string)Df1["Header1", 3] == (string)Df2["Header1",3])
{
  Console.WriteLine("The computer sees them as equal");
}
else
{
  Console.WriteLine("The computer sees them as not equal");
}
Gaspare Bonventre
  • 1,134
  • 11
  • 19

1 Answers1

1

I can only guess, that Df2["Header1",3] returns an object.
If you look at the following link == operator MSDN, you'll see that default implementation of == for reference types returns true only if it's the same object (same address on the heap).
Since Df1["Header1",3] and Df2["Header1",3] are two different objects == operator returns false. When you cast the result to string, values comparison is used (see same link above) and == operator returns true.
According to Deedle ReadCsv documentation, only primitive types are inferred, so, if that column contains dates, the return type would be object causing behavior described above

DanielS
  • 744
  • 6
  • 13
  • That seems logical - and somewhat indicative of the risk of overloading "==" in C# vs, say, java, where you have to use a specific method like .equals() if you want to compare the meaning vs. the raw object references. – Chris Stratton Jul 01 '15 at 02:10
  • @ChrisStratton, Interesting point about the risks of operator overloading. I found a related question on SO that discusses this matter in depth. http://stackoverflow.com/questions/77718/why-doesnt-java-offer-operator-overloading. I really liked the second answer (the one with highest score, not the accepted one). Especially the citation of James Gosling and Bjarne Stroustrup. I agree with Stroustrup. The ability to misuse a feature is not a good enough reason to disable it. – DanielS Jul 01 '15 at 03:58
  • I made use of Df1.Equals(Df2) once @DanielS pointed me in the correct direction – Gaspare Bonventre Jul 03 '15 at 00:16
  • @GaspareBonventre you're welcome. I'm glad I could help. – DanielS Jul 03 '15 at 00:42