-1

I want to override the default structure of KeyValuePair in C#, so that I can make a KeyValuePair to accept a 'var' types.

Something like this :

List<KeyValuePair<string, var>> kvpList = new List<KeyValuePair<string, var>>()
                {
                    new KeyValuePair<string, var>("Key1", 000),
                    new KeyValuePair<string, var>("Key2", "value2"),
                    new KeyValuePair<string, var>("Key3", 25.45),
                };

Even if its possible for dictionary, then also it will solve my problem.

Koder101
  • 844
  • 15
  • 28
  • 1
    `var` isn't a type... as Haney has said, if you want to just accept any (non-pointer) type, you can just use `object` instead. It would be worth you reading up more about what `var` really means in C#. – Jon Skeet Jul 25 '15 at 18:04
  • Thanks for your valuable answer/comment for making me understand that what is possible and what is not. – Koder101 Jul 25 '15 at 18:13
  • You still seem to have missed my point - `var` isn't a type. There's no such thing as a "List of type var". – Jon Skeet Jul 25 '15 at 18:15
  • Oops, yes, I got it now. – Koder101 Jul 25 '15 at 18:16

2 Answers2

2

You could use object as your type, and then cast to/from object to desired outcomes. However, it's important to note that this is very much the opposite of object oriented programming, and generally indicates an error in your design and architecture.

Haney
  • 32,775
  • 8
  • 59
  • 68
  • Thanks for your valuable answer/comment for making me understand that what is possible and what is not. – Koder101 Jul 25 '15 at 18:15
0

Hmm I am wondering if this might help you: To have a list as you want, it is really possible BUT the "var" type (as you named it) must be the same for all KeyValuePair instances. For having whatever type you must use object or dynamic (use Haney's answer).

So considering that you want a single type for all KeyValuePair instances, here is a solution:

Firstly, create this helper class:

public static class KeyValuePairExtentions
{
    public static List<KeyValuePair<string, T>> GetNewListOfType<T>(Expression<Func<T>> type)
    {
        return new List<KeyValuePair<string, T>>();
    }

    public static void AddNewKeyValuePair<T>(this List<KeyValuePair<string, T>> @this, string key, T element)
    {
        @this.Add(new KeyValuePair<string, T>(key, element));
    }
}

To consume these functions, here is an example:

var lst = KeyValuePairExtentions.GetNewListOfType(() => new {Id = default (int), Name = default (string)});

lst.AddNewKeyValuePair("test1", new {Id = 3, Name = "Keith"});

The ideea is to rely on the powerfull type inference feature that we have in C#. Some notes:

1) if T is anonymous and you create a new instance of a list in an assembly and consume it in another assembly it is VERY possible that this will NOT work due to the fact that an anonymous type is compiled per assembly (in other words, if you have a variable var x = new { X = 3 } in an assembly and in another var y = new { X = 3 } then x.GetType () != y.GeTType () but in the same assembly types are the same.)

2) If you are wondering whether an instance it's created or not by calling GetNewListOfType, the answer is NO because it is an expression tree function and the function is not even compiled. Even with a Func will work because I am not calling the function in my code. I am using the function just for type inference.

George Lica
  • 1,798
  • 1
  • 12
  • 23