Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

เมธอด Uri.GetHashCode() ใน C#

เมธอด Uri.GetHashCode() ใน C# ใช้สำหรับดึงค่ารหัสแฮชสำหรับ URI

ไวยากรณ์

ไวยากรณ์มีดังนี้:

public override int GetHashCode ();

ตัวอย่าง

ตัวอย่างการใช้งานเมธอด Uri.GetHashCode():

using System; 
public class Demo { 
    public static void Main(){ 
        string URI1 = "https://www.tutorialspoint.com/index.htm"; 
        Console.WriteLine("URI = " + URI1); 
        string URI2 = "https://www.tutorialspoint.com/"; 
        Console.WriteLine("URI = " + URI2); 
        Console.WriteLine("Escaped string (URI1) = " + Uri.EscapeDataString(URI1)); 
        Console.WriteLine("Escaped string (URI2) = " + Uri.EscapeDataString(URI2)); 
        Console.WriteLine("GetHashCode() for URI1 = " + URI1.GetHashCode()); 
        Console.WriteLine("GetHashCode() for URI2 = " + URI2.GetHashCode()); 
    } 
}

ผลลัพธ์

โค้ดจะแสดงผลดังนี้ (ตัวอย่าง):

URI = https://www.tutorialspoint.com/index.htm 
URI = https://www.tutorialspoint.com/ 
Escaped string (URI1) = https%3A%2F%2Fwww.tutorialspoint.com%2Findex.htm 
Escaped string (URI2) = https%3A%2F%2Fwww.tutorialspoint.com%2F 
GetHashCode() for URI1 = -799747664 
GetHashCode() for URI2 = 211597894

ตัวอย่างเพิ่มเติม

ตัวอย่างที่สองกับโดเมนอื่น:

using System; 
public class Demo { 
    public static void Main(){ 
        string URI1 = "https://www.qries.com/index.htm"; 
        Console.WriteLine("URI = " + URI1); 
        string URI2 = "https://www.qries.com/"; 
        Console.WriteLine("URI = " + URI2); 
        Console.WriteLine("Escaped string (URI1) = " + Uri.EscapeDataString(URI1)); 
        Console.WriteLine("Escaped string (URI2) = " + Uri.EscapeDataString(URI2)); 
        Console.WriteLine("GetHashCode() for URI1 = " + URI1.GetHashCode()); 
        Console.WriteLine("GetHashCode() for URI2 = " + URI2.GetHashCode()); 
    } 
}

ผลลัพธ์

โค้ดจะแสดงผลดังนี้ (ตัวอย่าง):

URI = https://www.qries.com/index.htm 
URI = https://www.qries.com/ 
Escaped string (URI1) = https%3A%2F%2Fwww.qries.com%2Findex.htm 
Escaped string (URI2) = https%3A%2F%2Fwww.qries.com%2F 
GetHashCode() for URI1 = -39330946 
GetHashCode() for URI2 = -262746624