chore: Define common IUniqueID member impls in interface

Taking advantage of nifty new language feats
This commit is contained in:
John Colvin committed 2023-08-22 11:35:41 -04:00
1 parent 5180d13fb0
commit c5b79b3dae
2 files changed
+15 -32

No files matched your search

+12 -10
View File
@@ -1,13 +1,15 @@
using System;
using System.Collections.Generic;
using System.Reflection;
namespace bts.websocket_core {
// Represents a unique client, a little redundant
public interface IUniqueID {
public string ID { get; }
public int GetHashCode();
public string ToString();
public bool Equals(object? obj);
public interface IUniqueID : IEquatable<object?> {
string ID { get; }
public int GetHashCode() =>
ID.GetHashCode();
public string? ToString() =>
ID;
bool IEquatable<object?>.Equals(object? obj) =>
obj is IUniqueID other && ID == other.ID;
}
}
}
+3 -22
View File
@@ -1,25 +1,6 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Reflection;
namespace bts.websocket_core {
// Represents a unique client, a little redundant
public class UniqueID : IUniqueID {
public string ID { get; }
public UniqueID(string id) {
ID = id;
}
public override int GetHashCode() {
return this.ID.GetHashCode();
}
public override string ToString() {
return this.ID.ToString() ?? "";
}
public override bool Equals(object? obj) {
var other = obj == null ? null : obj as IUniqueID;
return other != null && (string)other.ID == ID;
}
public class UniqueID(string id) : IUniqueID {
public string ID { get; } = id;
}
}
}