Created
May 19, 2017 21:58
-
-
Save airbreather/51afdbbf80cb5ba5d9f73976a48f1890 to your computer and use it in GitHub Desktop.
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.Runtime.InteropServices; | |
namespace WeirdStuff | |
{ | |
[StructLayout(LayoutKind.Auto)] | |
public struct NullableDictKey<T> where T : struct | |
{ | |
private T? value; | |
public NullableDictKey(T? value) => this.value = value; | |
public static implicit operator T?(NullableDictKey<T> value) => value.value; | |
public static explicit operator T(NullableDictKey<T> value) => (T)value.value; | |
public static implicit operator NullableDictKey<T>(T? value) => new NullableDictKey<T>(value); | |
public T GetValueOrDefault() => this.value.GetValueOrDefault(); | |
public T GetValueOrDefault(T defaultValue) => this.value.GetValueOrDefault(defaultValue); | |
public override int GetHashCode() => this.value.GetHashCode(); | |
public override bool Equals(object obj) => this.value.HasValue | |
? this.value.Equals(obj) | |
: obj == null || obj is NullableDictKey<T> ndk && !ndk.value.HasValue; | |
public override string ToString() => this.value.ToString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment