Update to C# 10 and upgrade code style (#10105)
diff --git a/csharp/src/Google.Protobuf/Collections/MapField.cs b/csharp/src/Google.Protobuf/Collections/MapField.cs
index 0f02d59..f0124ee 100644
--- a/csharp/src/Google.Protobuf/Collections/MapField.cs
+++ b/csharp/src/Google.Protobuf/Collections/MapField.cs
@@ -31,9 +31,7 @@
#endregion
using Google.Protobuf.Compatibility;
-using Google.Protobuf.Reflection;
using System;
-using System.Buffers;
using System.Collections;
using System.Collections.Generic;
using System.IO;
@@ -74,9 +72,8 @@
private static readonly EqualityComparer<TKey> KeyEqualityComparer = ProtobufEqualityComparers.GetEqualityComparer<TKey>();
// 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 =
- new Dictionary<TKey, LinkedListNode<KeyValuePair<TKey, TValue>>>(KeyEqualityComparer);
- private readonly LinkedList<KeyValuePair<TKey, TValue>> list = new LinkedList<KeyValuePair<TKey, TValue>>();
+ private readonly Dictionary<TKey, LinkedListNode<KeyValuePair<TKey, TValue>>> map = new(KeyEqualityComparer);
+ private readonly LinkedList<KeyValuePair<TKey, TValue>> list = new();
/// <summary>
/// Creates a deep clone of this object.
@@ -144,8 +141,7 @@
public bool Remove(TKey key)
{
ProtoPreconditions.CheckNotNullUnconstrained(key, nameof(key));
- LinkedListNode<KeyValuePair<TKey, TValue>> node;
- if (map.TryGetValue(key, out node))
+ if (map.TryGetValue(key, out LinkedListNode<KeyValuePair<TKey, TValue>> node))
{
map.Remove(key);
node.List.Remove(node);
@@ -167,15 +163,14 @@
/// <returns><c>true</c> if the map contains an element with the specified key; otherwise, <c>false</c>.</returns>
public bool TryGetValue(TKey key, out TValue value)
{
- LinkedListNode<KeyValuePair<TKey, TValue>> node;
- if (map.TryGetValue(key, out node))
+ if (map.TryGetValue(key, out LinkedListNode<KeyValuePair<TKey, TValue>> node))
{
value = node.Value.Value;
return true;
}
else
{
- value = default(TValue);
+ value = default;
return false;
}
}
@@ -192,8 +187,7 @@
get
{
ProtoPreconditions.CheckNotNullUnconstrained(key, nameof(key));
- TValue value;
- if (TryGetValue(key, out value))
+ if (TryGetValue(key, out TValue value))
{
return value;
}
@@ -207,9 +201,8 @@
{
ProtoPreconditions.CheckNotNullUnconstrained(value, nameof(value));
}
- LinkedListNode<KeyValuePair<TKey, TValue>> node;
var pair = new KeyValuePair<TKey, TValue>(key, value);
- if (map.TryGetValue(key, out node))
+ if (map.TryGetValue(key, out LinkedListNode<KeyValuePair<TKey, TValue>> node))
{
node.Value = pair;
}
@@ -224,12 +217,12 @@
/// <summary>
/// Gets a collection containing the keys in the map.
/// </summary>
- public ICollection<TKey> Keys { get { return new MapView<TKey>(this, pair => pair.Key, ContainsKey); } }
+ public ICollection<TKey> Keys => new MapView<TKey>(this, pair => pair.Key, ContainsKey);
/// <summary>
/// Gets a collection containing the values in the map.
/// </summary>
- public ICollection<TValue> Values { get { return new MapView<TValue>(this, pair => pair.Value, ContainsValue); } }
+ public ICollection<TValue> Values => new MapView<TValue>(this, pair => pair.Value, ContainsValue);
/// <summary>
/// Adds the specified entries to the map. The keys and values are not automatically cloned.
@@ -250,10 +243,7 @@
/// <returns>
/// An enumerator that can be used to iterate through the collection.
/// </returns>
- public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
- {
- return list.GetEnumerator();
- }
+ public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() => list.GetEnumerator();
/// <summary>
/// Returns an enumerator that iterates through a collection.
@@ -261,19 +251,13 @@
/// <returns>
/// An <see cref="T:System.Collections.IEnumerator" /> object that can be used to iterate through the collection.
/// </returns>
- IEnumerator IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
+ IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
/// <summary>
/// Adds the specified item to the map.
/// </summary>
/// <param name="item">The item to add to the map.</param>
- void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item)
- {
- Add(item.Key, item.Value);
- }
+ void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item) => Add(item.Key, item.Value);
/// <summary>
/// Removes all items from the map.
@@ -289,21 +273,16 @@
/// </summary>
/// <param name="item">The key/value pair to find.</param>
/// <returns></returns>
- bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item)
- {
- TValue value;
- return TryGetValue(item.Key, out value) && ValueEqualityComparer.Equals(item.Value, value);
- }
+ bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item) =>
+ TryGetValue(item.Key, out TValue value) && ValueEqualityComparer.Equals(item.Value, value);
/// <summary>
/// Copies the key/value pairs in this map to an array.
/// </summary>
/// <param name="array">The array to copy the entries into.</param>
/// <param name="arrayIndex">The index of the array at which to start copying values.</param>
- void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
- {
+ void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) =>
list.CopyTo(array, arrayIndex);
- }
/// <summary>
/// Removes the specified key/value pair from the map.
@@ -317,8 +296,7 @@
{
throw new ArgumentException("Key is null", nameof(item));
}
- LinkedListNode<KeyValuePair<TKey, TValue>> node;
- if (map.TryGetValue(item.Key, out node) &&
+ if (map.TryGetValue(item.Key, out LinkedListNode<KeyValuePair<TKey, TValue>> node) &&
EqualityComparer<TValue>.Default.Equals(item.Value, node.Value.Value))
{
map.Remove(item.Key);
@@ -334,12 +312,12 @@
/// <summary>
/// Gets the number of elements contained in the map.
/// </summary>
- public int Count { get { return list.Count; } }
+ public int Count => list.Count;
/// <summary>
/// Gets a value indicating whether the map is read-only.
/// </summary>
- public bool IsReadOnly { get { return false; } }
+ public bool IsReadOnly => false;
/// <summary>
/// Determines whether the specified <see cref="System.Object" />, is equal to this instance.
@@ -348,10 +326,7 @@
/// <returns>
/// <c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.
/// </returns>
- public override bool Equals(object other)
- {
- return Equals(other as MapField<TKey, TValue>);
- }
+ public override bool Equals(object other) => Equals(other as MapField<TKey, TValue>);
/// <summary>
/// Returns a hash code for this instance.
@@ -396,8 +371,7 @@
var valueComparer = ValueEqualityComparer;
foreach (var pair in this)
{
- TValue value;
- if (!other.TryGetValue(pair.Key, out value))
+ if (!other.TryGetValue(pair.Key, out TValue value))
{
return false;
}
@@ -529,33 +503,20 @@
}
#region IDictionary explicit interface implementation
- void IDictionary.Add(object key, object value)
- {
- Add((TKey)key, (TValue)value);
- }
- bool IDictionary.Contains(object key)
- {
- if (!(key is TKey))
- {
- return false;
- }
- return ContainsKey((TKey)key);
- }
+ void IDictionary.Add(object key, object value) => Add((TKey)key, (TValue)value);
- IDictionaryEnumerator IDictionary.GetEnumerator()
- {
- return new DictionaryEnumerator(GetEnumerator());
- }
+ bool IDictionary.Contains(object key) => key is TKey k && ContainsKey(k);
+
+ IDictionaryEnumerator IDictionary.GetEnumerator() => new DictionaryEnumerator(GetEnumerator());
void IDictionary.Remove(object key)
{
ProtoPreconditions.CheckNotNull(key, nameof(key));
- if (!(key is TKey))
+ if (key is TKey k)
{
- return;
+ Remove(k);
}
- Remove((TKey)key);
}
void ICollection.CopyTo(Array array, int index)
@@ -565,28 +526,27 @@
temp.CopyTo(array, index);
}
- bool IDictionary.IsFixedSize { get { return false; } }
+ bool IDictionary.IsFixedSize => false;
- ICollection IDictionary.Keys { get { return (ICollection)Keys; } }
+ ICollection IDictionary.Keys => (ICollection)Keys;
- ICollection IDictionary.Values { get { return (ICollection)Values; } }
+ ICollection IDictionary.Values => (ICollection)Values;
- bool ICollection.IsSynchronized { get { return false; } }
+ bool ICollection.IsSynchronized => false;
- object ICollection.SyncRoot { get { return this; } }
+ object ICollection.SyncRoot => this;
object IDictionary.this[object key]
{
get
{
ProtoPreconditions.CheckNotNull(key, nameof(key));
- if (!(key is TKey))
+ if (key is TKey k)
{
- return null;
+ TryGetValue(k, out TValue value);
+ return value;
}
- TValue value;
- TryGetValue((TKey)key, out value);
- return value;
+ return null;
}
set
@@ -610,20 +570,14 @@
this.enumerator = enumerator;
}
- public bool MoveNext()
- {
- return enumerator.MoveNext();
- }
+ public bool MoveNext() => enumerator.MoveNext();
- public void Reset()
- {
- enumerator.Reset();
- }
+ public void Reset() => enumerator.Reset();
- public object Current { get { return Entry; } }
- public DictionaryEntry Entry { get { return new DictionaryEntry(Key, Value); } }
- public object Key { get { return enumerator.Current.Key; } }
- public object Value { get { return enumerator.Current.Value; } }
+ public object Current => Entry;
+ public DictionaryEntry Entry => new DictionaryEntry(Key, Value);
+ public object Key => enumerator.Current.Key;
+ public object Value => enumerator.Current.Value;
}
/// <summary>
@@ -682,28 +636,19 @@
this.containsCheck = containsCheck;
}
- public int Count { get { return parent.Count; } }
+ public int Count => parent.Count;
- public bool IsReadOnly { get { return true; } }
+ public bool IsReadOnly => true;
- public bool IsSynchronized { get { return false; } }
+ public bool IsSynchronized => false;
- public object SyncRoot { get { return parent; } }
+ public object SyncRoot => parent;
- public void Add(T item)
- {
- throw new NotSupportedException();
- }
+ public void Add(T item) => throw new NotSupportedException();
- public void Clear()
- {
- throw new NotSupportedException();
- }
+ public void Clear() => throw new NotSupportedException();
- public bool Contains(T item)
- {
- return containsCheck(item);
- }
+ public bool Contains(T item) => containsCheck(item);
public void CopyTo(T[] array, int arrayIndex)
{
@@ -726,15 +671,9 @@
return parent.list.Select(projection).GetEnumerator();
}
- public bool Remove(T item)
- {
- throw new NotSupportedException();
- }
+ public bool Remove(T item) => throw new NotSupportedException();
- IEnumerator IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
+ IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public void CopyTo(Array array, int index)
{