chore: Define common IUniqueID member impls in interface
Taking advantage of nifty new language feats
This commit is contained in:
1 parent
5180d13fb0
commit
c5b79b3dae
2 files changed
+15
-32
No files matched your search
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user