Let say I have two KeyValuePair variable.
KeyValuePair<string, double> kv1 = new KeyValuePair<string, double>("a", 5);
KeyValuePair<string, double> kv2 = new KeyValuePair<string, double>("b", 7);
There is no definition of + operation in "KeyValuePair" struct.
So I want to use operator overloading!
My target is to get third KeyValuePair type variable like:
KeyValuePair<string, double> kv1 = new KeyValuePair<string, double>(kv1.Key + " + " + kv2.Key, kv1.Value + kv2.Value);
And the result will be :
KeyValuePair<string, double>("a + b", 12)
But tell me how to do it using "operator" ?
I was trying to do it like this way:
public partial class Form1 : Form
{
public Form1()
{
KeyValuePair<string, double> kv1 = new KeyValuePair<string, double>("a", 5);
KeyValuePair<string, double> kv2 = new KeyValuePair<string, double>("b", 7);
KeyValuePair<string, double> k = kv1 + kv2;
}
public static KeyValuePair<string, double> operator +(KeyValuePair<string, double> c1, KeyValuePair<string, double> c2) => new KeyValuePair<string, double>(c1.Key + " + " + c2.Key, c1.Value + c2.Value);
}
But there is an error message that: "At least one of the parameters should be Form1"
Which means that you can only create operator for Form.From1 inputs...
I thought to extend KeyValuePair class!
But then I introduced that "KeyValuePair" is a struct but it is not a class!
Can we create new struct which inherits from "KeyValuePair" struct?
So How to do it?
Thank you!