简体   繁体   English

相当于 C# 中的 java.lang.Number

[英]Equivalent of java.lang.Number in C#

I want to make arithmetic operations between dictionaries with numbers as value.我想在以数字为值的字典之间进行算术运算。 This is my code:这是我的代码:

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;
    }
}

I would like to know if I can create only one method for any numeric type (int float, decimal, ...) or do I have to create one method per numeric type, which means there will be the same code in each methods.我想知道我是否只能为任何数字类型(int float、decimal 等)创建一种方法,还是必须为每种数字类型创建一种方法,这意味着每种方法中都有相同的代码。

I want to be able to do this:我希望能够做到这一点:

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>());

You can avoid writing the same Method for every numeric type by using generics.通过使用泛型,您可以避免为每个数字类型编写相同的方法。 You already have a generic key in your dictionary.您的字典中已经有一个通用键。 The only thing missing is the generic value.唯一缺少的是通用值。 Change your method to use a generic dictionary value:更改您的方法以使用通用字典值:

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

The problem is, that there is no type constraint that allows only numbers (or objects that can be added with the + operator).问题是,没有只允许数字(或可以使用 + 运算符添加的对象)的类型约束。 I used IComparable in the line above because all numeric types are comparable.我在上面的行中使用了IComparable ,因为所有数字类型都是可比较的。

The next problem is, the IComparable does not help when trying to use the + operator.下一个问题是, IComparable在尝试使用+运算符时没有帮助。 For this you can use dynamics like so:为此,您可以像这样使用动态:

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

Now you can use the method for all types that implement IComparable.现在您可以将该方法用于实现 IComparable 的所有类型。 BUT you have no compile time safety.但是你没有编译时安全。 That means you will get runtime errors for all types that do not implement the + operator.这意味着您将收到所有未实现+运算符的类型的运行时错误。

This problem is already described here: C# Adding two Generic Values这个问题已经在这里描述: C#添加两个通用值

Here the full method:这里是完整的方法:

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM