共用方式為


IdnMapping.GetHashCode 方法

定義

傳回這個 IdnMapping 物件的雜湊程式碼。

public:
 override int GetHashCode();
public override int GetHashCode ();
override this.GetHashCode : unit -> int
Public Overrides Function GetHashCode () As Integer

傳回

四個 32 位元帶正負號的常數的其中一個,這些常數是從 IdnMapping 物件的屬性衍生。 傳回值沒有特別意義,且不適合在雜湊程式碼演算法中使用。

範例

下列範例假設單一字串可以包含多個以空格分隔的電子郵件位址。 它會從每個電子郵件位址中移除本機部分和 @ 字元,並將產生的域名傳遞至 GetAscii(String, Int32)GetAscii(String, Int32, Int32) 方法,以建立 Punycode 功能變數名稱。 然後,方法 GetUnicode(String, Int32, Int32) 會將 Punycode 功能變數名稱轉換成原始功能變數名稱。

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string email = "johann_doe@bücher.com john_doe@hotmail.com иван@мойдомен.рф";
      IdnMapping idn = new IdnMapping();
      int start = 0, end = 0;

      while (end >= 0) {
         start = email.IndexOf("@", end);
         end = email.IndexOf(" ", start);
         string domain = String.Empty;

         try {
            string punyCode = String.Empty;
            if (start >= 0 && end >= 0) {
               domain = email.Substring(start + 1, end - start - 1);
               punyCode = idn.GetAscii(email, start + 1, end - start - 1);
            }
            else {
               domain = email.Substring(start + 1);
               punyCode = idn.GetAscii(email, start + 1);
            }
            string name2 = idn.GetUnicode(punyCode);
            Console.WriteLine("{0} --> {1} --> {2}", domain, punyCode, name2);
         }
         catch (ArgumentException) {
            Console.WriteLine("{0} is not a valid domain name.", domain);
         }
         Console.WriteLine();
      }
   }
}
// The example displays the following output:
//       bücher.com --> xn--bcher-kva.com --> bücher.com
//
//       hotmail.com --> hotmail.com --> hotmail.com
//
//       мойдомен.рф --> xn--d1acklchcc.xn--p1ai --> мойдомен.рф
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim email As String = "johann_doe@bücher.com john_doe@hotmail.com иван@мойдомен.рф"
      Dim idn As New IdnMapping()
      Dim start, [end] As Integer
      
      Do While [end] >= 0
         start = email.IndexOf("@", [end])
         [end] = email.IndexOf(" ", start)
         Dim domain As String = String.Empty
         
         Try
            Dim punyCode As String = String.Empty
            If start >= 0 And [end] >= 0 Then 
               domain = email.Substring(start + 1, [end] - start - 1)
               punyCode = idn.GetAscii(email, start + 1, [end] - start - 1)
            Else
               domain = email.Substring(start + 1)
               punyCode = idn.GetAscii(email, start + 1)
            End If
            Dim name2 As String = idn.GetUnicode(punyCode)
            Console.WriteLine("{0} --> {1} --> {2}", domain, punyCode, name2) 
         Catch e As ArgumentException 
            Console.WriteLine("{0} is not a valid domain name.", domain)
         End Try
         Console.WriteLine()
      Loop   
   End Sub
End Module
' The example displays the following output:
'       bücher.com --> xn--bcher-kva.com --> bücher.com
'       
'       hotmail.com --> hotmail.com --> hotmail.com
'       
'       мойдомен.рф --> xn--d1acklchcc.xn--p1ai --> мойдомен.рф

備註

GetHashCode如果您的應用程式需要實作有意義的哈希程式代碼演算法,請覆寫 方法。

適用於