From eae4150e26638689475509071f9a491bacde224f Mon Sep 17 00:00:00 2001 From: antonfirsov Date: Wed, 12 Mar 2025 16:16:02 +0100 Subject: [PATCH 1/3] initial attempt --- .../System/IEquatableT/Equals/EqualsEx2.cs | 95 ++++----------- .../System/IEquatableT/Equals/Snippet12.cs | 95 ++++----------- .../System/IEquatableT/Equals/EqualsEx2.fs | 60 +++------ .../System/IEquatableT/Equals/Snippet12.fs | 59 +++------ .../vb/EqualsEx2.vb | 110 ++++++----------- .../vb/Snippet12.vb | 114 ++++++------------ xml/System/IEquatable`1.xml | 16 ++- 7 files changed, 165 insertions(+), 384 deletions(-) diff --git a/snippets/csharp/System/IEquatableT/Equals/EqualsEx2.cs b/snippets/csharp/System/IEquatableT/Equals/EqualsEx2.cs index b7e12b95342..0ec596aea25 100644 --- a/snippets/csharp/System/IEquatableT/Equals/EqualsEx2.cs +++ b/snippets/csharp/System/IEquatableT/Equals/EqualsEx2.cs @@ -1,84 +1,41 @@ // -using System; -using System.Collections.Generic; -using System.Text.RegularExpressions; - public class Person : IEquatable { - private string uniqueSsn; - private string lName; - - public Person(string lastName, string ssn) - { - if (Regex.IsMatch(ssn, @"\d{9}")) - uniqueSsn = $"{ssn.Substring(0, 3)}-{ssn.Substring(3, 2)}-{ssn.Substring(5, 4)}"; - else if (Regex.IsMatch(ssn, @"\d{3}-\d{2}-\d{4}")) - uniqueSsn = ssn; - else - throw new FormatException("The social security number has an invalid format."); - - this.LastName = lastName; - } - - public string SSN - { - get { return this.uniqueSsn; } - } - - public string LastName - { - get { return this.lName; } - set { - if (String.IsNullOrEmpty(value)) - throw new ArgumentException("The last name cannot be null or empty."); - else - this.lName = value; - } - } + public Person(string lastName, string ssn) + { + LastName = lastName; + SSN = ssn; + } - public bool Equals(Person other) - { - if (other == null) - return false; + public string LastName { get; } - if (this.uniqueSsn == other.uniqueSsn) - return true; - else - return false; - } + public string SSN { get; } - public override bool Equals(Object obj) - { - if (obj == null) - return false; + public bool Equals(Person? other) => other is not null && other.SSN == SSN; - Person personObj = obj as Person; - if (personObj == null) - return false; - else - return Equals(personObj); - } + public override bool Equals(object? obj) => Equals(obj as Person); - public override int GetHashCode() - { - return this.SSN.GetHashCode(); - } + public override int GetHashCode() => SSN.GetHashCode(); - public static bool operator == (Person person1, Person person2) - { - if (((object)person1) == null || ((object)person2) == null) - return Object.Equals(person1, person2); + public static bool operator ==(Person person1, Person person2) + { + if (person1 is null) + { + return person2 is null; + } - return person1.Equals(person2); - } + return person1.Equals(person2); + } - public static bool operator != (Person person1, Person person2) - { - if (((object)person1) == null || ((object)person2) == null) - return ! Object.Equals(person1, person2); + public static bool operator !=(Person person1, Person person2) + { + if (person1 is null) + { + return person2 is not null; + } - return ! (person1.Equals(person2)); - } + return !person1.Equals(person2); + } } // diff --git a/snippets/csharp/System/IEquatableT/Equals/Snippet12.cs b/snippets/csharp/System/IEquatableT/Equals/Snippet12.cs index cfb5e526740..d159d9bbde2 100644 --- a/snippets/csharp/System/IEquatableT/Equals/Snippet12.cs +++ b/snippets/csharp/System/IEquatableT/Equals/Snippet12.cs @@ -4,84 +4,41 @@ public class Person : IEquatable { - private string uniqueSsn; - private string lName; + public Person(string lastName, string ssn) + { + LastName = lastName; + SSN = ssn; + } - public Person(string lastName, string ssn) - { - this.SSN = ssn; - this.LastName = lastName; - } + public string LastName { get; } - public string SSN - { - get { return this.uniqueSsn; } - set { - if (Regex.IsMatch(value, @"\d{9}")) - uniqueSsn = String.Format("{0}-(1}-{2}", value.Substring(0, 3), - value.Substring(3, 2), - value.Substring(5, 4)); - else if (Regex.IsMatch(value, @"\d{3}-\d{2}-\d{4}")) - uniqueSsn = value; - else - throw new FormatException("The social security number has an invalid format."); - } - } + public string SSN { get; } - public string LastName - { - get { return this.lName; } - set { - if (String.IsNullOrEmpty(value)) - throw new ArgumentException("The last name cannot be null or empty."); - else - this.lName = value; - } - } + public bool Equals(Person? other) => other is not null && other.SSN == SSN; - public bool Equals(Person other) - { - if (other == null) - return false; + public override bool Equals(object? obj) => Equals(obj as Person); - if (this.uniqueSsn == other.uniqueSsn) - return true; - else - return false; - } + public override int GetHashCode() => SSN.GetHashCode(); - public override bool Equals(Object obj) - { - if (obj == null) - return false; + public static bool operator ==(Person person1, Person person2) + { + if (person1 is null) + { + return person2 is null; + } - Person personObj = obj as Person; - if (personObj == null) - return false; - else - return Equals(personObj); - } - - public override int GetHashCode() - { - return this.SSN.GetHashCode(); - } - - public static bool operator == (Person person1, Person person2) - { - if (((object)person1) == null || ((object)person2) == null) - return Object.Equals(person1, person2); + return person1.Equals(person2); + } - return person1.Equals(person2); - } + public static bool operator !=(Person person1, Person person2) + { + if (person1 is null) + { + return person2 is not null; + } - public static bool operator != (Person person1, Person person2) - { - if (((object)person1) == null || ((object)person2) == null) - return ! Object.Equals(person1, person2); - - return ! (person1.Equals(person2)); - } + return !person1.Equals(person2); + } } // diff --git a/snippets/fsharp/System/IEquatableT/Equals/EqualsEx2.fs b/snippets/fsharp/System/IEquatableT/Equals/EqualsEx2.fs index 29e42d75a6a..d78aba7c988 100644 --- a/snippets/fsharp/System/IEquatableT/Equals/EqualsEx2.fs +++ b/snippets/fsharp/System/IEquatableT/Equals/EqualsEx2.fs @@ -2,56 +2,28 @@ // open System -open System.Text.RegularExpressions -type Person(lastName, ssn) = - let mutable lastName = lastName - let ssn = - if Regex.IsMatch(ssn, @"\d{9}") then - $"{ssn.Substring(0, 3)}-{ssn.Substring(3, 2)}-{ssn.Substring(5, 4)}" - elif Regex.IsMatch(ssn, @"\d{3}-\d{2}-\d{4}") then - ssn - else - raise (FormatException "The social security number has an invalid format.") +type Person(lastName: string, ssn: string) = + member this.LastName = lastName + member this.SSN = ssn - member _.SSN = - ssn - - member _.LastName - with get () = lastName - and set (value) = - if String.IsNullOrEmpty value then - invalidArg (nameof value) "The last name cannot be null or empty." - else - lastName <- value - - static member op_Equality (person1: Person, person2: Person) = - if box person1 |> isNull || box person2 |> isNull then - Object.Equals(person1, person2) - else - person1.Equals person2 - - static member op_Inequality (person1: Person, person2: Person) = - if box person1 |> isNull || box person2 |> isNull then - Object.Equals(person1, person2) |> not - else - person1.Equals person2 |> not - - override _.GetHashCode() = - ssn.GetHashCode() + interface IEquatable with + member this.Equals(other: Person) = + other.SSN = this.SSN override this.Equals(obj: obj) = - match obj with - | :? Person as personObj -> - (this :> IEquatable<_>).Equals personObj + match obj with + | :? Person as person -> (this :> IEquatable).Equals(person) | _ -> false - interface IEquatable with - member this.Equals(other: Person) = - match box other with - | null -> false - | _ -> - this.SSN = other.SSN + override this.GetHashCode() = + this.SSN.GetHashCode() + + static member (==) (person1: Person, person2: Person) = + person1.Equals(person2) + + static member (!=) (person1: Person, person2: Person) = + not (person1.Equals(person2)) // // Create a Person object for each job applicant. let applicant1 = Person("Jones", "099-29-4999") diff --git a/snippets/fsharp/System/IEquatableT/Equals/Snippet12.fs b/snippets/fsharp/System/IEquatableT/Equals/Snippet12.fs index 39e48b695c9..ec85aff5712 100644 --- a/snippets/fsharp/System/IEquatableT/Equals/Snippet12.fs +++ b/snippets/fsharp/System/IEquatableT/Equals/Snippet12.fs @@ -2,54 +2,29 @@ open System.Collections.Generic open System.Text.RegularExpressions -type Person(lastName, ssn) = - let mutable lastName = lastName - let ssn = - if Regex.IsMatch(ssn, @"\d{9}") then - $"{ssn.Substring(0, 3)}-{ssn.Substring(3, 2)}-{ssn.Substring(5, 4)}" - elif Regex.IsMatch(ssn, @"\d{3}-\d{2}-\d{4}") then - ssn - else - raise (FormatException "The social security number has an invalid format.") +open System - member _.SSN = - ssn +type Person(lastName: string, ssn: string) = + member this.LastName = lastName + member this.SSN = ssn - member _.LastName - with get () = lastName - and set (value) = - if String.IsNullOrEmpty value then - invalidArg (nameof value) "The last name cannot be null or empty." - else - lastName <- value - - static member op_Equality (person1: Person, person2: Person) = - if box person1 |> isNull || box person2 |> isNull then - Object.Equals(person1, person2) - else - person1.Equals person2 - - static member op_Inequality (person1: Person, person2: Person) = - if box person1 |> isNull || box person2 |> isNull then - Object.Equals(person1, person2) |> not - else - person1.Equals person2 |> not - - override _.GetHashCode() = - ssn.GetHashCode() + interface IEquatable with + member this.Equals(other: Person) = + other.SSN = this.SSN override this.Equals(obj: obj) = - match obj with - | :? Person as personObj -> - (this :> IEquatable<_>).Equals personObj + match obj with + | :? Person as person -> (this :> IEquatable).Equals(person) | _ -> false - interface IEquatable with - member this.Equals(other: Person) = - match box other with - | null -> false - | _ -> - this.SSN = other.SSN + override this.GetHashCode() = + this.SSN.GetHashCode() + + static member (==) (person1: Person, person2: Person) = + person1.Equals(person2) + + static member (!=) (person1: Person, person2: Person) = + not (person1.Equals(person2)) // // Create a Person object for each job applicant. diff --git a/snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/EqualsEx2.vb b/snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/EqualsEx2.vb index 425406deef9..3ce5cb29844 100644 --- a/snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/EqualsEx2.vb +++ b/snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/EqualsEx2.vb @@ -2,84 +2,42 @@ Option Strict On ' -Imports System.Collections.Generic -Imports System.Text.RegularExpressions +Public Class Person + Implements IEquatable(Of Person) -Public Class Person : Implements IEquatable(Of Person) - Private uniqueSsn As String - Private lName As String - - Public Sub New(lastName As String, ssn As String) - If Regex.IsMatch(ssn, "\d{9}") Then - uniqueSsn = $"{ssn.Substring(0, 3)}-{ssn.Substring(3, 2)}-{ssn.Substring(5, 4)}" - ElseIf Regex.IsMatch(ssn, "\d{3}-\d{2}-\d{4}") Then - uniqueSsn = ssn - Else - Throw New FormatException("The social security number has an invalid format.") - End If - Me.LastName = lastName - End Sub - - Public ReadOnly Property SSN As String - Get - Return Me.uniqueSsn - End Get - End Property - - Public Property LastName As String - Get - Return Me.lName - End Get - Set - If String.IsNullOrEmpty(value) Then - Throw New ArgumentException("The last name cannot be null or empty.") - Else - lname = value - End If - End Set - End Property - - Public Overloads Function Equals(other As Person) As Boolean _ - Implements IEquatable(Of Person).Equals - If other Is Nothing Then Return False - - If Me.uniqueSsn = other.uniqueSsn Then - Return True - Else - Return False - End If - End Function + Public Sub New(ByVal lastName As String, ByVal ssn As String) + Me.LastName = lastName + Me.SSN = ssn + End Sub - Public Overrides Function Equals(obj As Object) As Boolean - If obj Is Nothing Then Return False - - Dim personObj As Person = TryCast(obj, Person) - If personObj Is Nothing Then - Return False - Else - Return Equals(personObj) - End If - End Function - - Public Overrides Function GetHashCode() As Integer - Return Me.SSN.GetHashCode() - End Function - - Public Shared Operator = (person1 As Person, person2 As Person) As Boolean - If person1 Is Nothing OrElse person2 Is Nothing Then - Return Object.Equals(person1, person2) - End If - - Return person1.Equals(person2) - End Operator - - Public Shared Operator <> (person1 As Person, person2 As Person) As Boolean - If person1 Is Nothing OrElse person2 Is Nothing Then - Return Not Object.Equals(person1, person2) - End If - - Return Not person1.Equals(person2) - End Operator + Public ReadOnly Property LastName As String + Public ReadOnly Property SSN As String + + Public Overloads Function Equals(ByVal other As Person) As Boolean Implements IEquatable(Of Person).Equals + Return other IsNot Nothing AndAlso other.SSN = Me.SSN + End Function + + Public Overrides Function Equals(ByVal obj As Object) As Boolean + Return Equals(TryCast(obj, Person)) + End Function + + Public Overrides Function GetHashCode() As Integer + Return SSN.GetHashCode() + End Function + + Public Shared Operator =(ByVal person1 As Person, ByVal person2 As Person) As Boolean + If person1 Is Nothing Then + Return person2 Is Nothing + End If + Return person1.Equals(person2) + End Operator + + Public Shared Operator <>(ByVal person1 As Person, ByVal person2 As Person) As Boolean + If person1 Is Nothing Then + Return person2 IsNot Nothing + End If + Return Not person1.Equals(person2) + End Operator End Class ' diff --git a/snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/Snippet12.vb b/snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/Snippet12.vb index 6de5888d74a..e97732d8bfd 100644 --- a/snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/Snippet12.vb +++ b/snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/Snippet12.vb @@ -4,86 +4,42 @@ Option Strict On Imports System.Collections.Generic Imports System.Text.RegularExpressions -Public Class Person : Implements IEquatable(Of Person) - Private uniqueSsn As String - Private lName As String - - Public Sub New(lastName As String, ssn As String) - Me.SSN = ssn - Me.LastName = lastName - End Sub - - Public Property SSN As String - Set - If Regex.IsMatch(value, "\d{9}") Then - uniqueSsn = String.Format("{0}-(1}-{2}", value.Substring(0, 3), _ - value.Substring(3, 2), _ - value.Substring(5, 4)) - ElseIf Regex.IsMatch(value, "\d{3}-\d{2}-\d{4}") Then - uniqueSsn = value - Else - Throw New FormatException("The social security number has an invalid format.") - End If - End Set - Get - Return Me.uniqueSsn - End Get - End Property - - Public Property LastName As String - Get - Return Me.lName - End Get - Set - If String.IsNullOrEmpty(value) Then - Throw New ArgumentException("The last name cannot be null or empty.") - Else - lname = value - End If - End Set - End Property - - Public Overloads Function Equals(other As Person) As Boolean _ - Implements IEquatable(Of Person).Equals - If other Is Nothing Then Return False - - If Me.uniqueSsn = other.uniqueSsn Then - Return True - Else - Return False - End If - End Function +Public Class Person + Implements IEquatable(Of Person) - Public Overrides Function Equals(obj As Object) As Boolean - If obj Is Nothing Then Return False - - Dim personObj As Person = TryCast(obj, Person) - If personObj Is Nothing Then - Return False - Else - Return Equals(personObj) - End If - End Function - - Public Overrides Function GetHashCode() As Integer - Return Me.SSN.GetHashCode() - End Function - - Public Shared Operator = (person1 As Person, person2 As Person) As Boolean - If person1 Is Nothing OrElse person2 Is Nothing Then - Return Object.Equals(person1, person2) - End If - - Return person1.Equals(person2) - End Operator - - Public Shared Operator <> (person1 As Person, person2 As Person) As Boolean - If person1 Is Nothing OrElse person2 Is Nothing Then - Return Not Object.Equals(person1, person2) - End If - - Return Not person1.Equals(person2) - End Operator + Public Sub New(ByVal lastName As String, ByVal ssn As String) + Me.LastName = lastName + Me.SSN = ssn + End Sub + + Public ReadOnly Property LastName As String + Public ReadOnly Property SSN As String + + Public Overloads Function Equals(ByVal other As Person) As Boolean Implements IEquatable(Of Person).Equals + Return other IsNot Nothing AndAlso other.SSN = Me.SSN + End Function + + Public Overrides Function Equals(ByVal obj As Object) As Boolean + Return Equals(TryCast(obj, Person)) + End Function + + Public Overrides Function GetHashCode() As Integer + Return SSN.GetHashCode() + End Function + + Public Shared Operator =(ByVal person1 As Person, ByVal person2 As Person) As Boolean + If person1 Is Nothing Then + Return person2 Is Nothing + End If + Return person1.Equals(person2) + End Operator + + Public Shared Operator <>(ByVal person1 As Person, ByVal person2 As Person) As Boolean + If person1 Is Nothing Then + Return person2 IsNot Nothing + End If + Return Not person1.Equals(person2) + End Operator End Class ' diff --git a/xml/System/IEquatable`1.xml b/xml/System/IEquatable`1.xml index b30e47c72cc..67915f777b9 100644 --- a/xml/System/IEquatable`1.xml +++ b/xml/System/IEquatable`1.xml @@ -60,11 +60,16 @@ > The interface defines the method, which determines the sort order of instances of the implementing type. The interface defines the method, which determines the equality of instances of the implementing type. The interface is used by generic collection objects such as , , and when testing for equality in such methods as `Contains`, `IndexOf`, `LastIndexOf`, and `Remove`. It should be implemented for any object that might be stored in a generic collection. - - - + + See remarks on the for more information about implementing the interface. + ## Examples - See the example for the method. + The following example shows the partial implementation of a `Person` class that implements and has two properties, `LastName` and `SSN`. `SSN` is considered to be a unique identifier, therefore the method returns `True` if the `SSN` property of two `Person` objects is identical; otherwise, it returns `False`. + Note that the F# example does not handle `null` values for `Person` instances. + + :::code language="csharp" source="~/snippets/csharp/System/IEquatableT/Equals/EqualsEx2.cs" id="Snippet3"::: + :::code language="fsharp" source="~/snippets/fsharp/System/IEquatableT/Equals/EqualsEx2.fs" id="Snippet3"::: + :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/EqualsEx2.vb" id="Snippet3"::: ]]> @@ -163,7 +168,8 @@ ## Examples - The following example shows the partial implementation of a `Person` class that implements and has two properties, `LastName` and `SSN`. The method returns `True` if the `SSN` property of two `Person` objects is identical; otherwise, it returns `False`. + The following example shows the partial implementation of a `Person` class that implements and has two properties, `LastName` and `SSN`. `SSN` is considered to be a unique identifier, therefore the method returns `True` if the `SSN` property of two `Person` objects is identical; otherwise, it returns `False`. + Note that the F# example does not handle `null` values for `Person` instances. :::code language="csharp" source="~/snippets/csharp/System/IEquatableT/Equals/EqualsEx2.cs" id="Snippet3"::: :::code language="fsharp" source="~/snippets/fsharp/System/IEquatableT/Equals/EqualsEx2.fs" id="Snippet3"::: From b240967017d3e9cf96de1ce16888e010e52684ad Mon Sep 17 00:00:00 2001 From: antonfirsov Date: Wed, 12 Mar 2025 16:53:11 +0100 Subject: [PATCH 2/3] Rework IEquatable example --- .../System/IEquatableT/Equals/Equals.csproj | 10 ++ .../System/IEquatableT/Equals/EqualsEx1.cs | 137 ------------------ .../System/IEquatableT/Equals/EqualsEx2.cs | 88 ----------- .../IEquatableT/Equals/EqualsExample.cs | 56 +++++++ .../System/IEquatableT/Equals/Snippet12.cs | 92 ------------ .../System/IEquatableT/Equals/Equals.fsproj | 12 ++ .../System/IEquatableT/Equals/EqualsEx2.fs | 65 --------- .../IEquatableT/Equals/EqualsExample.fs | 39 +++++ .../System/IEquatableT/Equals/Snippet12.fs | 55 ------- .../System/IEquatableT/Equals/fs.fsproj | 11 -- .../vb/Equals.vbproj | 9 ++ .../vb/EqualsEx2.vb | 78 ---------- .../vb/EqualsExample.vb | 61 ++++++++ .../vb/Snippet12.vb | 81 ----------- xml/System/IEquatable`1.xml | 24 +-- 15 files changed, 199 insertions(+), 619 deletions(-) create mode 100644 snippets/csharp/System/IEquatableT/Equals/Equals.csproj delete mode 100644 snippets/csharp/System/IEquatableT/Equals/EqualsEx1.cs delete mode 100644 snippets/csharp/System/IEquatableT/Equals/EqualsEx2.cs create mode 100644 snippets/csharp/System/IEquatableT/Equals/EqualsExample.cs delete mode 100644 snippets/csharp/System/IEquatableT/Equals/Snippet12.cs create mode 100644 snippets/fsharp/System/IEquatableT/Equals/Equals.fsproj delete mode 100644 snippets/fsharp/System/IEquatableT/Equals/EqualsEx2.fs create mode 100644 snippets/fsharp/System/IEquatableT/Equals/EqualsExample.fs delete mode 100644 snippets/fsharp/System/IEquatableT/Equals/Snippet12.fs delete mode 100644 snippets/fsharp/System/IEquatableT/Equals/fs.fsproj create mode 100644 snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/Equals.vbproj delete mode 100644 snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/EqualsEx2.vb create mode 100644 snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/EqualsExample.vb delete mode 100644 snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/Snippet12.vb diff --git a/snippets/csharp/System/IEquatableT/Equals/Equals.csproj b/snippets/csharp/System/IEquatableT/Equals/Equals.csproj new file mode 100644 index 00000000000..2150e3797ba --- /dev/null +++ b/snippets/csharp/System/IEquatableT/Equals/Equals.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + + diff --git a/snippets/csharp/System/IEquatableT/Equals/EqualsEx1.cs b/snippets/csharp/System/IEquatableT/Equals/EqualsEx1.cs deleted file mode 100644 index 66a01f732b9..00000000000 --- a/snippets/csharp/System/IEquatableT/Equals/EqualsEx1.cs +++ /dev/null @@ -1,137 +0,0 @@ -// -using System; -using System.Collections.Generic; -using System.Text.RegularExpressions; - -public class Person : IEquatable -{ - private string uniqueSsn; - private string lName; - - public Person(string lastName, string ssn) - { - this.SSN = ssn; - this.LastName = lastName; - } - - public string SSN - { - get { return this.uniqueSsn; } - set { - if (Regex.IsMatch(value, @"\d{9}")) - uniqueSsn = String.Format("{0}-(1}-{2}", value.Substring(0, 3), - value.Substring(3, 2), - value.Substring(5, 4)); - else if (Regex.IsMatch(value, @"\d{3}-\d{2}-\d{4}")) - uniqueSsn = value; - else - throw new FormatException("The social security number has an invalid format."); - } - } - - public string LastName - { - get { return this.lName; } - set { - if (String.IsNullOrEmpty(value)) - throw new ArgumentException("The last name cannot be null or empty."); - else - this.lName = value; - } - } - - public bool Equals(Person other) - { - if (other == null) - return false; - - if (this.uniqueSsn == other.uniqueSsn) - return true; - else - return false; - } - - public override bool Equals(Object obj) - { - if (obj == null) - return false; - - Person personObj = obj as Person; - if (personObj == null) - return false; - else - return Equals(personObj); - } - - public override int GetHashCode() - { - return this.SSN.GetHashCode(); - } - - public static bool operator == (Person person1, Person person2) - { - if (((object)person1) == null || ((object)person2) == null) - return Object.Equals(person1, person2); - - return person1.Equals(person2); - } - - public static bool operator != (Person person1, Person person2) - { - if (((object)person1) == null || ((object)person2) == null) - return ! Object.Equals(person1, person2); - - return ! (person1.Equals(person2)); - } -} -// - -// Snippet2> -public class TestIEquatable -{ - public static void Main() - { - // Create a Person object for each job applicant. - Person applicant1 = new Person("Jones", "099-29-4999"); - Person applicant2 = new Person("Jones", "199-29-3999"); - Person applicant3 = new Person("Jones", "299-49-6999"); - - // Add applicants to a List object. - List applicants = new List(); - applicants.Add(applicant1); - applicants.Add(applicant2); - applicants.Add(applicant3); - - // Create a Person object for the final candidate. - Person candidate = new Person("Jones", "199-29-3999"); - if (applicants.Contains(candidate)) - Console.WriteLine("Found {0} (SSN {1}).", - candidate.LastName, candidate.SSN); - else - Console.WriteLine("Applicant {0} not found.", candidate.SSN); - - // Call the shared inherited Equals(Object, Object) method. - // It will in turn call the IEquatable(Of T).Equals implementation. - Console.WriteLine("{0}({1}) already on file: {2}.", - applicant2.LastName, - applicant2.SSN, - Person.Equals(applicant2, candidate)); - } -} -// The example displays the following output: -// Found Jones (SSN 199-29-3999). -// Jones(199-29-3999) already on file: True. -// - -// This tests the handling of null values, but does not appear in the documentation. -// -//public class Example -//{ -// public static void Main() -// { -// var p1 = new Person("Joe", "613-24-0068"); -// Person p2 = null; -// -// Console.WriteLine(p1 == p2); -// } -//} diff --git a/snippets/csharp/System/IEquatableT/Equals/EqualsEx2.cs b/snippets/csharp/System/IEquatableT/Equals/EqualsEx2.cs deleted file mode 100644 index 0ec596aea25..00000000000 --- a/snippets/csharp/System/IEquatableT/Equals/EqualsEx2.cs +++ /dev/null @@ -1,88 +0,0 @@ -// -public class Person : IEquatable -{ - public Person(string lastName, string ssn) - { - LastName = lastName; - SSN = ssn; - } - - public string LastName { get; } - - public string SSN { get; } - - public bool Equals(Person? other) => other is not null && other.SSN == SSN; - - public override bool Equals(object? obj) => Equals(obj as Person); - - public override int GetHashCode() => SSN.GetHashCode(); - - public static bool operator ==(Person person1, Person person2) - { - if (person1 is null) - { - return person2 is null; - } - - return person1.Equals(person2); - } - - public static bool operator !=(Person person1, Person person2) - { - if (person1 is null) - { - return person2 is not null; - } - - return !person1.Equals(person2); - } -} -// - -public class TestIEquatable -{ - public static void Main() - { - // Create a Person object for each job applicant. - Person applicant1 = new Person("Jones", "099-29-4999"); - Person applicant2 = new Person("Jones", "199-29-3999"); - Person applicant3 = new Person("Jones", "299-49-6999"); - - // Add applicants to a List object. - List applicants = new List(); - applicants.Add(applicant1); - applicants.Add(applicant2); - applicants.Add(applicant3); - - // Create a Person object for the final candidate. - Person candidate = new Person("Jones", "199-29-3999"); - if (applicants.Contains(candidate)) - Console.WriteLine("Found {0} (SSN {1}).", - candidate.LastName, candidate.SSN); - else - Console.WriteLine("Applicant {0} not found.", candidate.SSN); - - // Call the shared inherited Equals(Object, Object) method. - // It will in turn call the IEquatable(Of T).Equals implementation. - Console.WriteLine("{0}({1}) already on file: {2}.", - applicant2.LastName, - applicant2.SSN, - Person.Equals(applicant2, candidate)); - } -} -// The example displays the following output: -// Found Jones (SSN 199-29-3999). -// Jones(199-29-3999) already on file: True. - -// This tests the handling of null values, but does not appear in the documentation. -// -//public class Example -//{ -// public static void Main() -// { -// var p1 = new Person("Joe", "613-24-0068"); -// Person p2 = null; -// -// Console.WriteLine(p1 == p2); -// } -//} diff --git a/snippets/csharp/System/IEquatableT/Equals/EqualsExample.cs b/snippets/csharp/System/IEquatableT/Equals/EqualsExample.cs new file mode 100644 index 00000000000..e5e6a43bac0 --- /dev/null +++ b/snippets/csharp/System/IEquatableT/Equals/EqualsExample.cs @@ -0,0 +1,56 @@ +// +List applicants = new List() +{ + new Person("Jones", "099-29-4999"), + new Person("Jones", "199-29-3999"), + new Person("Jones", "299-49-6999") +}; + +// Create a Person object for the final candidate. +Person candidate = new Person("Jones", "199-29-3999"); +bool contains = applicants.Contains(candidate); +Console.WriteLine($"{candidate.LastName} ({candidate.NationalId}) is on record: {contains}"); +// The example prints the following output: +// Jones (199-29-3999) is on record: True +// + +// +public class Person : IEquatable +{ + public Person(string lastName, string ssn) + { + LastName = lastName; + NationalId = ssn; + } + + public string LastName { get; } + + public string NationalId { get; } + + public bool Equals(Person? other) => other is not null && other.NationalId == NationalId; + + public override bool Equals(object? obj) => Equals(obj as Person); + + public override int GetHashCode() => NationalId.GetHashCode(); + + public static bool operator ==(Person person1, Person person2) + { + if (person1 is null) + { + return person2 is null; + } + + return person1.Equals(person2); + } + + public static bool operator !=(Person person1, Person person2) + { + if (person1 is null) + { + return person2 is not null; + } + + return !person1.Equals(person2); + } +} +// diff --git a/snippets/csharp/System/IEquatableT/Equals/Snippet12.cs b/snippets/csharp/System/IEquatableT/Equals/Snippet12.cs deleted file mode 100644 index d159d9bbde2..00000000000 --- a/snippets/csharp/System/IEquatableT/Equals/Snippet12.cs +++ /dev/null @@ -1,92 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text.RegularExpressions; - -public class Person : IEquatable -{ - public Person(string lastName, string ssn) - { - LastName = lastName; - SSN = ssn; - } - - public string LastName { get; } - - public string SSN { get; } - - public bool Equals(Person? other) => other is not null && other.SSN == SSN; - - public override bool Equals(object? obj) => Equals(obj as Person); - - public override int GetHashCode() => SSN.GetHashCode(); - - public static bool operator ==(Person person1, Person person2) - { - if (person1 is null) - { - return person2 is null; - } - - return person1.Equals(person2); - } - - public static bool operator !=(Person person1, Person person2) - { - if (person1 is null) - { - return person2 is not null; - } - - return !person1.Equals(person2); - } -} - -// -public class TestIEquatable -{ - public static void Main() - { - // Create a Person object for each job applicant. - Person applicant1 = new Person("Jones", "099-29-4999"); - Person applicant2 = new Person("Jones", "199-29-3999"); - Person applicant3 = new Person("Jones", "299-49-6999"); - - // Add applicants to a List object. - List applicants = new List(); - applicants.Add(applicant1); - applicants.Add(applicant2); - applicants.Add(applicant3); - - // Create a Person object for the final candidate. - Person candidate = new Person("Jones", "199-29-3999"); - if (applicants.Contains(candidate)) - Console.WriteLine("Found {0} (SSN {1}).", - candidate.LastName, candidate.SSN); - else - Console.WriteLine("Applicant {0} not found.", candidate.SSN); - - // Call the shared inherited Equals(Object, Object) method. - // It will in turn call the IEquatable(Of T).Equals implementation. - Console.WriteLine("{0}({1}) already on file: {2}.", - applicant2.LastName, - applicant2.SSN, - Person.Equals(applicant2, candidate)); - } -} -// The example displays the following output: -// Found Jones (SSN 199-29-3999). -// Jones(199-29-3999) already on file: True. -// - -// This tests the handling of null values, but does not appear in the documentation. -// -//public class Example -//{ -// public static void Main() -// { -// var p1 = new Person("Joe", "613-24-0068"); -// Person p2 = null; -// -// Console.WriteLine(p1 == p2); -// } -//} diff --git a/snippets/fsharp/System/IEquatableT/Equals/Equals.fsproj b/snippets/fsharp/System/IEquatableT/Equals/Equals.fsproj new file mode 100644 index 00000000000..32c2318f221 --- /dev/null +++ b/snippets/fsharp/System/IEquatableT/Equals/Equals.fsproj @@ -0,0 +1,12 @@ + + + + Exe + net8.0 + + + + + + + diff --git a/snippets/fsharp/System/IEquatableT/Equals/EqualsEx2.fs b/snippets/fsharp/System/IEquatableT/Equals/EqualsEx2.fs deleted file mode 100644 index d78aba7c988..00000000000 --- a/snippets/fsharp/System/IEquatableT/Equals/EqualsEx2.fs +++ /dev/null @@ -1,65 +0,0 @@ -module EqualsEx2 - -// -open System - -type Person(lastName: string, ssn: string) = - member this.LastName = lastName - member this.SSN = ssn - - interface IEquatable with - member this.Equals(other: Person) = - other.SSN = this.SSN - - override this.Equals(obj: obj) = - match obj with - | :? Person as person -> (this :> IEquatable).Equals(person) - | _ -> false - - override this.GetHashCode() = - this.SSN.GetHashCode() - - static member (==) (person1: Person, person2: Person) = - person1.Equals(person2) - - static member (!=) (person1: Person, person2: Person) = - not (person1.Equals(person2)) -// -// Create a Person object for each job applicant. -let applicant1 = Person("Jones", "099-29-4999") -let applicant2 = Person("Jones", "199-29-3999") -let applicant3 = Person("Jones", "299-49-6999") - -// Add applicants to a List object. -let applicants = ResizeArray() -applicants.Add applicant1 -applicants.Add applicant2 -applicants.Add applicant3 - -// Create a Person object for the final candidate. -let candidate = Person("Jones", "199-29-3999") -if applicants.Contains candidate then - printfn $"Found {candidate.LastName} (SSN {candidate.SSN})." -else - printfn $"Applicant {candidate.SSN} not found." - -// Call the shared inherited Equals(Object, Object) method. -// It will in turn call the IEquatable.Equals implementation. -printfn $"{applicant2.LastName}({applicant2.SSN}) already on file: {Person.Equals(applicant2, candidate)}." - -// The example displays the following output: -// Found Jones (SSN 199-29-3999). -// Jones(199-29-3999) already on file: True. - -// This tests the handling of null values, but does not appear in the documentation. -// -//public class Example -//{ -// public static void Main() -// { -// var p1 = new Person("Joe", "613-24-0068") -// Person p2 = null -// -// Console.WriteLine(p1 == p2) -// } -//} \ No newline at end of file diff --git a/snippets/fsharp/System/IEquatableT/Equals/EqualsExample.fs b/snippets/fsharp/System/IEquatableT/Equals/EqualsExample.fs new file mode 100644 index 00000000000..9aff81cdf57 --- /dev/null +++ b/snippets/fsharp/System/IEquatableT/Equals/EqualsExample.fs @@ -0,0 +1,39 @@ + +// +open System + +type Person(lastName: string, nationalId: string) = + member this.LastName = lastName + member this.NationalId = nationalId + + interface IEquatable with + member this.Equals(other: Person) = + other.NationalId = this.NationalId + + override this.Equals(obj: obj) = + match obj with + | :? Person as person -> (this :> IEquatable).Equals(person) + | _ -> false + + override this.GetHashCode() = + this.NationalId.GetHashCode() + + static member (==) (person1: Person, person2: Person) = + person1.Equals(person2) + + static member (!=) (person1: Person, person2: Person) = + not (person1.Equals(person2)) +// + +// +let applicants = + [ Person("Jones", "099-29-4999") + Person("Jones", "199-29-3999") + Person("Jones", "299-49-6999") ] + +let candidate = Person("Jones", "199-29-3999") +let contains = List.contains candidate applicants +printfn "%s (%s) is on record: %b" candidate.LastName candidate.NationalId contains +// The example prints the following output: +// Jones (199-29-3999) is on record: true +// diff --git a/snippets/fsharp/System/IEquatableT/Equals/Snippet12.fs b/snippets/fsharp/System/IEquatableT/Equals/Snippet12.fs deleted file mode 100644 index ec85aff5712..00000000000 --- a/snippets/fsharp/System/IEquatableT/Equals/Snippet12.fs +++ /dev/null @@ -1,55 +0,0 @@ -open System -open System.Collections.Generic -open System.Text.RegularExpressions - -open System - -type Person(lastName: string, ssn: string) = - member this.LastName = lastName - member this.SSN = ssn - - interface IEquatable with - member this.Equals(other: Person) = - other.SSN = this.SSN - - override this.Equals(obj: obj) = - match obj with - | :? Person as person -> (this :> IEquatable).Equals(person) - | _ -> false - - override this.GetHashCode() = - this.SSN.GetHashCode() - - static member (==) (person1: Person, person2: Person) = - person1.Equals(person2) - - static member (!=) (person1: Person, person2: Person) = - not (person1.Equals(person2)) - -// -// Create a Person object for each job applicant. -let applicant1 = Person("Jones", "099-29-4999") -let applicant2 = Person("Jones", "199-29-3999") -let applicant3 = Person("Jones", "299-49-6999") - -// Add applicants to a List object. -let applicants = ResizeArray() -applicants.Add applicant1 -applicants.Add applicant2 -applicants.Add applicant3 - -// Create a Person object for the final candidate. -let candidate = Person("Jones", "199-29-3999") -if applicants.Contains candidate then - printfn $"Found {candidate.LastName} (SSN {candidate.SSN})." -else - printfn $"Applicant {candidate.SSN} not found." - -// Call the shared inherited Equals(Object, Object) method. -// It will in turn call the IEquatable.Equals implementation. -printfn $"{applicant2.LastName}({applicant2.SSN}) already on file: {Person.Equals(applicant2, candidate)}." - -// The example displays the following output: -// Found Jones (SSN 199-29-3999). -// Jones(199-29-3999) already on file: True. -// \ No newline at end of file diff --git a/snippets/fsharp/System/IEquatableT/Equals/fs.fsproj b/snippets/fsharp/System/IEquatableT/Equals/fs.fsproj deleted file mode 100644 index 34dea6b9fa0..00000000000 --- a/snippets/fsharp/System/IEquatableT/Equals/fs.fsproj +++ /dev/null @@ -1,11 +0,0 @@ - - - Exe - net6.0 - - - - - - - \ No newline at end of file diff --git a/snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/Equals.vbproj b/snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/Equals.vbproj new file mode 100644 index 00000000000..4855707fd9d --- /dev/null +++ b/snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/Equals.vbproj @@ -0,0 +1,9 @@ + + + + Exe + vb + net8.0 + + + diff --git a/snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/EqualsEx2.vb b/snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/EqualsEx2.vb deleted file mode 100644 index 3ce5cb29844..00000000000 --- a/snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/EqualsEx2.vb +++ /dev/null @@ -1,78 +0,0 @@ -' Visual Basic .NET Document -Option Strict On - -' -Public Class Person - Implements IEquatable(Of Person) - - Public Sub New(ByVal lastName As String, ByVal ssn As String) - Me.LastName = lastName - Me.SSN = ssn - End Sub - - Public ReadOnly Property LastName As String - Public ReadOnly Property SSN As String - - Public Overloads Function Equals(ByVal other As Person) As Boolean Implements IEquatable(Of Person).Equals - Return other IsNot Nothing AndAlso other.SSN = Me.SSN - End Function - - Public Overrides Function Equals(ByVal obj As Object) As Boolean - Return Equals(TryCast(obj, Person)) - End Function - - Public Overrides Function GetHashCode() As Integer - Return SSN.GetHashCode() - End Function - - Public Shared Operator =(ByVal person1 As Person, ByVal person2 As Person) As Boolean - If person1 Is Nothing Then - Return person2 Is Nothing - End If - Return person1.Equals(person2) - End Operator - - Public Shared Operator <>(ByVal person1 As Person, ByVal person2 As Person) As Boolean - If person1 Is Nothing Then - Return person2 IsNot Nothing - End If - Return Not person1.Equals(person2) - End Operator -End Class -' - -Module TestIEquatable - Public Sub Main() - ' Create a Person object for each job applicant. - Dim applicant1 As New Person("Jones", "099-29-4999") - Dim applicant2 As New Person("Jones", "199-29-3999") - Dim applicant3 As New Person("Jones", "299-49-6999") - - ' Add applicants to a List object. - Dim applicants As New List(Of Person) - applicants.Add(applicant1) - applicants.Add(applicant2) - applicants.Add(applicant3) - - ' Create a Person object for the final candidate. - Dim candidate As New Person("Jones", "199-29-3999") - - If applicants.Contains(candidate) Then - Console.WriteLine("Found {0} (SSN {1}).", _ - candidate.LastName, candidate.SSN) - Else - Console.WriteLine("Applicant {0} not found.", candidate.SSN) - End If - - ' Call the shared inherited Equals(Object, Object) method. - ' It will in turn call the IEquatable(Of T).Equals implementation. - Console.WriteLine("{0}({1}) already on file: {2}.", _ - applicant2.LastName, _ - applicant2.SSN, _ - Person.Equals(applicant2, candidate)) - End Sub -End Module -' The example displays the following output: -' Found Jones (SSN 199-29-3999). -' Jones(199-29-3999) already on file: True. - diff --git a/snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/EqualsExample.vb b/snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/EqualsExample.vb new file mode 100644 index 00000000000..73cb24e18b8 --- /dev/null +++ b/snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/EqualsExample.vb @@ -0,0 +1,61 @@ +' Visual Basic .NET Document +Option Strict On + +' +Public Class Person + Implements IEquatable(Of Person) + + Public Sub New(lastName As String, nationalId As String) + Me.LastName = lastName + Me.NationalId = nationalId + End Sub + + Public ReadOnly Property LastName As String + Public ReadOnly Property NationalId As String + + Public Overloads Function Equals(other As Person) As Boolean Implements IEquatable(Of Person).Equals + Return other IsNot Nothing AndAlso other.NationalId = Me.NationalId + End Function + + Public Overrides Function Equals(obj As Object) As Boolean + Return Equals(TryCast(obj, Person)) + End Function + + Public Overrides Function GetHashCode() As Integer + Return NationalId.GetHashCode() + End Function + + Public Shared Operator =(person1 As Person, person2 As Person) As Boolean + If person1 Is Nothing Then + Return person2 Is Nothing + End If + Return person1.Equals(person2) + End Operator + + Public Shared Operator <>(person1 As Person, person2 As Person) As Boolean + If person1 Is Nothing Then + Return person2 IsNot Nothing + End If + Return Not person1.Equals(person2) + End Operator +End Class +' + +Module EqualsExample + Public Sub Main() + ' + Dim applicants As New List(Of Person) + applicants.Add(New Person("Jones", "099-29-4999")) + applicants.Add(New Person("Jones", "199-29-3999")) + applicants.Add(New Person("Jones", "299-49-6999")) + + ' Create a Person object for the final candidate. + Dim candidate As New Person("Jones", "199-29-3999") + Dim contains As Boolean = applicants.Contains(candidate) + Console.WriteLine($"{candidate.LastName} ({candidate.NationalId}) is on record: {contains}") + ' The example prints the following output: + ' Jones (199-29-3999) Is on record True + ' + End Sub +End Module + diff --git a/snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/Snippet12.vb b/snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/Snippet12.vb deleted file mode 100644 index e97732d8bfd..00000000000 --- a/snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/Snippet12.vb +++ /dev/null @@ -1,81 +0,0 @@ -' Visual Basic .NET Document -Option Strict On - -Imports System.Collections.Generic -Imports System.Text.RegularExpressions - -Public Class Person - Implements IEquatable(Of Person) - - Public Sub New(ByVal lastName As String, ByVal ssn As String) - Me.LastName = lastName - Me.SSN = ssn - End Sub - - Public ReadOnly Property LastName As String - Public ReadOnly Property SSN As String - - Public Overloads Function Equals(ByVal other As Person) As Boolean Implements IEquatable(Of Person).Equals - Return other IsNot Nothing AndAlso other.SSN = Me.SSN - End Function - - Public Overrides Function Equals(ByVal obj As Object) As Boolean - Return Equals(TryCast(obj, Person)) - End Function - - Public Overrides Function GetHashCode() As Integer - Return SSN.GetHashCode() - End Function - - Public Shared Operator =(ByVal person1 As Person, ByVal person2 As Person) As Boolean - If person1 Is Nothing Then - Return person2 Is Nothing - End If - Return person1.Equals(person2) - End Operator - - Public Shared Operator <>(ByVal person1 As Person, ByVal person2 As Person) As Boolean - If person1 Is Nothing Then - Return person2 IsNot Nothing - End If - Return Not person1.Equals(person2) - End Operator -End Class - -' -Module TestIEquatable - Public Sub Main() - ' Create a Person object for each job applicant. - Dim applicant1 As New Person("Jones", "099-29-4999") - Dim applicant2 As New Person("Jones", "199-29-3999") - Dim applicant3 As New Person("Jones", "299-49-6999") - - ' Add applicants to a List object. - Dim applicants As New List(Of Person) - applicants.Add(applicant1) - applicants.Add(applicant2) - applicants.Add(applicant3) - - ' Create a Person object for the final candidate. - Dim candidate As New Person("Jones", "199-29-3999") - - If applicants.Contains(candidate) Then - Console.WriteLine("Found {0} (SSN {1}).", _ - candidate.LastName, candidate.SSN) - Else - Console.WriteLine("Applicant {0} not found.", candidate.SSN) - End If - - ' Call the shared inherited Equals(Object, Object) method. - ' It will in turn call the IEquatable(Of T).Equals implementation. - Console.WriteLine("{0}({1}) already on file: {2}.", _ - applicant2.LastName, _ - applicant2.SSN, _ - Person.Equals(applicant2, candidate)) - End Sub -End Module -' The example displays the following output: -' Found Jones (SSN 199-29-3999). -' Jones(199-29-3999) already on file: True. -' - diff --git a/xml/System/IEquatable`1.xml b/xml/System/IEquatable`1.xml index 67915f777b9..2a5adb854fd 100644 --- a/xml/System/IEquatable`1.xml +++ b/xml/System/IEquatable`1.xml @@ -64,12 +64,12 @@ See remarks on the for more information about implementing the interface. ## Examples - The following example shows the partial implementation of a `Person` class that implements and has two properties, `LastName` and `SSN`. `SSN` is considered to be a unique identifier, therefore the method returns `True` if the `SSN` property of two `Person` objects is identical; otherwise, it returns `False`. + The following example shows the partial implementation of a `Person` class that implements and has two properties, `LastName` and `NationalId`. `NationalId` is considered to be a unique identifier, therefore the method returns `True` if the `NationalId` property of two `Person` objects is identical; otherwise, it returns `False`. Note that the F# example does not handle `null` values for `Person` instances. - :::code language="csharp" source="~/snippets/csharp/System/IEquatableT/Equals/EqualsEx2.cs" id="Snippet3"::: - :::code language="fsharp" source="~/snippets/fsharp/System/IEquatableT/Equals/EqualsEx2.fs" id="Snippet3"::: - :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/EqualsEx2.vb" id="Snippet3"::: + :::code language="csharp" source="~/snippets/csharp/System/IEquatableT/Equals/EqualsExample.cs" id="Person"::: + :::code language="fsharp" source="~/snippets/fsharp/System/IEquatableT/Equals/EqualsExample.fs" id="Person"::: + :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/EqualsExample.vb" id="Person"::: ]]> @@ -168,18 +168,18 @@ ## Examples - The following example shows the partial implementation of a `Person` class that implements and has two properties, `LastName` and `SSN`. `SSN` is considered to be a unique identifier, therefore the method returns `True` if the `SSN` property of two `Person` objects is identical; otherwise, it returns `False`. + The following example shows the partial implementation of a `Person` class that implements and has two properties, `LastName` and `NationalId`. `NationalId` is considered to be a unique identifier, therefore the method returns `True` if the `NationalId` property of two `Person` objects is identical; otherwise, it returns `False`. Note that the F# example does not handle `null` values for `Person` instances. - :::code language="csharp" source="~/snippets/csharp/System/IEquatableT/Equals/EqualsEx2.cs" id="Snippet3"::: - :::code language="fsharp" source="~/snippets/fsharp/System/IEquatableT/Equals/EqualsEx2.fs" id="Snippet3"::: - :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/EqualsEx2.vb" id="Snippet3"::: + :::code language="csharp" source="~/snippets/csharp/System/IEquatableT/Equals/EqualsExample.cs" id="Person"::: + :::code language="fsharp" source="~/snippets/fsharp/System/IEquatableT/Equals/EqualsExample.fs" id="Person"::: + :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/EqualsExample.vb" id="Person"::: - `Person` objects can then be stored in a object and can be identified by the `Contains` method, as the following example shows. + When a `Person` is stored in a , `Contains` will use its' implementation to find a match. - :::code language="csharp" source="~/snippets/csharp/System/IEquatableT/Equals/Snippet12.cs" id="Snippet12"::: - :::code language="fsharp" source="~/snippets/fsharp/System/IEquatableT/Equals/Snippet12.fs" id="Snippet12"::: - :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/Snippet12.vb" id="Snippet12"::: + :::code language="csharp" source="~/snippets/csharp/System/IEquatableT/Equals/EqualsExample.cs" id="PersonSample"::: + :::code language="fsharp" source="~/snippets/fsharp/System/IEquatableT/Equals/EqualsExample.fs" id="PersonSample"::: + :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/EqualsExample.vb" id="PersonSample"::: ]]> From d042eeb4213dd5133f3cdd08cae421eea36b594a Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sat, 15 Mar 2025 15:52:35 +0100 Subject: [PATCH 3/3] Apply suggestions from code review Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com> --- xml/System/IEquatable`1.xml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/xml/System/IEquatable`1.xml b/xml/System/IEquatable`1.xml index 2a5adb854fd..47967e257e3 100644 --- a/xml/System/IEquatable`1.xml +++ b/xml/System/IEquatable`1.xml @@ -61,11 +61,11 @@ The interface is used by generic collection objects such as , , and when testing for equality in such methods as `Contains`, `IndexOf`, `LastIndexOf`, and `Remove`. It should be implemented for any object that might be stored in a generic collection. - See remarks on the for more information about implementing the interface. +For more information about implementing the interface, see remarks on the method. ## Examples The following example shows the partial implementation of a `Person` class that implements and has two properties, `LastName` and `NationalId`. `NationalId` is considered to be a unique identifier, therefore the method returns `True` if the `NationalId` property of two `Person` objects is identical; otherwise, it returns `False`. - Note that the F# example does not handle `null` values for `Person` instances. + (Note that the F# example does not handle `null` values for `Person` instances.) :::code language="csharp" source="~/snippets/csharp/System/IEquatableT/Equals/EqualsExample.cs" id="Person"::: :::code language="fsharp" source="~/snippets/fsharp/System/IEquatableT/Equals/EqualsExample.fs" id="Person"::: @@ -169,13 +169,13 @@ ## Examples The following example shows the partial implementation of a `Person` class that implements and has two properties, `LastName` and `NationalId`. `NationalId` is considered to be a unique identifier, therefore the method returns `True` if the `NationalId` property of two `Person` objects is identical; otherwise, it returns `False`. - Note that the F# example does not handle `null` values for `Person` instances. + (Note that the F# example does not handle `null` values for `Person` instances.) :::code language="csharp" source="~/snippets/csharp/System/IEquatableT/Equals/EqualsExample.cs" id="Person"::: :::code language="fsharp" source="~/snippets/fsharp/System/IEquatableT/Equals/EqualsExample.fs" id="Person"::: :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.GenericIEquatable.Equals/vb/EqualsExample.vb" id="Person"::: - When a `Person` is stored in a , `Contains` will use its' implementation to find a match. + When a `Person` is stored in a , `Contains` uses its implementation to search for a match. :::code language="csharp" source="~/snippets/csharp/System/IEquatableT/Equals/EqualsExample.cs" id="PersonSample"::: :::code language="fsharp" source="~/snippets/fsharp/System/IEquatableT/Equals/EqualsExample.fs" id="PersonSample":::