Can you explain UseRandomizedStringHashAlgorithm configuration attribute introduced in .NET Framework 4.5?

By default, the StringComparer class and the string.GetHashCode method use a single hashing algorithm that produces a consistent hash code across application domains. This is equivalent to setting the enabled attribute of the element to 0. This is the hashing algorithm used in the .NET Framework 4. The StringComparer class and the String.GetHashCode method can also use a different hashing algorithm that computes hash codes on a per application domain basis. As a result, hash codes for equivalent strings will differ across application domains. This is an opt-in feature; to take advantage of it, you must set the enabled attribute of the element to 1.

<configuration>
<runtime>
<UseRandomizedStringHashAlgorithm enabled="1" />
</runtime>
</configuration>

public class Example
{
public static void Main()
{
// Show hash code in current domain.
DisplayString display = new DisplayString();
display.ShowStringHashCode();
// Create a new app domain and show string hash code.
AppDomain domain = AppDomain.CreateDomain("NewDomain");
var display2 = (DisplayString) domain.CreateInstanceAndUnwrap(typeof(Example).Assembly.FullName,
"DisplayString");
display2.ShowStringHashCode();
}
}
public class DisplayString : MarshalByRefObject
{
private String s = "This is a string.";
public override bool Equals(Object obj)
{
String s2 = obj as String;
if (s2 == null)
return false;
else
return s == s2;
}
public bool Equals(String str)
{
return s == str;
}
public override int GetHashCode()
{
return s.GetHashCode();
}
public override String ToString()
{
return s;
}
public void ShowStringHashCode()
{
Console.WriteLine("String '{0}' in domain '{1}': {2:X8}", s, AppDomain.CurrentDomain.FriendlyName, s.GetHashCode());
}
}

Need Help? Contact Us.

Log in

*
*

Forgot password?

*

New User

*
*
*
*