Fix IDE0025 (use expression body for properties)
[mono-project.git] / netcore / System.Private.CoreLib / shared / System / Collections / Generic / KeyValuePair.cs
blobf53b816e985c53fabffc7416cd02ff48f12c83b4
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;
6 using System.Text;
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);
19 /// <summary>
20 /// Used by KeyValuePair.ToString to reduce generic code
21 /// </summary>
22 internal static string PairToString(object? key, object? value)
24 StringBuilder s = StringBuilderCache.Acquire();
25 s.Append('[');
27 if (key != null)
29 s.Append(key);
32 s.Append(", ");
34 if (value != null)
36 s.Append(value);
39 s.Append(']');
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>.
48 [Serializable]
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)
57 this.key = key;
58 this.value = 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)
73 key = Key;
74 value = Value;