Last active
August 29, 2015 14:25
-
-
Save rexcfnghk/c6db9c774371faba7e57 to your computer and use it in GitHub Desktop.
KeyValuePair implementation using C♯ 6 features
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; | |
public struct KeyValuePair<TKey, TValue> : IEquatable<KeyValuePair<TKey, TValue>> | |
{ | |
public KeyValuePair(TKey key, TValue value) | |
{ | |
Key = key; | |
Value = value; | |
} | |
public TKey Key { get; } | |
public TValue Value { get; } | |
public static bool operator ==(KeyValuePair<TKey, TValue> left, KeyValuePair<TKey, TValue> right) => Equals(left, right); | |
public static bool operator !=(KeyValuePair<TKey, TValue> left, KeyValuePair<TKey, TValue> right) => !Equals(left, right); | |
public override bool Equals(object obj) => obj is KeyValuePair<TKey, TValue> && Equals((KeyValuePair<TKey, TValue>)obj); | |
public bool Equals(KeyValuePair<TKey, TValue> other) => Equals(Key, other.Key) && Equals(Value, other.Value); | |
public override int GetHashCode() | |
{ | |
unchecked | |
{ | |
int hash = 13; | |
hash = (hash * 7) + Key?.GetHashCode() ?? 0; | |
hash = (hash * 7) + Value?.GetHashCode() ?? 0; | |
return hash; | |
} | |
} | |
public override string ToString() => $"[{Key?.ToString()}, {Value?.ToString()}]"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment