-
Notifications
You must be signed in to change notification settings - Fork 6
/
PiiString.cs
46 lines (35 loc) · 1.31 KB
/
PiiString.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System;
namespace Gaev.Blog.Examples;
public class PiiString
{
private readonly string _string;
public PiiString(string underlyingString)
=> _string = underlyingString ?? throw new ArgumentNullException(nameof(underlyingString));
public override string ToString()
=> _string;
public override int GetHashCode()
=> _string.GetHashCode();
public override bool Equals(object obj)
=> obj switch
{
PiiString other => AreEqual(this, other),
string other => AreEqual(this, new PiiString(other)),
_ => false
};
public static bool operator ==(PiiString a, PiiString b)
=> AreEqual(a, b);
public static bool operator !=(PiiString a, PiiString b)
=> !AreEqual(a, b);
public static implicit operator string(PiiString piiString)
=> piiString?._string;
public static implicit operator PiiString(string underlyingString)
=> underlyingString == null ? null : new PiiString(underlyingString);
private static bool AreEqual(PiiString a, PiiString b) =>
(a, b) switch
{
(null, null) => true,
(null, _) => false,
(_, null) => false,
(_, _) => a._string.Equals(b._string)
};
}