I’m currently using…
public static class KeyValuePairFactory {
public static KeyValuePair<TKey, TValue> Create<TKey, TValue>(
TKey key,
TValue value
) {
return new KeyValuePair<TKey, TValue>(key, value);
}
}
…so instead of…
var pair = new KeyValuePair<MyVeryUnwieldyType1, MyVeryUnwieldyType2>(
new MyVeryUnwieldyType1(),
new MyVeryUnwieldyType2()
);
…I can avoid repeating types and simply:
var pair = KeyValuePairFactory.Create(
new MyVeryUnwieldyType1(),
new MyVeryUnwieldyType2()
);
Am I reinventing the wheel and reimplementing something that is already available in .NET Framework?
Well, it depends on whether you definitely want
KeyValuePair. I don’t know that there’s anything forKeyValuePair, but it does exist onTuple, viaTuple.Create:Personally I rarely find myself creating
KeyValuePairvalues directly – I find it’s more common to be consuming them as part of using LINQ to Objects on aDictionary<,>.