How can my strings have different hash codes but the same text value of “16 777 216”?
The test method does not pass:
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace UnitTestStringEquals
{
[TestClass]
public class FormatterTests
{
[TestMethod]
public void Double_Test_1()
{
Assert.AreEqual("16 777 216", FormatDoubleWithThousandSeparator(16777216, 0));
}
public string FormatDoubleWithThousandSeparator(double value, int digits)
{
double result = Math.Round((double)value, digits);
System.Globalization.NumberFormatInfo nfi = (System.Globalization.NumberFormatInfo)System.Globalization.NumberFormatInfo.InvariantInfo.Clone();
nfi.NumberGroupSeparator = " ";
return result.ToString("###,###,###,###,###,###,##0.#####", nfi);
}
}
}
Because the two strings are not the same object, thus they are not equal, there is a string comparision method that should be used instead.
Make sure this isn’t the case one string has a non-printing character and the other one does not. You have to remember that a string is an object. So you need to compare the value of said object to the value of another object.
http://msdn.microsoft.com/en-us/library/system.string.equals.aspx
http://msdn.microsoft.com/en-us/library/1hkt4325.aspx