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

เมธอด Boolean.GetHashCode() ใน C# พร้อมตัวอย่าง


เมธอด Boolean.GetHashCode() ใน C# ใช้สำหรับคืนค่า Hash Code ของ Instance นี้

Syntax

public override int GetHashCode ();

ตัวอย่าง

using System; 
public class Demo { 
    public static void Main(String[] args){ 
        string str = "JackSparrow!"; 
        bool val = true; 
        char[] arr = { 'J', 'a' }; 
        Console.WriteLine("String = " + str); 
        Console.WriteLine("String (after trim) = " + str.Trim(arr)); 
        Console.WriteLine("String (Hashcode) = " + str.GetHashCode()); 
        Console.WriteLine("Bool (Hashcode) = " + val.GetHashCode()); 
    } 
}

ผลลัพธ์

String = JackSparrow! 
String (after trim) = ckSparrow! 
String (Hashcode) = -203134198 
Bool (Hashcode) = 1

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

using System; 
public class Demo { 
    public static void Main(String[] args) { 
        bool val1 = true; 
        bool val2 = false; 
        Console.WriteLine("Value1 (Hashcode) = " + val1.GetHashCode()); 
        Console.WriteLine("Value1 (TypeCode) = " + val1.GetTypeCode()); 
        Console.WriteLine("Value2 (Hashcode) = " + val2.GetHashCode()); 
        Console.WriteLine("Value2 (TypeCode) = " + val2.GetTypeCode()); 
    } 
}

ผลลัพธ์

Value1 (Hashcode) = 1 
Value1 (TypeCode) = Boolean 
Value2 (Hashcode) = 0 
Value2 (TypeCode) = Boolean