Bagikan melalui


Object.Equals Metode

Definisi

Menentukan apakah dua instans objek sama.

Overload

Equals(Object)

Menentukan apakah objek yang ditentukan sama dengan objek saat ini.

Equals(Object, Object)

Menentukan apakah instans objek yang ditentukan dianggap sama.

Equals(Object)

Sumber:
Object.cs
Sumber:
Object.cs
Sumber:
Object.cs

Menentukan apakah objek yang ditentukan sama dengan objek saat ini.

public:
 virtual bool Equals(System::Object ^ obj);
public virtual bool Equals (object obj);
public virtual bool Equals (object? obj);
abstract member Equals : obj -> bool
override this.Equals : obj -> bool
Public Overridable Function Equals (obj As Object) As Boolean

Parameter

obj
Object

Objek untuk dibandingkan dengan objek saat ini.

Mengembalikan

true jika objek yang ditentukan sama dengan objek saat ini; jika tidak, false.

Keterangan

Untuk informasi selengkapnya tentang API ini, lihat Keterangan API Tambahan untuk Object.Equals.

Lihat juga

Berlaku untuk

Equals(Object, Object)

Sumber:
Object.cs
Sumber:
Object.cs
Sumber:
Object.cs

Menentukan apakah instans objek yang ditentukan dianggap sama.

public:
 static bool Equals(System::Object ^ objA, System::Object ^ objB);
public static bool Equals (object objA, object objB);
public static bool Equals (object? objA, object? objB);
static member Equals : obj * obj -> bool
Public Shared Function Equals (objA As Object, objB As Object) As Boolean

Parameter

objA
Object

Objek pertama yang dibandingkan.

objB
Object

Objek kedua untuk dibandingkan.

Mengembalikan

true jika objek dianggap sama; jika tidak, false. Jika dan objAobjBnull, metode akan mengembalikan true.

Contoh

Contoh berikut mengilustrasikan Equals(Object, Object) metode dan membandingkannya dengan ReferenceEquals metode .

using System;

public class Example
{
   public static void Main()
   {
      Dog m1 = new Dog("Alaskan Malamute");
      Dog m2 = new Dog("Alaskan Malamute");
      Dog g1 = new Dog("Great Pyrenees");
      Dog g2 = g1;
      Dog d1 = new Dog("Dalmation");
      Dog n1 = null;
      Dog n2 = null;

      Console.WriteLine("null = null: {0}", Object.Equals(n1, n2));
      Console.WriteLine("null Reference Equals null: {0}\n", Object.ReferenceEquals(n1, n2));

      Console.WriteLine("{0} = {1}: {2}", g1, g2, Object.Equals(g1, g2));
      Console.WriteLine("{0} Reference Equals {1}: {2}\n", g1, g2, Object.ReferenceEquals(g1, g2));

      Console.WriteLine("{0} = {1}: {2}", m1, m2, Object.Equals(m1, m2));
      Console.WriteLine("{0} Reference Equals {1}: {2}\n", m1, m2, Object.ReferenceEquals(m1, m2));

      Console.WriteLine("{0} = {1}: {2}", m1, d1, Object.Equals(m1, d1));
      Console.WriteLine("{0} Reference Equals {1}: {2}", m1, d1, Object.ReferenceEquals(m1, d1));
   }
}

public class Dog
{
   // Public field.
   public string Breed;

   // Class constructor.
   public Dog(string dogBreed)
   {
      this.Breed = dogBreed;
   }

   public override bool Equals(Object obj)
   {
      if (obj == null || !(obj is Dog))
         return false;
      else
         return this.Breed == ((Dog) obj).Breed;
   }

   public override int GetHashCode()
   {
      return this.Breed.GetHashCode();
   }

   public override string ToString()
   {
      return this.Breed;
   }
}
// The example displays the following output:
//       null = null: True
//       null Reference Equals null: True
//
//       Great Pyrenees = Great Pyrenees: True
//       Great Pyrenees Reference Equals Great Pyrenees: True
//
//       Alaskan Malamute = Alaskan Malamute: True
//       Alaskan Malamute Reference Equals Alaskan Malamute: False
//
//       Alaskan Malamute = Dalmation: False
//       Alaskan Malamute Reference Equals Dalmation: False
open System

// Class constructor
type Dog(dogBreed) =
    // Public property.
    member _.Breed = dogBreed

    override this.Equals(obj) =
        match obj with
        | :? Dog as dog when dog.Breed = this.Breed -> true
        | _ -> false

    override _.GetHashCode() =
        dogBreed.GetHashCode()

    override _.ToString() =
        dogBreed

let m1 = Dog "Alaskan Malamute"
let m2 = Dog "Alaskan Malamute"
let g1 = Dog "Great Pyrenees"
let g2 = g1
let d1 = Dog "Dalmation"
let n1 = Unchecked.defaultof<Dog>
let n2 = Unchecked.defaultof<Dog>

