0Pricing
C# Academy · บทเรียน

ชนิดคล้ายเรคคอร์ดและความเท่าเทียมตามค่า (รูปแบบ)

ใช้งานความเท่าเทียมตามค่าแบบเรคคอร์ด: เขียนทับ Equals/GetHashCode ใช้งาน IEquatable เพิ่มตัวดำเนินการ ==/!= และรักษาชนิดให้เปลี่ยนแปลงไม่ได้

ชนิดคล้ายเรคคอร์ดและความเท่าเทียมตามค่า (รูปแบบ) เป็นบทเรียน C# Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 3 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน C# Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส C# Academy มีบทเรียนทั้งหมด 3 บทเรียน

ภาพรวมชนิดข้อมูลลักษณะเรคคอร์ด

เป้าหมาย: ทำให้ออบเจ็กต์เปรียบเทียบกันด้วยค่าในลักษณะเรคคอร์ดใน C# 6

  • ข้อมูลที่ไม่เปลี่ยนแปลง
  • แทนที่ Equals และ GetHashCode
  • นำ IEquatable<T>ไปใช้งาน
  • ตัวเลือกเสริม: กำหนด ==/!=

ปัญหาความเท่ากันของ reference

โดยค่าเริ่มต้น คลาสจะเปรียบเทียบกันด้วยreference ออบเจ็กต์สองรายการที่มีข้อมูลเหมือนกันจะไม่ถือว่าเท่ากัน

using System;

public class PersonRef
{
  public string First;
  public string Last;
}

public class Program
{
  public static void Main(string[] args)
  {
    PersonRef a = new PersonRef { First = "Ada", Last = "Lovelace" };
    PersonRef b = new PersonRef { First = "Ada", Last = "Lovelace" };

    Console.WriteLine("Reference Equals? " + object.ReferenceEquals(a, b)); // false
    Console.WriteLine("== (default)? " + (a == b)); // false (same as ReferenceEquals for classes)
  }
}

รูปแบบคลาสลักษณะเรคคอร์ด

ทำให้คลาสไม่เปลี่ยนแปลง และนำ IEquatable<T>, Equals, GetHashCode และ ==/!=ไปใช้งาน เพื่อให้ความเท่ากันขึ้นอยู่กับข้อมูล

using System;

// Immutable, value-based equality
public sealed class Person : IEquatable<Person>
{
  public string First { get; private set; }
  public string Last  { get; private set; }

  public Person(string first, string last)
  {
    if (string.IsNullOrEmpty(first)) throw new ArgumentException("first");
    if (string.IsNullOrEmpty(last))  throw new ArgumentException("last");
    First = first; Last = last;
  }

  // "With" helper for copy-on-change
  public Person WithFirst(string first) { return new Person(first, Last); }
  public Person WithLast(string last)   { return new Person(First, last); }

  public bool Equals(Person other)
  {
    if (object.ReferenceEquals(other, null)) return false;
    if (object.ReferenceEquals(this, other)) return true;
    return string.Equals(First, other.First) && string.Equals(Last, other.Last);
  }

  public override bool Equals(object obj)
  {
    return Equals(obj as Person);
  }

  public override int GetHashCode()
  {
    unchecked
    {
      int h = 17;
      h = h * 31 + (First == null ? 0 : First.GetHashCode());
      h = h * 31 + (Last  == null ? 0 : Last.GetHashCode());
      return h;
    }
  }

  public static bool operator ==(Person a, Person b)
  {
    if (object.ReferenceEquals(a, b)) return true;
    if ((object)a == null || (object)b == null) return false;
    return a.Equals(b);
  }

  public static bool operator !=(Person a, Person b) { return !(a == b); }
}

public class Program
{
  public static void Main(string[] args)
  {
    Person p1 = new Person("Ada", "Lovelace");
    Person p2 = new Person("Ada", "Lovelace");
    Console.WriteLine("Value Equals? " + (p1 == p2)); // true
    Person p3 = p1.WithLast("King");
    Console.WriteLine("After With: " + (p1 == p3));   // false
  }
}

รูปแบบความเท่ากันของโครงสร้าง

โดยค่าเริ่มต้น โครงสร้างจะเปรียบเทียบจากฟิลด์ผ่าน ValueType.Equals แต่การนำ IEquatable<T>ไปใช้งานจะทำให้เจตนาและการคำนวณแฮชมีความชัดเจน

using System;

public struct Point2 : IEquatable<Point2>
{
  public int X;
  public int Y;

  public Point2(int x, int y) { X = x; Y = y; }

  public bool Equals(Point2 other)
  {
    return X == other.X && Y == other.Y;
  }

