Wrapping tuples to be used as keys in a Dictionary I'm currently working on a project where I have to map some objects to several properties.

Basically I need a Dictionary with Tuples as keys and instances of MyClass as values.

As using tuples can quickly lead to an obscure (because of item1, item2... properties) and verbose code I decided to make a class to wrap the tuples.

class Key
{
    private Tuple&lt;string, int, int, int&gt; _impl;

    public Key(string unitID, int address, int comPort, int id)
    {
        _impl = new Tuple&lt;string, int, int, int&gt;(unitID, address, comPort, id);
    }

    public override int GetHashCode()
    {
        return _impl.GetHashCode();
    }

    public override bool Equals(object obj)
    {
        return _impl.Equals(obj);
    }

    public override string ToString()
    {
        return _impl.ToString();
    }
}


That way I should take advantage of the Tuple class and avoid its inconvenients.

So I can write:

Dictionary&lt;Key, MyClass&gt; mapping = new Dictionary&lt;Key, MyClass&gt;();


Instead of:

Dictionary&lt;Tuple&lt;string, int, int, int&gt;, MyClass&gt; mapping = new Dictionary&lt;Tuple&lt;string, int, int, int&gt;, MyClass&gt;();


And look up the dictionary like this:

Key key = new Key("abc", 1, 5, 5464);
MyClass myInstance = mapping[key];


Is this implementation correct ? Is there a more proper way to achieve this ?
public Key(string unitID, int address, int comPort, int id)



Clearly this class represents something much more specialized than an all-purpose "key". Name the type for what it stands for!


private Tuple&lt;string, int, int, int&gt; _impl;



The private field should be readonly. But why don't you have this instead?

private readonly string _unitId;
private readonly int _address;
private readonly int _comPort;
private readonly int _id;



public override int GetHashCode() 
{
    return _impl.GetHashCode(); 
}



Ideally your GetHashCode implementation should rely on immutable fields. Your _impl field isn't marked readonly, which doesn't quite communicate that intent. Using actual fields and dropping the Tuple altogether, you can come up with a more standard GetHashCode implementation.


  That way I should take advantage of the Tuple class and avoid its inconvenients.


You're not getting anything out the Tuple. Get rid of it, it's only obfuscating the class' implementation.



Also, you're not instantiating your Tuple the idiomatic way. This:


_impl = new Tuple&lt;string, int, int, int&gt;(unitID, address, comPort, id);



Should be written like this and leverage generic type parameter inference:

_impl = Tuple.Create(unitID, address, comPort, id);