1

I often use Tuple.Create on the Tuple class. Now I'm creating a KeyValuePair with:

new KeyValuePair<string, MyAnnoyingButNecessarilyVerboseClass>(myString, myObj)

I didn't want to include the type arguments in this case because they obfuscate the code a bit IMO. I've added the following to mimic Tuple.Create for KeyValuePairs.

public static class KVP
{
    public static KeyValuePair<TKey, TValue> Create<TKey, TValue>(TKey key, TValue value)
    {
        return new KeyValuePair<TKey, TValue>(key, value);
    }
}

So I can write

KVP.Create(myString, myObj)

without having to include type arguments explicitly because they are inferred from the other arguments. This works nicely, and I don't see any downsides. So I have a few very related questions.

  1. Why did C# not include a non generic System.KeyValuePair class with a static create method? ("They didn't because they didn't want to invest the effort for a feature that provides little value" is a completely acceptable and understandable answer)
  2. Am I missing a function that does something similiar?
  3. Are there downsides to creating a KeyValuePair class that I'm missing?
  4. Is there a more terse way to create a generic KeyValuePair (besides shortening the class and function name obviously :))?
Steven Wexler
  • 16,589
  • 8
  • 53
  • 80
  • You're still going to have to provide type parameters to KVP.Create, just like with Tuple. You can't create a non-generic factory method which produces generic output. – Dave R. May 22 '13 at 23:19
  • 1
    Yes, KVP.Create requires type arguments. But I don't have to explicitly provide them because they are inferred by the other arguments. I updated my question to make what I mean a bit clearer. (I think I used parameters where I meant arguments in two places) – Steven Wexler May 22 '13 at 23:25
  • 1
    I would remove question #1 as no one here (Except perhaps [Eric Lippert](http://stackoverflow.com/users/88656/eric-lippert)) can answer that one without guessing and does not fit the Q&A format of this site. – Scott Chamberlain May 22 '13 at 23:36
  • 1
    Is your #1 basically "Why isn't my one-line method already implemented in the BCL" ? And is your #4 "Can my code consisting of a single method invocation be made shorter" ?! – AakashM May 23 '13 at 09:00

3 Answers3

1

Why did C# not include a non generic System.KeyValuePair class with a static create method? ("They didn't because they didn't want to invest the effort for a feature that provides little value" is a completely acceptable and understandable answer)

Question isn't a good fit for SO, doubtful it actually has an answer.

Am I missing a function that does something similiar?

No, as of .net 4.5 there isn't one in the framework.

Are there downsides to creating a KeyValuePair class that I'm missing?

No, I've created one myself.

Is there a more terse way to create a generic KeyValuePair ?

No, that's pretty much as terse as you could do it in C#

Binary Worrier
  • 50,774
  • 20
  • 136
  • 184
0

I think that KeyValuePair only exists at all because it predates Tuple by several versions of C#. With Tuple they just figured out a better way to do things.

Marc Shapiro
  • 571
  • 2
  • 2
  • 1
    I think KeyValuePair plays a slightly different, and important role compared to Tuple. It emphasizes a different relationship between its two properties and more importantly it fits nicely with IDictionary. Imagine writing code like mydictionary.Where(kvp => kvp.Item1 == "mykey") if dictionaries implemented an IEnumerable of tuples instead of KeyValuePairs...weird – Steven Wexler May 22 '13 at 23:49
  • 1
    Also, `KeyValuePair` is `struct` whereas `Tuple` is `class`. They both have their uses. – 13xforever Jun 05 '13 at 10:51
0

a non generic equivalent for

KeyValuePair<TKey, TValue>

is

System.Collections.DictionaryEntry 
xMichal
  • 624
  • 2
  • 7
  • 19