Skip to content

Commit

Permalink
Fix sendgrid#425: Implement IEquatable for EmailAddress
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevek committed Oct 3, 2017
1 parent 88a8a90 commit 70ce26d
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/SendGrid/Helpers/Mail/Model/EmailAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// </copyright>

using System;


namespace SendGrid.Helpers.Mail
{
using Newtonsoft.Json;
Expand All @@ -11,7 +14,7 @@ namespace SendGrid.Helpers.Mail
/// An email object containing the email address and name of the sender or recipient.
/// </summary>
[JsonObject(IsReference = false)]
public class EmailAddress
public class EmailAddress : IEquatable<EmailAddress>
{
/// <summary>
/// Initializes a new instance of the <see cref="EmailAddress"/> class.
Expand Down Expand Up @@ -42,5 +45,23 @@ public EmailAddress(string email, string name = null)
/// </summary>
[JsonProperty(PropertyName = "email")]
public string Email { get; set; }

public bool Equals(EmailAddress other) {
if(ReferenceEquals(null, other)) return false;
if(ReferenceEquals(this, other)) return true;
if(Email==null || other.Email==null) return false;
return string.Equals(Email.ToLowerInvariant(), other.Email.ToLowerInvariant());
}

public override bool Equals(object obj) {
if(ReferenceEquals(null, obj)) return false;
if(ReferenceEquals(this, obj)) return true;
if(obj.GetType()!=GetType()) return false;
return Equals((EmailAddress)obj);
}

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

0 comments on commit 70ce26d

Please sign in to comment.