C#用带有值的元组实例化字典

Awe*_*Guy 2 c# dictionary tuples

我有一个包含int和元组的dictonaty.

我想在实例化时插入一些值,但是我收到一个错误:

无法从'System.Tuple'转换为'System.Collections.Generic.IEqualtyComparer

这就是我想要做的:

public static readonly Dictionary<int, Tuple<int, double>> DIAMETER_METRIC_CHAMFER 
        = new Dictionary<int, Tuple<int, double>>({80, new Tuple<int, double>(16, 37.0)}
);
Run Code Online (Sandbox Code Playgroud)

Dar*_*dan 7

你已经将元素传递{80, new Tuple<int, double>(16, 37.0)}给了字典的构造函数,你应该使用初始化器:

public static readonly Dictionary<int, Tuple<int, double>> DIAMETER_METRIC_CHAMFER
        = new Dictionary<int, Tuple<int, double>>() 
            { 
                { 80, new Tuple<int, double>(16, 37.0) } 
            };
Run Code Online (Sandbox Code Playgroud)