I’m sure this is something simple, but I’m getting a compiler error when using the .Create() method of the new Tuple class.
Dictionary<long, Tuple<long, long>> test = new Dictionary<long, Tuple<long, long>>();
test.Add(1, Tuple.Create(1, 2)); // <-- compiler error
The error is that Tuple.Create has invalid arguments, as it is expecting longs, and the numbers I’ve put in are being considered as integers.
In this case yes they are within the range of int, but they may may sometimes have a value too big for an int.
I could cast (long)1 to get the code to compile, but this doesn’t seem right – what am I missing?
Your code is equivalent:
But, because
1can be eitherintorlong, so by default, compiler will returnTuple<int, int>when calling:That is why you get compiler error. Therefore, you need to make casting explicitly by using generic version: