Sobes.tech
Junior — Senior

Dictionary behavior when using a custom key type

livecode

Task condition

What action does this program perform and what output will appear on the console?

using static System.Console;

try
{
    var dictionary = new Dictionary<Key, int>();

    var firstKey = new Key(5);
    dictionary[firstKey] = 42;

    var secondKey = new Key(5);
    WriteLine($"M0: {dictionary[secondKey]}");
}
catch { }

sealed class Key
    {
        public long InnerKey {get;}

        public Key(long value)
        {
            InnerKey = value;
        }

        public override int GetHashCode()
            => HashCode.Combine(InnerKey);
    }