<UseRandomizedStringHashAlgorithm > 元素<UseRandomizedStringHashAlgorithm> Element

判斷 common language runtime 是否會針對每個應用程式網域,計算字串的雜湊碼。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 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 屬性設定為 [0]。This 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 屬性設定為 [1]。This 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