我在Dictionary <string,int>上定义/运算符的尝试有什么问题?

时间:2015-11-16 14:10:51

标签: c# .net algorithm data-structures

我正在尝试创建一个能够获得两个Dictionary<string,int> s差异的函数,例如

{ "SomeKey" -> 4 , "SomeOtherKey" -> 2 } / { "SomeKey" -> 1, "Keykeykey" -> 69 }
= { "SomeKey -> 3" , "SomeOtherKey" -> 2 }

我的程序是

    public static Dictionary<string, int> operator / ( Dictionary<string, int> first, Dictionary<string, int> second )
    {
        // Returns all elements in first but not second, where the number in the first but not second
        // for a given key is the first's value minus the second's value
        Dictionary<string, int> firstNotSecond = new Dictionary<string, int>();
        foreach ( KeyValuePair<string, int> pair in first )
        {
            int secondNum = second.ContainsKey(pair.Key) ? second[pair.Key] : 0;
            if ( pair.Value > secondNum )
            {
                firstNotSecond[pair.Key] = pair.Value - secondNum;
            }
        }
        return firstNotSecond;
    }

当我尝试在案例

中使用它时
        Dictionary<string, int> friendsGained = latestTwo[0].names / latestTwo[1].names,
                                  friendsLost = latestTwo[1].names / latestTwo[0].names;

我收到编译时错误

  

运算符'/'不能应用于类型的操作数   'System.Collections.Generic.Dictionary<string,int>'和   'System.Collections.Generic.Dictionary<string,int>'

是什么给出了?

2 个答案:

答案 0 :(得分:2)

当实现像<{1}}这样的二元运算符时,至少有一个参数应该是类,你正在实现

/

我建议使用扩展方法,例如

  public class MyClass {
    ...
    // Doesn't compile:
    // Either "first" or "second" should be of MyClass type
    public static Dictionary<string, int> operator / 
      (Dictionary<string, int> first, Dictionary<string, int> second) {...}
    ...
  }

所以你可以把它

public static DictionaryExtensions {
  public static Dictionary<K, int> Subtract(this IDictionary<K, int> first, 
                                                 IDictionary<K, int> second) {
    if (null == first)
      return null; // or throw new ArgumentNullException("first");  
    else if (null == second)
      return first.ToDictionary(pair => pair.Key, pair => pair.Value); // let it be copy
             // or throw new ArgumentNullException("second");  

    Dictionary<K, int> result = new Dictionary<K, int>();

    foreach (var pair in first) {
      int secondNum;

      if (second.TryGetValue(pair.Key, out secondNum)) 
        if (pair.Value > secondName) 
          result.Add(pair.Key, pair.Value - secondNum);
    }

    return result;
  }
}

答案 1 :(得分:0)

正如其他人所说,你需要把你的班级作为经营者的一部分。

public class YourClass
{
    public Dictionary<string, int> YourProperty...

     public static Dictionary<string, int> operator / ( YourClass first, Dictionary<string, int> second )
    {
        // Returns all elements in first but not second, where the number in the first but not second
        // for a given key is the first's value minus the second's value
        Dictionary<string, int> firstNotSecond = new Dictionary<string, int>();
        foreach ( KeyValuePair<string, int> pair in first.YourProperty )
        {
            int secondNum = second.ContainsKey(pair.Key) ? second[pair.Key] : 0;
            if ( pair.Value > secondNum )
            {
                firstNotSecond[pair.Key] = pair.Value - secondNum;
            }
        }
        return firstNotSecond;
    }
}