I was wondering if i can make 2 dictionaries sum into one using operator overloading, but im far away of the right mode to do it, here is my code.
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;
}
}
This is a complex situation but I can give you some idea and you may choose the best one for you.
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+
forDictionary<>
class. Refer to this question here what is the problem for containing class.
One way to solve this is issue is to inherit from Dictionary<>
class itself. Lets see the example below to understand better what I mean.
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;
}
}
Another solution is to add an extension method to Dictionary<>
class like below:
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;
}
You can call it like below:
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)}");
}