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<string, int, int, int> _impl;
public Key(string unitID, int address, int comPort, int id)
{
_impl = new Tuple<string, int, int, int>(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<Key, MyClass> mapping = new Dictionary<Key, MyClass>();
Instead of:
Dictionary<Tuple<string, int, int, int>, MyClass> mapping = new Dictionary<Tuple<string, int, int, int>, MyClass>();
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 ?