  public override bool Equals(object obj)
  {
    if (!(obj is Point2)) return false;
    return Equals((Point2)obj);
  }

  public override int GetHashCode()
  {
    unchecked { return (X * 397) ^ Y; }
  }

  public static bool operator ==(Point2 a, Point2 b) { return a.Equals(b); }
  public static bool operator !=(Point2 a, Point2 b) { return !a.Equals(b); }
}

public class Program
{
  public static void Main(string[] args)
  {
    Point2 a = new Point2(1, 2);
    Point2 b = new Point2(1, 2);
    Console.WriteLine("Struct value equality: " + (a == b)); // true
  }
}

ออบเจ็กต์แบบค่าในฐานะคีย์

เมื่อออบเจ็กต์แบบค่าถูกใช้เป็นคีย์ของพจนานุกรม การกำหนด Equals/GetHashCode อย่างถูกต้องจะทำให้การค้นหาสำเร็จ

using System;
using System.Collections.Generic;

public sealed class Email : IEquatable<Email>
{
  public string Address { get; private set; }
  public Email(string address)
  {
    if (string.IsNullOrEmpty(address)) throw new ArgumentException("address");
    Address = address.Trim().ToLowerInvariant();
  }

  public bool Equals(Email other)
  {
    if (object.ReferenceEquals(other, null)) return false;
    return Address == other.Address;
  }

  public override bool Equals(object obj) { return Equals(obj as Email); }

  public override int GetHashCode()
  {
    return Address == null ? 0 : Address.GetHashCode();
  }
}

public class Program
{
  public static void Main(string[] args)
  {
    Dictionary<Email, string> owners = new Dictionary<Email, string>();
    owners[new Email("User@Mail.com")] = "Ada";
    Console.WriteLine(owners.ContainsKey(new Email("user@mail.com"))); // true
  }
}

รายการตรวจสอบความเท่ากัน

รายการตรวจสอบ:

  • เก็บข้อมูลให้ไม่เปลี่ยนแปลง
  • แทนที่ Equals และ GetHashCode
  • นำ IEquatable<T>ไปใช้งานเพื่อการเปรียบเทียบที่ปลอดภัยตามชนิดข้อมูลและรวดเร็ว
  • เพิ่ม ==/!= เพื่อความสะดวก
  • ใช้ฟิลด์ชุดเดียวกันอย่างสอดคล้องทั้งในการเปรียบเทียบความเท่ากันและการคำนวณแฮช

ข้อกำหนดความเท่ากันแบบลักษณะเรคคอร์ด

ตรวจสอบความเข้าใจอย่างรวดเร็ว: หากต้องการให้คลาสทำงานเหมือนค่าในลักษณะเรคคอร์ดใน C# 6 ต้องทำอย่างไร

ทบทวน

ทบทวน: ใน C# 6 คุณสร้างชนิดข้อมูลลักษณะเรคคอร์ดด้วยตนเอง ได้แก่ ข้อมูลที่ไม่เปลี่ยนแปลง IEquatable<T>, Equals/GetHashCode ที่ถูกต้อง และอาจเพิ่ม ==/!= ใช้ชนิดข้อมูลเหล่านี้อย่างปลอดภัยในเซตและพจนานุกรม

คำถามที่พบบ่อย

บทเรียน “ชนิดคล้ายเรคคอร์ดและความเท่าเทียมตามค่า (รูปแบบ)” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “ชนิดคล้ายเรคคอร์ดและความเท่าเทียมตามค่า (รูปแบบ)” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส C# Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส C# Academy มีบทเรียนทั้งหมด 3 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “ชนิดคล้ายเรคคอร์ดและความเท่าเทียมตามค่า (รูปแบบ)”

ใช้งานความเท่าเทียมตามค่าแบบเรคคอร์ด: เขียนทับ Equals/GetHashCode ใช้งาน IEquatable เพิ่มตัวดำเนินการ ==/!= และรักษาชนิดให้เปลี่ยนแปลงไม่ได้ คุณปฏิบัติ C# Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน C# Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน C# Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 3 บทเรียน

บทเรียน “ชนิดคล้ายเรคคอร์ดและความเท่าเทียมตามค่า (รูปแบบ)” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน C# Academy นี้ได้ไหม

ได้ บทเรียน C# Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. Structs: ความหมายแบบค่าและรูปแบบเปลี่ยนแปลงไม่ได้
  2. ชนิดคล้ายเรคคอร์ดและความเท่าเทียมตามค่า (รูปแบบ)
  3. Enum และ [Flags]
← กลับไปที่ C# Academy