等效于C#中的java.lang.Number

时间:2019-02-03 15:26:26

标签: c#

我想用数字作为值在字典之间进行算术运算。 这是我的代码:

public class DictionaryOperation {

    public static Dictionary<TKey, double> Add<TKey>(Dictionary<TKey, double> d1, Dictionary<TKey, double> d2) {
        Dictionary<TKey, double> result = new Dictionary<TKey, double>();
        foreach (TKey key in d1.Keys) {
            if (d2.ContainsKey(key))
                result[key] = d1[key] + d2[key];
            else
                result[key] = d1[key];
        }
        foreach (TKey key in d2.Keys) {
            if (!result.ContainsKey(key))
                result[key] = d2[key];
        }
        return result;
    }
}

我想知道是否只能为任何数字类型(int浮点数,十进制,...)创建一个方法,还是我必须为每个数字类型创建一个方法,这意味着在其中将有相同的代码每种方法。

我希望能够做到这一点:

Dictionary<string, int> ints = DictionaryOperation.Add(new Dictionary<string, int>(), new Dictionary<string, int>());
Dictionary<string, float> floats = DictionaryOperation.Add(new Dictionary<string, float>(), new Dictionary<string, float>());

1 个答案:

答案 0 :(得分:2)

可以避免使用泛型为每种数字类型编写相同的方法。您的词典中已经有一个通用密钥。唯一缺少的是通用值。更改您的方法以使用通用字典值:

public static Dictionary<TKey, TValue> Add<TKey, TValue>(Dictionary<TKey, TValue> d1, Dictionary<TKey, TValue> d2) 
    where TValue : IComparable

问题是,没有类型约束只允许数字(或可以用+运算符添加的对象)。我在上面的行中使用了IComparable,因为所有数字类型都是可比较的。

下一个问题是,尝试使用IComparable运算符时+无济于事。为此,您可以像这样使用动力学:

dynamic a = d1[key];
dynamic b = d2[key];
result[key] = a + b;

现在,您可以对实现IComparable的所有类型使用该方法。但是您没有编译时的安全性。这意味着您将获得所有未实现+运算符的类型的运行时错误。

此问题已在此处描述: C# Adding two Generic Values

这里是完整的方法:

public static Dictionary<TKey, TValue> Add<TKey, TValue>(Dictionary<TKey, TValue> d1, Dictionary<TKey, TValue> d2) 
        where TValue : IComparable
    {
        Dictionary<TKey, TValue> result = new Dictionary<TKey, TValue>();
    foreach (TKey key in d1.Keys) {
        if (d2.ContainsKey(key))
        {
            dynamic a = d1[key];
            dynamic b = d2[key];
            result[key] = a + b;
        }
        else
            result[key] = d1[key];
    }
    foreach (TKey key in d2.Keys) {
        if (!result.ContainsKey(key))
            result[key] = d2[key];
    }
    return result;
}
相关问题