IdnMapping.GetHashCode 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
返回此 IdnMapping 对象的哈希代码。
public:
override int GetHashCode();
public override int GetHashCode ();
override this.GetHashCode : unit -> int
Public Overrides Function GetHashCode () As Integer
返回
从 IdnMapping 对象的属性派生的四个 32 位常量中的一个。 返回值没有特殊意义,不适合在哈希代码算法中使用。
示例
以下示例假定单个字符串可以包含多个用空格分隔的电子邮件地址。 它从每个电子邮件地址中删除本地部分和 @ 字符,并将生成的域名传递给 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 请重写 方法。