运算符重载的字典

时间:2017-05-29 22:53:34

标签: c# dictionary operator-overloading .net-standard

我想知道我是否可以使用运算符重载将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;
   }
   }

2 个答案:

答案 0 :(得分:3)

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

  

首先,如果其中一个参数,您不能重载运算符   重载参数列表与包含的类型不同   class,所以如果你在类Polinomio中,你必须提供这种类型的一个参数,这是你不想要的,因为你想要为+类重载运算符Dictionary<>。   请参阅此问题here包含类的问题是什么。

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

    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<>类添加扩展方法,如下所示:

      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;
      }
      
  2. 你可以像下面这样打电话:

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

答案 1 :(得分:0)

我可以建议这个吗?

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