1

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!

  • I'd expect `"a" + "b"` to produce `"ab"`, not `"a + b"`... Either way, if you want operator overloading you'll have to create your own type. If you want both operator overloading and need to use `KeyValuePair`, you can define (explicit and/or implicit) [conversion operators](https://msdn.microsoft.com/en-us/library/85w54y0a.aspx) to ease conversion between your own type and `KeyValuePair`. – Pieter Witvoet Apr 22 '16 at 12:43

1 Answers1

6

You cannot do this.

There is no way to add operators to other types, you must add them inside the types involved.

The best you can do is either to create an extension method or just a normal method somewhere.

However, extension method isn't easy either since you don't have access to operators for generic types in generic methods.

Extension method example that doesn't work:

public static class MyKeyValuePairExtensions
{
    public static KeyValuePair<TKey, TValue> Add<TKey, TValue>(
        this KeyValuePair<TKey, TValue> first,
        KeyValuePair<TKey, TValue> second)
    {
        return new KeyValuePair<TKey, TValue>(
            first.Key + second.Key,
            first.Value + second.Value);
    }
}

This will fail to compile with this:

CS0019 Operator '+' cannot be applied to operands of type 'TKey' and 'TKey'

Now, you could add in various 3rd party nuget packages that lets you simulate this but I'm going to leave this as an excercise for the reader.

A different alternative would be to just add an extension method for your specific case:

public static class MyKeyValuePairExtensions
{
    public static KeyValuePair<string, double> Add(
        this KeyValuePair<string, double> first,
        KeyValuePair<string, double> second)
    {
        return new KeyValuePair<string, double>(
            first.Key + " + " + second.Key,
            first.Value + second.Value);
    }
}

You would then call it like this:

KeyValuePair<string, double> kv1 = new KeyValuePair<string, double>("a", 5);
KeyValuePair<string, double> kv2 = new KeyValuePair<string, double>("b", 7);
var sum = kv1.Add(kv2); Key="a + b", and Value=12

Here's a .NET Fiddle to experiment with.

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825