C# – serializable generic Key/Value pair class in .NET

.netc++

I'm looking for a key/value pair object that I can include in a web service.

I tried using .NET's System.Collections.Generic.KeyValuePair<> class, but it does not properly serialize in a web service. In a web service, the Key and Value properties are not serialized, making this class useless, unless someone knows a way to fix this.

Is there any other generic class that can be used for this situation?

I'd use .NET's System.Web.UI.Pair class, but it uses Object for its types. It would be nice to use a Generic class, if only for type safety.

Best Solution

Just define a struct/class.

[Serializable]
public struct KeyValuePair<K,V>
{
  public K Key {get;set;}
  public V Value {get;set;}
}