Skip to content

Instantly share code, notes, and snippets.

@HaloFour
Last active June 28, 2018 10:36
Show Gist options
  • Save HaloFour/4ca895d3f4356dafdc19cfdde20b7c41 to your computer and use it in GitHub Desktop.
Save HaloFour/4ca895d3f4356dafdc19cfdde20b7c41 to your computer and use it in GitHub Desktop.
Data Classes with Primary Constructor
using System;
using System.Collections.Generic;
/*
* public data class LoginResource(string Username, string Password)
* {
* public bool RememberMe { get; } = true;
* }
*/
public sealed class LoginResource : IEquatable<LoginResource> {
public struct Builder {
public string Username;
public string Password;
public bool RememberMe;
}
public static void Init(ref Builder builder, string Username, string Password) {
builder.Username = Username;
builder.Password = Password;
builder.RememberMe = true;
}
private readonly Builder _builder;
public LoginResource(Builder builder) => _builder = builder;
public LoginResource(string Username, string Password) => Init(ref _builder, Username, Password);
public string Username => _builder.Username;
public string Password => _builder.Password;
public bool RememberMe => _builder.RememberMe;
public override bool Equals(object obj)
{
return Equals(obj as LoginResource);
}
public bool Equals(LoginResource that)
{
if (that is null) return false;
var eq1 = EqualityComparer<string>.Default;
return eq1.Equals(this.Username, that.Username)
&& eq1.Equals(this.Password, that.Password)
&& this.RememberMe == that.RememberMe;
}
public override int GetHashCode()
{
var eq1 = EqualityComparer<string>.Default;
var hashCode = -736459255;
hashCode = hashCode * -1521134295 + eq1.GetHashCode(Username);
hashCode = hashCode * -1521134295 + eq1.GetHashCode(Password);
hashCode = hashCode * -1521134295 + RememberMe.GetHashCode();
return hashCode;
}
public override string ToString()
{
return $"{{{nameof(Username)} = {Username}, {nameof(Password)} = {Password}, {nameof(RememberMe)} = {RememberMe}}}";
}
public void Deconstruct(out string Username, out string Password)
{
Username = this.Username;
Password = this.Password;
}
public static bool operator ==(LoginResource x, LoginResource y) {
return x is LoginResource @this && @this.Equals(x);
}
public static bool operator !=(LoginResource x, LoginResource y) {
return !(x == y);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment