Skip to content

Instantly share code, notes, and snippets.

@airbreather
Created May 19, 2017 21:58
Show Gist options
  • Save airbreather/51afdbbf80cb5ba5d9f73976a48f1890 to your computer and use it in GitHub Desktop.
Save airbreather/51afdbbf80cb5ba5d9f73976a48f1890 to your computer and use it in GitHub Desktop.
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