1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
5 using System
.ComponentModel
;
8 namespace System
.Collections
.Generic
10 // Provides the Create factory method for KeyValuePair<TKey, TValue>.
11 public static class KeyValuePair
13 // Creates a new KeyValuePair<TKey, TValue> from the given values.
14 public static KeyValuePair
<TKey
, TValue
> Create
<TKey
, TValue
>(TKey key
, TValue
value)
16 return new KeyValuePair
<TKey
, TValue
>(key
, value);
20 /// Used by KeyValuePair.ToString to reduce generic code
22 internal static string PairToString(object? key
, object? value)
24 StringBuilder s
= StringBuilderCache
.Acquire();
41 return StringBuilderCache
.GetStringAndRelease(s
);
45 // A KeyValuePair holds a key and a value from a dictionary.
46 // It is used by the IEnumerable<T> implementation for both IDictionary<TKey, TValue>
47 // and IReadOnlyDictionary<TKey, TValue>.
49 [System
.Runtime
.CompilerServices
.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
50 public readonly struct KeyValuePair
<TKey
, TValue
>
52 private readonly TKey key
; // Do not rename (binary serialization)
53 private readonly TValue
value; // Do not rename (binary serialization)
55 public KeyValuePair(TKey key
, TValue
value)
61 public TKey Key
=> key
;
63 public TValue Value
=> value;
65 public override string ToString()
67 return KeyValuePair
.PairToString(Key
, Value
);
70 [EditorBrowsable(EditorBrowsableState
.Never
)]
71 public void Deconstruct(out TKey key
, out TValue
value)