aboutsummaryrefslogtreecommitdiff
path: root/csharp
diff options
context:
space:
mode:
authorJean-Rémy Bancel <dev@bancel.me>2017-07-12 00:25:30 -0700
committerJon Skeet <skeet@pobox.com>2017-07-12 09:28:43 +0100
commit727c0dc1fae6c7b72f59fb29f47a52f133618461 (patch)
tree62fe7c84338da3abb7c7481c15905c1eb29c6fce /csharp
parent9ab7c73f7c989afa777a40ff2a7c5f0f138be22c (diff)
downloadprotobuf-727c0dc1fae6c7b72f59fb29f47a52f133618461.tar.gz
protobuf-727c0dc1fae6c7b72f59fb29f47a52f133618461.tar.bz2
protobuf-727c0dc1fae6c7b72f59fb29f47a52f133618461.zip
C#: Implement IReadOnlyDictionary<K,V> in MapField<K,V>
Diffstat (limited to 'csharp')
-rw-r--r--csharp/src/Google.Protobuf.Test/Collections/MapFieldTest.cs16
-rw-r--r--csharp/src/Google.Protobuf/Collections/MapField.cs11
2 files changed, 27 insertions, 0 deletions
diff --git a/csharp/src/Google.Protobuf.Test/Collections/MapFieldTest.cs b/csharp/src/Google.Protobuf.Test/Collections/MapFieldTest.cs
index 9d3d69af..68b4de45 100644
--- a/csharp/src/Google.Protobuf.Test/Collections/MapFieldTest.cs
+++ b/csharp/src/Google.Protobuf.Test/Collections/MapFieldTest.cs
@@ -540,6 +540,22 @@ namespace Google.Protobuf.Collections
Assert.Throws<ArgumentException>(() => map.ToString());
}
+#if !NET35
+ [Test]
+ public void IDictionaryKeys_Equals_IReadOnlyDictionaryKeys()
+ {
+ var map = new MapField<string, string> { { "foo", "bar" }, { "x", "y" } };
+ CollectionAssert.AreEquivalent(((IDictionary<string, string>)map).Keys, ((IReadOnlyDictionary<string, string>)map).Keys);
+ }
+
+ [Test]
+ public void IDictionaryValues_Equals_IReadOnlyDictionaryValues()
+ {
+ var map = new MapField<string, string> { { "foo", "bar" }, { "x", "y" } };
+ CollectionAssert.AreEquivalent(((IDictionary<string, string>)map).Values, ((IReadOnlyDictionary<string, string>)map).Values);
+ }
+#endif
+
private static KeyValuePair<TKey, TValue> NewKeyValuePair<TKey, TValue>(TKey key, TValue value)
{
return new KeyValuePair<TKey, TValue>(key, value);
diff --git a/csharp/src/Google.Protobuf/Collections/MapField.cs b/csharp/src/Google.Protobuf/Collections/MapField.cs
index ef5651c9..8dac8e30 100644
--- a/csharp/src/Google.Protobuf/Collections/MapField.cs
+++ b/csharp/src/Google.Protobuf/Collections/MapField.cs
@@ -67,6 +67,9 @@ namespace Google.Protobuf.Collections
/// </para>
/// </remarks>
public sealed class MapField<TKey, TValue> : IDeepCloneable<MapField<TKey, TValue>>, IDictionary<TKey, TValue>, IEquatable<MapField<TKey, TValue>>, IDictionary
+#if !NET35
+ , IReadOnlyDictionary<TKey, TValue>
+#endif
{
// TODO: Don't create the map/list until we have an entry. (Assume many maps will be empty.)
private readonly Dictionary<TKey, LinkedListNode<KeyValuePair<TKey, TValue>>> map =
@@ -548,6 +551,14 @@ namespace Google.Protobuf.Collections
}
#endregion
+ #region IReadOnlyDictionary explicit interface implementation
+#if !NET35
+ IEnumerable<TKey> IReadOnlyDictionary<TKey, TValue>.Keys => Keys;
+
+ IEnumerable<TValue> IReadOnlyDictionary<TKey, TValue>.Values => Values;
+#endif
+ #endregion
+
private class DictionaryEnumerator : IDictionaryEnumerator
{
private readonly IEnumerator<KeyValuePair<TKey, TValue>> enumerator;