Someone can explain me why this test fails :
[TestMethod] public void WierdComparison() { var machineConf = ConfigurationManager.OpenMachineConfiguration(); var systemWeb = machineConf.GetSectionGroup('system.web') as SystemWebSectionGroup; var prov = systemWeb.Membership.Providers.OfType<ProviderSettings>().Where((s) => s.Name == 'AspNetSqlMembershipProvider').First(); bool result, expected; var connectionStringName = prov.ElementInformation.Properties['connectionStringName'].Value; result = connectionStringName.Equals('LocalSqlServer'); expected = true; Assert.AreEqual(expected, result); result = connectionStringName == 'LocalSqlServer'; expected = true; Assert.AreEqual(expected, result); //This assertion fails }
That is because you are comparing an
Objectto aString.If you compare two strings, the == operator is overloaded to compare the values of the strings. If you compare an object and a string, the == operator that compares two
Objectreferences is used, and that simply compares the references, not the values.The Equals method is a virtual method, so eventhough you call it on an
Objectreference, it will still use the overridden method in theStringclass as the actual type of the object isString.