I have a dictionary that takes in a tuple function and an int
Dictionary<Tuple<string,string>, int> fullNames = new Dictionary<Tuple<string,string>, int>();
Where the Tuple class is defined as
public class Tuple<T, T2>
{
public Tuple(T first, T2 second)
{
First = first;
Second = second;
}
public T First { get; set; }
public T2 Second { get; set; }
}
I want to use the Containskey function as such
if (fullNames.ContainsKey(Tuple<"firstname","lastname">))
But I am getting an overload error. Any suggestions?
The code you have provided is invalid, since you’re trying to provide a type definition in the place where an actual object should be (and the type definition is invalid too, since a string is not actually the type
System.Stringthat a generic expects). If the tuple is the key value for the dictionary, then you can do this:But then you can run afoul of the reference equality issue since two tuples created in memory with the same properties are not necesarily the same object. It would be better to do this:
That will tell you if a key exists that shares the same property data. Then accessing that element is going to be just as complicated.
EDIT: Long story short, if you plan on using a reference type for your key, you need to make sure your object implements
EqualsandGetHashCodein a way to properly identify two separate in memory instances are the same.