运算符重载的字典

Dictionaries with operator overloading

我想知道我是否可以使用运算符重载将 2 个词典合并为一个,但我离正确的模式还很远,这是我的代码。

    class Program
    {
     static void Main(string[] args)
     {
         int[] array1key = new int[] { 0, 1, 3, 7 };
         int[] array1values = new int[] { 5, 7, -3, 9 };

         int[] array2key = new int[] { 0, 2 };
         int[] array2values = new int[] { 2, 4 };

         Dictionary<int, int> Polinomio1 = new Dictionary<int, int>();
         Dictionary<int, int> Polinomio2 = new Dictionary<int, int>();

         for (int i = 0; i < array1key.Length; i++)
         {
             Polinomio1.Add(array1key[i], array1values[i]);
         }
         for (int i = 0; i < array2key.Length; i++)
         {
             Polinomio2.Add(array2key[i], array2values[i]);
         }
         Dictionary<int, int> Sumar = Polinomio1 + Polinomio2;
     }
   }
   class Polinomio
   {
   public Dictionary<int,int> Polinomio1 = new Dictionary<int,int>();
   public static Dictionary<int,int> operator + (Dictionary<int,int> 
   Polinomio1,Dictionary<int,int> Polinomio2)
   {
        return Polinomio1 + Polinomio2;
   }
   }

这是一个复杂的情况,但我可以给你一些想法,你可以选择最适合你的。

First you can not overload an operator if one of the parameters in overloading parameter list is not the same type of the containing class, so if you are in the class Polinomio you must provide one of the parameters of this type which is not want you want because you are looking to overload operator + for Dictionary<> class. Refer to this question here what is the problem for containing class.

  1. 解决这个问题的一种方法是从 Dictionary<> class 本身继承。让我们看下面的例子来更好地理解我的意思。

    class Program
    {
        static void Main(string[] args)
        {
            //Made those in same size to not throw an exception
            int[] array1Key = new int[] { 0, 1, 3, 7 };
            int[] array1Values = new int[] { 5, 7, -3, 9 };
    
            int[] array2Key = new int[] { 0, 2, 3, 4 };
            int[] array2Values = new int[] { 2, 4, 4, 8 };
    
            //Create Polinomio object which will serve as `Dictionary<>` in this case
            Polinomio polinomio1 = new Polinomio();
            Polinomio polinomio2 = new Polinomio();
    
            for (int i = 0; i < array1Key.Length; i++)
            {
                polinomio1.Add(array1Key[i], array1Values[i]);
            }
            for (int i = 0; i < array2Key.Length; i++)
            {
                polinomio2.Add(array2Key[i], array2Values[i]);
            }
            Dictionary<int, int> sum = polinomio1 + polinomio2;
            for (int i = 0; i < sum.Count; i++)
            {
                Console.WriteLine($"{sum.Keys.ElementAt(i)} {sum.Values.ElementAt(1)}");
            }
            Console.ReadLine();
        }
    
    }
    public class Polinomio : Dictionary<int, int> //inheritance
    {
        public static Dictionary<int, int> operator +(Polinomio p1, Polinomio p2)
        {
            if (p1.Count != p2.Count)
            {
                throw new Exception("Not the same Size");
            }
            Dictionary<int, int> dictionaryTemp = new Dictionary<int, int>();
            for (int i = 0; i < p1.Count; i++)
            {
                dictionaryTemp.Add(p1.Keys.ElementAt(i) + p1.Keys.ElementAt(i), p1.Values.ElementAt(i) + p2.Values.ElementAt(1));
            }
            return dictionaryTemp;
        }
    }
    
    1. 另一种解决方案是向 Dictionary<> class 添加扩展方法,如下所示:

      public static Dictionary<int, int> Sum(this Dictionary<int, int> p1, Dictionary<int, int> p2) 
      {
         if (p1.Count != p2.Count)
         {
             throw new Exception("Not the same Size");
         }
         Dictionary<int, int> dictionaryTemp = new Dictionary<int, int>();
         for (int i = 0; i < p1.Count; i++)
         {
             dictionaryTemp.Add(p1.Keys.ElementAt(i) + p1.Keys.ElementAt(i), p1.Values.ElementAt(i) + p2.Values.ElementAt(1));
         }
         return dictionaryTemp;
      }
      

你可以这样称呼它:

Dictionary<int, int> sum = polinomio1.Sum(polinomio2);
for (int i = 0; i < sum.Count; i++)
{
   Console.WriteLine($"{sum.Keys.ElementAt(i)} {sum.Values.ElementAt(1)}");
}

我可以推荐这个吗:

    public static Dictionary<int, double> Addition(this Dictionary<int, double> p1, Dictionary<int, double> p2)
{
    if (p1.Count != p2.Count)
    {
        throw new Exception("Not the same Size");
    }
    Dictionary<int, double> dictionaryTemp = new Dictionary<int, double>();
    foreach(var key in p1.Keys)
    {
        dictionaryTemp.Add(key, p1[key] + p2[key]);
    }
    return dictionaryTemp;
}