Created
June 14, 2018 22:38
-
-
Save agocke/6ba6e64f77f1212ba7292bfd1f1000e5 to your computer and use it in GitHub Desktop.
C# data class example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
public class LoginResource : IEquatable<LoginResource> | |
{ | |
public string Username { get; set; } | |
public string Password { get; set; } | |
public bool RememberMe { get; set; } = false; | |
public override bool Equals(object obj) | |
=> obj is LoginResource resource && Equals(resource); | |
public bool Equals(LoginResource other) | |
{ | |
return other != null && | |
Username == other.Username && | |
Password == other.Password && | |
RememberMe == other.RememberMe; | |
} | |
public override int GetHashCode() | |
{ | |
var hashCode = -736459255; | |
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(Username); | |
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(Password); | |
hashCode = hashCode * -1521134295 + RememberMe.GetHashCode(); | |
return hashCode; | |
} | |
public override string ToString() | |
{ | |
return $"{{{nameof(Username)} = {Username}, {nameof(Password)} = {Password}, {nameof(RememberMe)} = {RememberMe}}}"; | |
} | |
public static bool operator ==(LoginResource resource1, LoginResource resource2) | |
{ | |
return EqualityComparer<LoginResource>.Default.Equals(resource1, resource2); | |
} | |
public static bool operator !=(LoginResource resource1, LoginResource resource2) | |
{ | |
return !(resource1 == resource2); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment