<UseRandomizedStringHashAlgorithm > 元素<UseRandomizedStringHashAlgorithm> Element

确定公共语言运行时是否按应用程序域计算字符串的哈希代码。Determines whether the common language runtime calculates hash codes for strings on a per application domain basis.

<configuration> <configuration>
    <runtime >   <runtime>
     <UseRandomizedStringHashAlgorithm >    <UseRandomizedStringHashAlgorithm>

语法Syntax

<UseRandomizedStringHashAlgorithm   
   enabled=0|1 />  

特性和元素Attributes and Elements

下列各节描述了特性、子元素和父元素。The following sections describe attributes, child elements, and parent elements.

特性Attributes

特性Attribute 描述Description
enabled 必需的特性。Required attribute.

指定是否基于每个应用程序域计算字符串的哈希代码。Specifies whether hash codes for strings are calculated on a per application domain basis.

enabled 特性enabled Attribute

“值”Value 描述Description
0 公共语言运行时不会为每个应用程序域计算字符串的哈希代码;单个算法用于计算字符串哈希代码。The common language runtime does not compute hash codes for strings on a per application domain basis; a single algorithm is used to calculate string hash codes. 这是默认设置。This is the default.
1 公共语言运行时基于每个应用程序域计算字符串的哈希代码。The common language runtime computes hash codes for strings on a per application domain basis. 不同应用程序域和不同进程中的相同字符串具有不同的哈希代码。Identical strings in different application domains and in different processes will have different hash codes.

子元素Child Elements

无。None.

父元素Parent Elements

元素Element 描述Description
configuration 公共语言运行时和 .NET Framework 应用程序所使用的每个配置文件中的根元素。The root element in every configuration file used by the common language runtime and .NET Framework applications.
runtime 包含有关运行时初始化选项的信息。Contains information about runtime initialization options.

备注Remarks

默认情况下,StringComparer 类和 String.GetHashCode 方法使用单个哈希算法,该算法可跨应用程序域生成一致的哈希代码。By default, the StringComparer class and the String.GetHashCode method use a single hashing algorithm that produces a consistent hash code across application domains. 这等效于将 <UseRandomizedStringHashAlgorithm> 元素的 enabled 特性设置为 0This is equivalent to setting the enabled attribute of the <UseRandomizedStringHashAlgorithm> element to 0. 这是 .NET Framework 4 中使用的哈希算法。This is the hashing algorithm used in the .NET Framework 4.

StringComparer 类和 String.GetHashCode 方法还可以使用不同的哈希算法来计算每个应用程序域的哈希代码。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. 这是一项可选功能;若要利用它,必须将 <UseRandomizedStringHashAlgorithm> 元素的 enabled 特性设置为 1This is an opt-in feature; to take advantage of it, you must set the enabled attribute of the <UseRandomizedStringHashAlgorithm> element to 1.

哈希表中的字符串查找通常为 O (1)操作。The string lookup in a hash table is typically an O(1) operation. 但是,当发生大量冲突时,查找可能会成为 O (n2)操作。However, when a large number of collisions occur, the lookup can become an O(n2) operation. 你可以使用 <UseRandomizedStringHashAlgorithm> 配置元素为每个应用程序域生成随机哈希算法,这反过来会限制潜在冲突的数目,特别是当从中计算哈希代码的键基于的数据输入时那些.You can use the <UseRandomizedStringHashAlgorithm> configuration element to generate a random hashing algorithm per application domain, which in turn limits the number of potential collisions, particularly when the keys from which the hash codes are calculated are based on data input by users.

示例Example

下面的示例定义一个 DisplayString 类,该类包含一个私有字符串常量,s,其值为 "This is a string"。The following example defines a DisplayString class that includes a private string constant, s, whose value is "This is a string." 它还包括一个 ShowStringHashCode 方法,该方法显示字符串值及其哈希代码,以及在其中执行方法的应用程序域的名称。It also includes a ShowStringHashCode method that displays the string value and its hash code along with the name of the application domain in which the method is executing.

using System;

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());
   }
}
Module Example
   Public Sub Main()
      ' Show hash code in current domain.
      Dim display As New DisplayString()
      display.ShowStringHashCode()
      
      ' Create a new app domain and show string hash code.
      Dim domain As AppDomain = AppDomain.CreateDomain("NewDomain")
      Dim display2 = CType(domain.CreateInstanceAndUnwrap(GetType(Example).Assembly.FullName, 
                                                          "DisplayString"), DisplayString)   
      display2.ShowStringHashCode()
   End Sub
End Module

Public Class DisplayString : Inherits MarshalByRefObject

   Private s As String = "This is a string."
   
   Public Overrides Function Equals(obj As Object) As Boolean
      Dim s2 As String = TryCast(obj, String)
      If s2 Is Nothing Then
         Return False
      Else
         Return s = s2 
      End If
   End Function

   Public Overloads Function Equals(str As String) As Boolean
      Return s = str
   End Function    
   
   Public Overrides Function GetHashCode() As Integer
      Return s.GetHashCode()
   End Function
   
   Public Overrides Function ToString() As String
      Return s
   End Function

   Public Sub ShowStringHashCode()
      Console.WriteLine("String '{0}' in domain '{1}': {2:X8}",
                        s, AppDomain.CurrentDomain.FriendlyName, 
                        s.GetHashCode())
   End Sub
End Class

运行该示例时,如果不提供配置文件,它将显示如下所示的输出。When you run the example without supplying a configuration file, it displays output similar to the following. 请注意,这两个应用程序域中的字符串的哈希代码是相同的。Note that the hash codes for the string are identical in the two application domains.

String 'This is a string.' in domain 'PerDomain.exe': 941BCEAC  
String 'This is a string.' in domain 'NewDomain': 941BCEAC  

但是,如果将下面的配置文件添加到示例的目录中,然后运行该示例,则相同字符串的哈希代码将与应用程序域不同。However, if you add the following configuration file to the example's directory and then run the example, the hash codes for the same string will differ by application domain.

<?xml version ="1.0"?>  
<configuration>  
   <runtime>  
      <UseRandomizedStringHashAlgorithm enabled="1" />  
   </runtime>  
</configuration>  

如果配置文件存在,则此示例将显示以下输出:When the configuration file is present, the example displays the following output:

String 'This is a string.' in domain 'PerDomain.exe': 5435776D  
String 'This is a string.' in domain 'NewDomain': 75CC8236  

请参阅See also