printfn $"null = null: {Object.Equals(n1, n2)}"
printfn $"null Reference Equals null: {Object.ReferenceEquals(n1, n2)}\n"

printfn $"{g1} = {g2}: {Object.Equals(g1, g2)}"
printfn $"{g1} Reference Equals {g2}: {Object.ReferenceEquals(g1, g2)}\n"

printfn $"{m1} = {m2}: {Object.Equals(m1, m2)}"
printfn $"{m1} Reference Equals {m2}: {Object.ReferenceEquals(m1, m2)}\n"

printfn $"{m1} = {d1}: {Object.Equals(m1, d1)}"
printfn $"{m1} Reference Equals {d1}: {Object.ReferenceEquals(m1, d1)}"

// The example displays the following output:
//       null = null: True
//       null Reference Equals null: True
//
//       Great Pyrenees = Great Pyrenees: True
//       Great Pyrenees Reference Equals Great Pyrenees: True
//
//       Alaskan Malamute = Alaskan Malamute: True
//       Alaskan Malamute Reference Equals Alaskan Malamute: False
//
//       Alaskan Malamute = Dalmation: False
//       Alaskan Malamute Reference Equals Dalmation: False
Module Example
   Public Sub Main()
      Dim m1 As New Dog("Alaskan Malamute")
      Dim m2 As New Dog("Alaskan Malamute")
      Dim g1 As New Dog("Great Pyrenees")
      Dim g2 As Dog = g1
      Dim d1 As New Dog("Dalmation")
      Dim n1 As Dog = Nothing
      Dim n2 As Dog = Nothing
      
      Console.WriteLine("null = null: {0}", Object.Equals(n1, n2))
      Console.WriteLine("null Reference Equals null: {0}", Object.ReferenceEquals(n1, n2))
      Console.WriteLine()
      
      Console.WriteLine("{0} = {1}: {2}", g1, g2, Object.Equals(g1, g2))
      Console.WriteLine("{0} Reference Equals {1}: {2}", g1, g2, Object.ReferenceEquals(g1, g2))
      Console.WriteLine()
      
      Console.WriteLine("{0} = {1}: {2}", m1, m2, Object.Equals(m1, m2))
      Console.WriteLine("{0} Reference Equals {1}: {2}", m1, m2, Object.ReferenceEquals(m1, m2))
      Console.WriteLine()
      
      Console.WriteLine("{0} = {1}: {2}", m1, d1, Object.Equals(m1, d1))  
      Console.WriteLine("{0} Reference Equals {1}: {2}", m1, d1, Object.ReferenceEquals(m1, d1))  
   End Sub
End Module

Public Class Dog
   ' Public field.
   Public Breed As String
   
   ' Class constructor.
   Public Sub New(dogBreed As String)
      Me.Breed = dogBreed
   End Sub

   Public Overrides Function Equals(obj As Object) As Boolean
      If obj Is Nothing OrElse Not typeof obj Is Dog Then
         Return False
      Else
         Return Me.Breed = CType(obj, Dog).Breed
      End If   
   End Function
   
   Public Overrides Function GetHashCode() As Integer
      Return Me.Breed.GetHashCode()
   End Function
   
   Public Overrides Function ToString() As String
      Return Me.Breed
   End Function
End Class
' The example displays the following output:
'       null = null: True
'       null Reference Equals null: True
'       
'       Great Pyrenees = Great Pyrenees: True
'       Great Pyrenees Reference Equals Great Pyrenees: True
'       
'       Alaskan Malamute = Alaskan Malamute: True
'       Alaskan Malamute Reference Equals Alaskan Malamute: False
'       
'       Alaskan Malamute = Dalmation: False
'       Alaskan Malamute Reference Equals Dalmation: False

Keterangan

Metode statis Equals(Object, Object) menunjukkan apakah dua objek, objA dan objB, sama. Ini juga memungkinkan Anda untuk menguji objek yang nilainya null untuk kesetaraan. Ini membandingkan objA dan objB untuk kesetaraan sebagai berikut:

  • Ini menentukan apakah kedua objek mewakili referensi objek yang sama. Jika mereka melakukannya, metode mengembalikan true. Pengujian ini setara dengan memanggil ReferenceEquals metode . Selain itu, jika keduanya objA dan objBnull, metode mengembalikan true.

  • Ini menentukan apakah baik objA atau objBnull. Jika demikian, ia mengembalikan false.

  • Jika kedua objek tidak mewakili referensi objek yang sama dan tidak keduanya null, maka akan memanggil objA.Equals(objB) dan mengembalikan hasilnya. Ini berarti bahwa jika objA mengambil alih Object.Equals(Object) metode , penimpaan ini dipanggil.

Lihat juga

Berlaku untuk