Last active
August 18, 2020 03:30
-
-
Save sakapon/ed8320839f2543d21e9f793afd215e3f to your computer and use it in GitHub Desktop.
OperatorsSample/Title class
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; | |
namespace OperatorsLib.Classes | |
{ | |
public class Title : IEquatable<Title>, IComparable<Title> | |
{ | |
public string Name { get; } | |
public int Number { get; } | |
public Title(string name, int number) => (Name, Number) = (name, number); | |
public override string ToString() => $"{Name} #{Number}"; | |
#region Equality Operators | |
// other != null では無限ループ。 | |
public bool Equals(Title other) => !(other is null) && Name == other.Name && Number == other.Number; | |
// 参照型の場合は null 値があるため、静的メソッドが実装されることも多いです。 | |
public static bool Equals(Title v1, Title v2) => v1?.Equals(v2) ?? (v2 is null); | |
public static bool operator ==(Title v1, Title v2) => Equals(v1, v2); | |
public static bool operator !=(Title v1, Title v2) => !Equals(v1, v2); | |
public override bool Equals(object obj) => Equals(obj as Title); | |
public override int GetHashCode() => HashCode.Combine(Name, Number); | |
#endregion | |
#region Comparison Operators | |
public int CompareTo(Title other) | |
{ | |
if (other is null) return 1; | |
var c1 = string.Compare(Name, other.Name); | |
if (c1 != 0) return c1; | |
return Number.CompareTo(other.Number); | |
} | |
// 参照型の場合は null 値があるため、静的メソッドが実装されることも多いです。 | |
public static int Compare(Title v1, Title v2) => v1?.CompareTo(v2) ?? (v2 is null ? 0 : -1); | |
public static bool operator <(Title v1, Title v2) => Compare(v1, v2) < 0; | |
public static bool operator >(Title v1, Title v2) => Compare(v1, v2) > 0; | |
public static bool operator <=(Title v1, Title v2) => Compare(v1, v2) <= 0; | |
public static bool operator >=(Title v1, Title v2) => Compare(v1, v2) >= 0; | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment