I am having difficulties following all of the rules for the GetHashCode method in MSDN for a class whose only property is a Connection String.
I have tried just returning the hash of the string, but that returns a different value for the strings Provider=Microsoft.ACE.OLEDB.12.0; Data Source=path, Provider=Microsoft.ACE.OLEDB.12.0; Data Source=path; and Data Source=path;Provider=Microsoft.ACE.OLEDB.12.0;, all of which are equivalent.
Then, I tried using the OleDbConnectionStringBuilder.GetHashCode method, but that doesn’t even return the same HashCode in this case:
test1.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" +
PathToExcelFile + ";Extended Properties='Excel 12.0 Macro;HDR=YES;IMEX=1';";
test2.ConnectionString = test1.ConnectionString;
Console.WriteLine(test1.GetHashCode());
Console.WriteLine(test2.GetHashCode());
My goal is to use my class as a Key to a Dictionary, so GetHashCode is kind of important. How should I go about implementing it?
Define a canonical connection string and call GetHashCode on that string. In this case, you could define “Provider=Microsoft.ACE.OLEDB.12.0; Data Source=path” as your canonical form, and then you would need to transform all equivalent connection strings to the canonical string and then call GetHashCode.
The question then becomes: How hard is it to turn all equivalent connection strings into the same string?