From 22824d27c3129cf6773507a3ff7849832f300b5c Mon Sep 17 00:00:00 2001 From: buyaa-n Date: Thu, 19 Sep 2019 17:19:48 -0700 Subject: [PATCH] Annotate System.Collections.Specialized for nullable (dotnet/corefx#41092) Annotate System.Collections.NonGeneric for nullable Commit migrated from https://github.com/dotnet/corefx/commit/0d6adbc80b1d8262771a4db71931c4383646ff1f --- .../ref/System.Collections.Specialized.cs | 128 +++++++++--------- .../ref/System.Collections.Specialized.csproj | 1 + .../src/System.Collections.Specialized.csproj | 3 +- .../System/Collections/Specialized/BitVector32.cs | 4 +- .../Collections/Specialized/HybridDictionary.cs | 19 ++- .../Collections/Specialized/IOrderedDictionary.cs | 4 +- .../Collections/Specialized/ListDictionary.cs | 51 +++---- .../Specialized/NameObjectCollectionBase.cs | 72 +++++----- .../Collections/Specialized/NameValueCollection.cs | 67 +++++----- .../Collections/Specialized/OrderedDictionary.cs | 147 ++++++++++----------- .../Collections/Specialized/StringCollection.cs | 42 +++--- .../Collections/Specialized/StringDictionary.cs | 8 +- 12 files changed, 276 insertions(+), 270 deletions(-) diff --git a/src/libraries/System.Collections.Specialized/ref/System.Collections.Specialized.cs b/src/libraries/System.Collections.Specialized/ref/System.Collections.Specialized.cs index c485a17..43f8fc8 100644 --- a/src/libraries/System.Collections.Specialized/ref/System.Collections.Specialized.cs +++ b/src/libraries/System.Collections.Specialized/ref/System.Collections.Specialized.cs @@ -19,7 +19,7 @@ namespace System.Collections.Specialized public static int CreateMask(int previous) { throw null; } public static System.Collections.Specialized.BitVector32.Section CreateSection(short maxValue) { throw null; } public static System.Collections.Specialized.BitVector32.Section CreateSection(short maxValue, System.Collections.Specialized.BitVector32.Section previous) { throw null; } - public override bool Equals(object o) { throw null; } + public override bool Equals(object? o) { throw null; } public override int GetHashCode() { throw null; } public override string ToString() { throw null; } public static string ToString(System.Collections.Specialized.BitVector32 value) { throw null; } @@ -29,7 +29,7 @@ namespace System.Collections.Specialized public short Mask { get { throw null; } } public short Offset { get { throw null; } } public bool Equals(System.Collections.Specialized.BitVector32.Section obj) { throw null; } - public override bool Equals(object o) { throw null; } + public override bool Equals(object? o) { throw null; } public override int GetHashCode() { throw null; } public static bool operator ==(System.Collections.Specialized.BitVector32.Section a, System.Collections.Specialized.BitVector32.Section b) { throw null; } public static bool operator !=(System.Collections.Specialized.BitVector32.Section a, System.Collections.Specialized.BitVector32.Section b) { throw null; } @@ -47,11 +47,11 @@ namespace System.Collections.Specialized public bool IsFixedSize { get { throw null; } } public bool IsReadOnly { get { throw null; } } public bool IsSynchronized { get { throw null; } } - public object this[object key] { get { throw null; } set { } } + public object? this[object key] { get { throw null; } set { } } public System.Collections.ICollection Keys { get { throw null; } } public object SyncRoot { get { throw null; } } public System.Collections.ICollection Values { get { throw null; } } - public void Add(object key, object value) { } + public void Add(object key, object? value) { } public void Clear() { } public bool Contains(object key) { throw null; } public void CopyTo(System.Array array, int index) { } @@ -61,24 +61,24 @@ namespace System.Collections.Specialized } public partial interface IOrderedDictionary : System.Collections.ICollection, System.Collections.IDictionary, System.Collections.IEnumerable { - object this[int index] { get; set; } + object? this[int index] { get; set; } new System.Collections.IDictionaryEnumerator GetEnumerator(); - void Insert(int index, object key, object value); + void Insert(int index, object key, object? value); void RemoveAt(int index); } public partial class ListDictionary : System.Collections.ICollection, System.Collections.IDictionary, System.Collections.IEnumerable { public ListDictionary() { } - public ListDictionary(System.Collections.IComparer comparer) { } + public ListDictionary(System.Collections.IComparer? comparer) { } public int Count { get { throw null; } } public bool IsFixedSize { get { throw null; } } public bool IsReadOnly { get { throw null; } } public bool IsSynchronized { get { throw null; } } - public object this[object key] { get { throw null; } set { } } + public object? this[object key] { get { throw null; } set { } } public System.Collections.ICollection Keys { get { throw null; } } public object SyncRoot { get { throw null; } } public System.Collections.ICollection Values { get { throw null; } } - public void Add(object key, object value) { } + public void Add(object key, object? value) { } public void Clear() { } public bool Contains(object key) { throw null; } public void CopyTo(System.Array array, int index) { } @@ -89,44 +89,44 @@ namespace System.Collections.Specialized public abstract partial class NameObjectCollectionBase : System.Collections.ICollection, System.Collections.IEnumerable, System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializable { protected NameObjectCollectionBase() { } - protected NameObjectCollectionBase(System.Collections.IEqualityComparer equalityComparer) { } + protected NameObjectCollectionBase(System.Collections.IEqualityComparer? equalityComparer) { } [System.ObsoleteAttribute("Please use NameObjectCollectionBase(IEqualityComparer) instead.")] - protected NameObjectCollectionBase(System.Collections.IHashCodeProvider hashProvider, System.Collections.IComparer comparer) { } + protected NameObjectCollectionBase(System.Collections.IHashCodeProvider? hashProvider, System.Collections.IComparer? comparer) { } protected NameObjectCollectionBase(int capacity) { } - protected NameObjectCollectionBase(int capacity, System.Collections.IEqualityComparer equalityComparer) { } + protected NameObjectCollectionBase(int capacity, System.Collections.IEqualityComparer? equalityComparer) { } [System.ObsoleteAttribute("Please use NameObjectCollectionBase(Int32, IEqualityComparer) instead.")] - protected NameObjectCollectionBase(int capacity, System.Collections.IHashCodeProvider hashProvider, System.Collections.IComparer comparer) { } + protected NameObjectCollectionBase(int capacity, System.Collections.IHashCodeProvider? hashProvider, System.Collections.IComparer? comparer) { } protected NameObjectCollectionBase(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { } public virtual int Count { get { throw null; } } protected bool IsReadOnly { get { throw null; } set { } } public virtual System.Collections.Specialized.NameObjectCollectionBase.KeysCollection Keys { get { throw null; } } bool System.Collections.ICollection.IsSynchronized { get { throw null; } } object System.Collections.ICollection.SyncRoot { get { throw null; } } - protected void BaseAdd(string name, object value) { } + protected void BaseAdd(string? name, object? value) { } protected void BaseClear() { } - protected object BaseGet(int index) { throw null; } - protected object BaseGet(string name) { throw null; } - protected string[] BaseGetAllKeys() { throw null; } - protected object[] BaseGetAllValues() { throw null; } - protected object[] BaseGetAllValues(System.Type type) { throw null; } - protected string BaseGetKey(int index) { throw null; } + protected object? BaseGet(int index) { throw null; } + protected object? BaseGet(string? name) { throw null; } + protected string?[] BaseGetAllKeys() { throw null; } + protected object?[] BaseGetAllValues() { throw null; } + protected object?[] BaseGetAllValues(System.Type type) { throw null; } + protected string? BaseGetKey(int index) { throw null; } protected bool BaseHasKeys() { throw null; } - protected void BaseRemove(string name) { } + protected void BaseRemove(string? name) { } protected void BaseRemoveAt(int index) { } - protected void BaseSet(int index, object value) { } - protected void BaseSet(string name, object value) { } + protected void BaseSet(int index, object? value) { } + protected void BaseSet(string? name, object? value) { } public virtual System.Collections.IEnumerator GetEnumerator() { throw null; } public virtual void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { } - public virtual void OnDeserialization(object sender) { } + public virtual void OnDeserialization(object? sender) { } void System.Collections.ICollection.CopyTo(System.Array array, int index) { } public partial class KeysCollection : System.Collections.ICollection, System.Collections.IEnumerable { internal KeysCollection() { } public int Count { get { throw null; } } - public string this[int index] { get { throw null; } } + public string? this[int index] { get { throw null; } } bool System.Collections.ICollection.IsSynchronized { get { throw null; } } object System.Collections.ICollection.SyncRoot { get { throw null; } } - public virtual string Get(int index) { throw null; } + public virtual string? Get(int index) { throw null; } public System.Collections.IEnumerator GetEnumerator() { throw null; } void System.Collections.ICollection.CopyTo(System.Array array, int index) { } } @@ -134,58 +134,58 @@ namespace System.Collections.Specialized public partial class NameValueCollection : System.Collections.Specialized.NameObjectCollectionBase { public NameValueCollection() { } - public NameValueCollection(System.Collections.IEqualityComparer equalityComparer) { } + public NameValueCollection(System.Collections.IEqualityComparer? equalityComparer) { } [System.ObsoleteAttribute("Please use NameValueCollection(IEqualityComparer) instead.")] - public NameValueCollection(System.Collections.IHashCodeProvider hashProvider, System.Collections.IComparer comparer) { } + public NameValueCollection(System.Collections.IHashCodeProvider? hashProvider, System.Collections.IComparer? comparer) { } public NameValueCollection(System.Collections.Specialized.NameValueCollection col) { } public NameValueCollection(int capacity) { } - public NameValueCollection(int capacity, System.Collections.IEqualityComparer equalityComparer) { } + public NameValueCollection(int capacity, System.Collections.IEqualityComparer? equalityComparer) { } [System.ObsoleteAttribute("Please use NameValueCollection(Int32, IEqualityComparer) instead.")] - public NameValueCollection(int capacity, System.Collections.IHashCodeProvider hashProvider, System.Collections.IComparer comparer) { } + public NameValueCollection(int capacity, System.Collections.IHashCodeProvider? hashProvider, System.Collections.IComparer? comparer) { } public NameValueCollection(int capacity, System.Collections.Specialized.NameValueCollection col) { } protected NameValueCollection(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { } - public virtual string[] AllKeys { get { throw null; } } - public string this[int index] { get { throw null; } } - public string this[string name] { get { throw null; } set { } } + public virtual string?[] AllKeys { get { throw null; } } + public string? this[int index] { get { throw null; } } + public string? this[string? name] { get { throw null; } set { } } public void Add(System.Collections.Specialized.NameValueCollection c) { } - public virtual void Add(string name, string value) { } + public virtual void Add(string? name, string? value) { } public virtual void Clear() { } public void CopyTo(System.Array dest, int index) { } - public virtual string Get(int index) { throw null; } - public virtual string Get(string name) { throw null; } - public virtual string GetKey(int index) { throw null; } - public virtual string[] GetValues(int index) { throw null; } - public virtual string[] GetValues(string name) { throw null; } + public virtual string? Get(int index) { throw null; } + public virtual string? Get(string? name) { throw null; } + public virtual string? GetKey(int index) { throw null; } + public virtual string[]? GetValues(int index) { throw null; } + public virtual string[]? GetValues(string? name) { throw null; } public bool HasKeys() { throw null; } protected void InvalidateCachedArrays() { } - public virtual void Remove(string name) { } - public virtual void Set(string name, string value) { } + public virtual void Remove(string? name) { } + public virtual void Set(string? name, string? value) { } } public partial class OrderedDictionary : System.Collections.ICollection, System.Collections.IDictionary, System.Collections.IEnumerable, System.Collections.Specialized.IOrderedDictionary, System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializable { public OrderedDictionary() { } - public OrderedDictionary(System.Collections.IEqualityComparer comparer) { } + public OrderedDictionary(System.Collections.IEqualityComparer? comparer) { } public OrderedDictionary(int capacity) { } - public OrderedDictionary(int capacity, System.Collections.IEqualityComparer comparer) { } + public OrderedDictionary(int capacity, System.Collections.IEqualityComparer? comparer) { } protected OrderedDictionary(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { } public int Count { get { throw null; } } public bool IsReadOnly { get { throw null; } } - public object this[int index] { get { throw null; } set { } } - public object this[object key] { get { throw null; } set { } } + public object? this[int index] { get { throw null; } set { } } + public object? this[object key] { get { throw null; } set { } } public System.Collections.ICollection Keys { get { throw null; } } bool System.Collections.ICollection.IsSynchronized { get { throw null; } } object System.Collections.ICollection.SyncRoot { get { throw null; } } bool System.Collections.IDictionary.IsFixedSize { get { throw null; } } public System.Collections.ICollection Values { get { throw null; } } - public void Add(object key, object value) { } + public void Add(object key, object? value) { } public System.Collections.Specialized.OrderedDictionary AsReadOnly() { throw null; } public void Clear() { } public bool Contains(object key) { throw null; } public void CopyTo(System.Array array, int index) { } public virtual System.Collections.IDictionaryEnumerator GetEnumerator() { throw null; } public virtual void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { } - public void Insert(int index, object key, object value) { } - protected virtual void OnDeserialization(object sender) { } + public void Insert(int index, object key, object? value) { } + protected virtual void OnDeserialization(object? sender) { } public void Remove(object key) { } public void RemoveAt(int index) { } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { throw null; } @@ -197,42 +197,42 @@ namespace System.Collections.Specialized public int Count { get { throw null; } } public bool IsReadOnly { get { throw null; } } public bool IsSynchronized { get { throw null; } } - public string this[int index] { get { throw null; } set { } } + public string? this[int index] { get { throw null; } set { } } public object SyncRoot { get { throw null; } } bool System.Collections.IList.IsFixedSize { get { throw null; } } bool System.Collections.IList.IsReadOnly { get { throw null; } } - object System.Collections.IList.this[int index] { get { throw null; } set { } } - public int Add(string value) { throw null; } + object? System.Collections.IList.this[int index] { get { throw null; } set { } } + public int Add(string? value) { throw null; } public void AddRange(string[] value) { } public void Clear() { } - public bool Contains(string value) { throw null; } + public bool Contains(string? value) { throw null; } public void CopyTo(string[] array, int index) { } public System.Collections.Specialized.StringEnumerator GetEnumerator() { throw null; } - public int IndexOf(string value) { throw null; } - public void Insert(int index, string value) { } - public void Remove(string value) { } + public int IndexOf(string? value) { throw null; } + public void Insert(int index, string? value) { } + public void Remove(string? value) { } public void RemoveAt(int index) { } void System.Collections.ICollection.CopyTo(System.Array array, int index) { } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { throw null; } - int System.Collections.IList.Add(object value) { throw null; } - bool System.Collections.IList.Contains(object value) { throw null; } - int System.Collections.IList.IndexOf(object value) { throw null; } - void System.Collections.IList.Insert(int index, object value) { } - void System.Collections.IList.Remove(object value) { } + int System.Collections.IList.Add(object? value) { throw null; } + bool System.Collections.IList.Contains(object? value) { throw null; } + int System.Collections.IList.IndexOf(object? value) { throw null; } + void System.Collections.IList.Insert(int index, object? value) { } + void System.Collections.IList.Remove(object? value) { } } public partial class StringDictionary : System.Collections.IEnumerable { public StringDictionary() { } public virtual int Count { get { throw null; } } public virtual bool IsSynchronized { get { throw null; } } - public virtual string this[string key] { get { throw null; } set { } } + public virtual string? this[string key] { get { throw null; } set { } } public virtual System.Collections.ICollection Keys { get { throw null; } } public virtual object SyncRoot { get { throw null; } } public virtual System.Collections.ICollection Values { get { throw null; } } - public virtual void Add(string key, string value) { } + public virtual void Add(string key, string? value) { } public virtual void Clear() { } public virtual bool ContainsKey(string key) { throw null; } - public virtual bool ContainsValue(string value) { throw null; } + public virtual bool ContainsValue(string? value) { throw null; } public virtual void CopyTo(System.Array array, int index) { } public virtual System.Collections.IEnumerator GetEnumerator() { throw null; } public virtual void Remove(string key) { } @@ -240,7 +240,7 @@ namespace System.Collections.Specialized public partial class StringEnumerator { internal StringEnumerator() { } - public string Current { get { throw null; } } + public string? Current { get { throw null; } } public bool MoveNext() { throw null; } public void Reset() { } } diff --git a/src/libraries/System.Collections.Specialized/ref/System.Collections.Specialized.csproj b/src/libraries/System.Collections.Specialized/ref/System.Collections.Specialized.csproj index f453b4e..be44def 100644 --- a/src/libraries/System.Collections.Specialized/ref/System.Collections.Specialized.csproj +++ b/src/libraries/System.Collections.Specialized/ref/System.Collections.Specialized.csproj @@ -1,6 +1,7 @@ netcoreapp-Debug;netcoreapp-Release;uap-Debug;uap-Release + enable diff --git a/src/libraries/System.Collections.Specialized/src/System.Collections.Specialized.csproj b/src/libraries/System.Collections.Specialized/src/System.Collections.Specialized.csproj index 803be56..f12c952 100644 --- a/src/libraries/System.Collections.Specialized/src/System.Collections.Specialized.csproj +++ b/src/libraries/System.Collections.Specialized/src/System.Collections.Specialized.csproj @@ -1,8 +1,9 @@ - + System.Collections.Specialized System.Collections.Specialized netcoreapp-Debug;netcoreapp-Release;uap-Windows_NT-Debug;uap-Windows_NT-Release + enable diff --git a/src/libraries/System.Collections.Specialized/src/System/Collections/Specialized/BitVector32.cs b/src/libraries/System.Collections.Specialized/src/System/Collections/Specialized/BitVector32.cs index 33f23d5..a71558f 100644 --- a/src/libraries/System.Collections.Specialized/src/System/Collections/Specialized/BitVector32.cs +++ b/src/libraries/System.Collections.Specialized/src/System/Collections/Specialized/BitVector32.cs @@ -185,7 +185,7 @@ namespace System.Collections.Specialized return new Section(CreateMaskFromHighValue(maxValue), offset); } - public override bool Equals(object o) + public override bool Equals(object? o) { if (!(o is BitVector32)) { @@ -254,7 +254,7 @@ namespace System.Collections.Specialized } } - public override bool Equals(object o) + public override bool Equals(object? o) { if (o is Section) return Equals((Section)o); diff --git a/src/libraries/System.Collections.Specialized/src/System/Collections/Specialized/HybridDictionary.cs b/src/libraries/System.Collections.Specialized/src/System/Collections/Specialized/HybridDictionary.cs index abe2856..13aa099 100644 --- a/src/libraries/System.Collections.Specialized/src/System/Collections/Specialized/HybridDictionary.cs +++ b/src/libraries/System.Collections.Specialized/src/System/Collections/Specialized/HybridDictionary.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System.Diagnostics; + namespace System.Collections.Specialized { /// @@ -25,8 +27,8 @@ namespace System.Collections.Specialized private const int FixedSizeCutoverPoint = 6; // Instance variables. This keeps the HybridDictionary very light-weight when empty - private ListDictionary list; // Do not rename (binary serialization) - private Hashtable hashtable; // Do not rename (binary serialization) + private ListDictionary? list; // Do not rename (binary serialization) + private Hashtable? hashtable; // Do not rename (binary serialization) private readonly bool caseInsensitive; // Do not rename (binary serialization) public HybridDictionary() @@ -58,7 +60,7 @@ namespace System.Collections.Specialized } } - public object this[object key] + public object? this[object key] { get { @@ -66,7 +68,7 @@ namespace System.Collections.Specialized // Although we never made the same guarantee for HybridDictionary, // it is still nice to do the same thing here since we have recommended // HybridDictionary as replacement for Hashtable. - ListDictionary cachedList = list; + ListDictionary? cachedList = list; if (hashtable != null) { return hashtable[key]; @@ -97,6 +99,7 @@ namespace System.Collections.Specialized if (list.Count >= CutoverPoint - 1) { ChangeOver(); + Debug.Assert(hashtable != null); hashtable[key] = value; } else @@ -126,6 +129,7 @@ namespace System.Collections.Specialized private void ChangeOver() { + Debug.Assert(list != null); IDictionaryEnumerator en = list.GetEnumerator(); Hashtable newTable; if (caseInsensitive) @@ -152,7 +156,7 @@ namespace System.Collections.Specialized { get { - ListDictionary cachedList = list; + ListDictionary? cachedList = list; if (hashtable != null) { return hashtable.Count; @@ -230,7 +234,7 @@ namespace System.Collections.Specialized } } - public void Add(object key, object value) + public void Add(object key, object? value) { if (hashtable != null) { @@ -246,6 +250,7 @@ namespace System.Collections.Specialized else if (list.Count + 1 >= CutoverPoint) { ChangeOver(); + Debug.Assert(hashtable != null); hashtable.Add(key, value); } else @@ -274,7 +279,7 @@ namespace System.Collections.Specialized public bool Contains(object key) { - ListDictionary cachedList = list; + ListDictionary? cachedList = list; if (hashtable != null) { return hashtable.Contains(key); diff --git a/src/libraries/System.Collections.Specialized/src/System/Collections/Specialized/IOrderedDictionary.cs b/src/libraries/System.Collections.Specialized/src/System/Collections/Specialized/IOrderedDictionary.cs index bee80fa..16dbf91 100644 --- a/src/libraries/System.Collections.Specialized/src/System/Collections/Specialized/IOrderedDictionary.cs +++ b/src/libraries/System.Collections.Specialized/src/System/Collections/Specialized/IOrderedDictionary.cs @@ -19,7 +19,7 @@ namespace System.Collections.Specialized /// /// Returns the object at the given index /// - object this[int index] { get; set; } + object? this[int index] { get; set; } // Returns an IDictionaryEnumerator for this dictionary. new IDictionaryEnumerator GetEnumerator(); @@ -28,7 +28,7 @@ namespace System.Collections.Specialized /// /// Inserts the given object, with the given key, at the given index /// - void Insert(int index, object key, object value); + void Insert(int index, object key, object? value); /// /// Removes the object and key at the given index diff --git a/src/libraries/System.Collections.Specialized/src/System/Collections/Specialized/ListDictionary.cs b/src/libraries/System.Collections.Specialized/src/System/Collections/Specialized/ListDictionary.cs index 74d3b13..ac2a1e9 100644 --- a/src/libraries/System.Collections.Specialized/src/System/Collections/Specialized/ListDictionary.cs +++ b/src/libraries/System.Collections.Specialized/src/System/Collections/Specialized/ListDictionary.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System.Diagnostics; + namespace System.Collections.Specialized { /// @@ -15,21 +17,21 @@ namespace System.Collections.Specialized [System.Runtime.CompilerServices.TypeForwardedFrom("System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")] public class ListDictionary : IDictionary { - private DictionaryNode head; // Do not rename (binary serialization) + private DictionaryNode? head; // Do not rename (binary serialization) private int version; // Do not rename (binary serialization) private int count; // Do not rename (binary serialization) - private readonly IComparer comparer; // Do not rename (binary serialization) + private readonly IComparer? comparer; // Do not rename (binary serialization) public ListDictionary() { } - public ListDictionary(IComparer comparer) + public ListDictionary(IComparer? comparer) { this.comparer = comparer; } - public object this[object key] + public object? this[object key] { get { @@ -37,7 +39,7 @@ namespace System.Collections.Specialized { throw new ArgumentNullException(nameof(key)); } - DictionaryNode node = head; + DictionaryNode? node = head; if (comparer == null) { while (node != null) @@ -71,8 +73,8 @@ namespace System.Collections.Specialized throw new ArgumentNullException(nameof(key)); } version++; - DictionaryNode last = null; - DictionaryNode node; + DictionaryNode? last = null; + DictionaryNode? node; for (node = head; node != null; node = node.next) { object oldKey = node.key; @@ -154,16 +156,16 @@ namespace System.Collections.Specialized } } - public void Add(object key, object value) + public void Add(object key, object? value) { if (key == null) { throw new ArgumentNullException(nameof(key)); } version++; - DictionaryNode last = null; - DictionaryNode node; - for (node = head; node != null; node = node.next) + DictionaryNode? last = null; + + for (DictionaryNode? node = head; node != null; node = node.next) { object oldKey = node.key; if ((comparer == null) ? oldKey.Equals(key) : comparer.Compare(oldKey, key) == 0) @@ -200,7 +202,7 @@ namespace System.Collections.Specialized { throw new ArgumentNullException(nameof(key)); } - for (DictionaryNode node = head; node != null; node = node.next) + for (DictionaryNode? node = head; node != null; node = node.next) { object oldKey = node.key; if ((comparer == null) ? oldKey.Equals(key) : comparer.Compare(oldKey, key) == 0) @@ -221,7 +223,7 @@ namespace System.Collections.Specialized if (array.Length - index < count) throw new ArgumentException(SR.Arg_InsufficientSpace); - for (DictionaryNode node = head; node != null; node = node.next) + for (DictionaryNode? node = head; node != null; node = node.next) { array.SetValue(new DictionaryEntry(node.key, node.value), index); index++; @@ -245,8 +247,8 @@ namespace System.Collections.Specialized throw new ArgumentNullException(nameof(key)); } version++; - DictionaryNode last = null; - DictionaryNode node; + DictionaryNode? last = null; + DictionaryNode? node; for (node = head; node != null; node = node.next) { object oldKey = node.key; @@ -266,6 +268,7 @@ namespace System.Collections.Specialized } else { + Debug.Assert(last != null); last.next = node.next; } count--; @@ -274,7 +277,7 @@ namespace System.Collections.Specialized private class NodeEnumerator : IDictionaryEnumerator { private readonly ListDictionary _list; - private DictionaryNode _current; + private DictionaryNode? _current; private readonly int _version; private bool _start; @@ -319,7 +322,7 @@ namespace System.Collections.Specialized } } - public object Value + public object? Value { get { @@ -378,7 +381,7 @@ namespace System.Collections.Specialized if (index < 0) throw new ArgumentOutOfRangeException(nameof(index), index, SR.ArgumentOutOfRange_NeedNonNegNum_Index); - for (DictionaryNode node = _list.head; node != null; node = node.next) + for (DictionaryNode? node = _list.head; node != null; node = node.next) { array.SetValue(_isKeys ? node.key : node.value, index); index++; @@ -390,7 +393,7 @@ namespace System.Collections.Specialized get { int count = 0; - for (DictionaryNode node = _list.head; node != null; node = node.next) + for (DictionaryNode? node = _list.head; node != null; node = node.next) { count++; } @@ -423,7 +426,7 @@ namespace System.Collections.Specialized private class NodeKeyValueEnumerator : IEnumerator { private readonly ListDictionary _list; - private DictionaryNode _current; + private DictionaryNode? _current; private readonly int _version; private readonly bool _isKeys; private bool _start; @@ -437,7 +440,7 @@ namespace System.Collections.Specialized _current = null; } - public object Current + public object? Current { get { @@ -483,9 +486,9 @@ namespace System.Collections.Specialized [System.Runtime.CompilerServices.TypeForwardedFrom("System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")] public class DictionaryNode { - public object key; // Do not rename (binary serialization) - public object value; // Do not rename (binary serialization) - public DictionaryNode next; // Do not rename (binary serialization) + public object key = null!; // Do not rename (binary serialization) + public object? value; // Do not rename (binary serialization) + public DictionaryNode? next; // Do not rename (binary serialization) } } } diff --git a/src/libraries/System.Collections.Specialized/src/System/Collections/Specialized/NameObjectCollectionBase.cs b/src/libraries/System.Collections.Specialized/src/System/Collections/Specialized/NameObjectCollectionBase.cs index f809126..19de422 100644 --- a/src/libraries/System.Collections.Specialized/src/System/Collections/Specialized/NameObjectCollectionBase.cs +++ b/src/libraries/System.Collections.Specialized/src/System/Collections/Specialized/NameObjectCollectionBase.cs @@ -24,11 +24,11 @@ namespace System.Collections.Specialized public abstract class NameObjectCollectionBase : ICollection, ISerializable, IDeserializationCallback { private bool _readOnly = false; - private ArrayList _entriesArray; + private ArrayList _entriesArray = null!; // initialized in Reset method, called from constructor private IEqualityComparer _keyComparer; - private volatile Hashtable _entriesTable; - private volatile NameObjectEntry _nullKeyEntry; - private KeysCollection _keys; + private volatile Hashtable _entriesTable = null!; // initialized in Reset method, called from constructor + private volatile NameObjectEntry? _nullKeyEntry; + private KeysCollection? _keys; private int _version; private static readonly StringComparer s_defaultComparer = CultureInfo.InvariantCulture.CompareInfo.GetStringComparer(CompareOptions.IgnoreCase); @@ -41,26 +41,26 @@ namespace System.Collections.Specialized { } - protected NameObjectCollectionBase(IEqualityComparer equalityComparer) + protected NameObjectCollectionBase(IEqualityComparer? equalityComparer) { _keyComparer = (equalityComparer == null) ? s_defaultComparer : equalityComparer; Reset(); } - protected NameObjectCollectionBase(int capacity, IEqualityComparer equalityComparer) : this(equalityComparer) + protected NameObjectCollectionBase(int capacity, IEqualityComparer? equalityComparer) : this(equalityComparer) { Reset(capacity); } [Obsolete("Please use NameObjectCollectionBase(IEqualityComparer) instead.")] - protected NameObjectCollectionBase(IHashCodeProvider hashProvider, IComparer comparer) + protected NameObjectCollectionBase(IHashCodeProvider? hashProvider, IComparer? comparer) { _keyComparer = new CompatibleComparer(hashProvider, comparer); Reset(); } [Obsolete("Please use NameObjectCollectionBase(Int32, IEqualityComparer) instead.")] - protected NameObjectCollectionBase(int capacity, IHashCodeProvider hashProvider, IComparer comparer) + protected NameObjectCollectionBase(int capacity, IHashCodeProvider? hashProvider, IComparer? comparer) { _keyComparer = new CompatibleComparer(hashProvider, comparer); Reset(capacity); @@ -87,7 +87,7 @@ namespace System.Collections.Specialized throw new PlatformNotSupportedException(); } - public virtual void OnDeserialization(object sender) + public virtual void OnDeserialization(object? sender) { throw new PlatformNotSupportedException(); } @@ -112,10 +112,10 @@ namespace System.Collections.Specialized _version++; } - private NameObjectEntry FindEntry(string key) + private NameObjectEntry? FindEntry(string? key) { if (key != null) - return (NameObjectEntry)_entriesTable[key]; + return (NameObjectEntry?)_entriesTable[key]; else return _nullKeyEntry; } @@ -159,7 +159,7 @@ namespace System.Collections.Specialized /// Adds an entry with the specified key and value into the /// instance. /// - protected void BaseAdd(string name, object value) + protected void BaseAdd(string? name, object? value) { if (_readOnly) throw new NotSupportedException(SR.CollectionReadOnly); @@ -188,7 +188,7 @@ namespace System.Collections.Specialized /// Removes the entries with the specified key from the /// instance. /// - protected void BaseRemove(string name) + protected void BaseRemove(string? name) { if (_readOnly) throw new NotSupportedException(SR.CollectionReadOnly); @@ -230,7 +230,7 @@ namespace System.Collections.Specialized if (_readOnly) throw new NotSupportedException(SR.CollectionReadOnly); - string key = BaseGetKey(index); + string? key = BaseGetKey(index); if (key != null) { @@ -268,9 +268,9 @@ namespace System.Collections.Specialized /// Gets the value of the first entry with the specified key from /// the instance. /// - protected object BaseGet(string name) + protected object? BaseGet(string? name) { - NameObjectEntry e = FindEntry(name); + NameObjectEntry? e = FindEntry(name); return (e != null) ? e.Value : null; } @@ -280,12 +280,12 @@ namespace System.Collections.Specialized /// into the /// instance. /// - protected void BaseSet(string name, object value) + protected void BaseSet(string? name, object? value) { if (_readOnly) throw new NotSupportedException(SR.CollectionReadOnly); - NameObjectEntry entry = FindEntry(name); + NameObjectEntry? entry = FindEntry(name); if (entry != null) { entry.Value = value; @@ -305,9 +305,9 @@ namespace System.Collections.Specialized /// Gets the value of the entry at the specified index of /// the instance. /// - protected object BaseGet(int index) + protected object? BaseGet(int index) { - NameObjectEntry entry = (NameObjectEntry)_entriesArray[index]; + NameObjectEntry entry = (NameObjectEntry)_entriesArray[index]!; // no null entry added to the array return entry.Value; } @@ -316,9 +316,9 @@ namespace System.Collections.Specialized /// /// instance. /// - protected string BaseGetKey(int index) + protected string? BaseGetKey(int index) { - NameObjectEntry entry = (NameObjectEntry)_entriesArray[index]; + NameObjectEntry entry = (NameObjectEntry)_entriesArray[index]!; return entry.Key; } @@ -326,12 +326,12 @@ namespace System.Collections.Specialized /// Sets the value of the entry at the specified index of /// the instance. /// - protected void BaseSet(int index, object value) + protected void BaseSet(int index, object? value) { if (_readOnly) throw new NotSupportedException(SR.CollectionReadOnly); - NameObjectEntry entry = (NameObjectEntry)_entriesArray[index]; + NameObjectEntry entry = (NameObjectEntry)_entriesArray[index]!; entry.Value = value; _version++; } @@ -400,10 +400,10 @@ namespace System.Collections.Specialized /// Returns a array containing all the keys in the /// instance. /// - protected string[] BaseGetAllKeys() + protected string?[] BaseGetAllKeys() { int n = _entriesArray.Count; - string[] allKeys = new string[n]; + string?[] allKeys = new string[n]; for (int i = 0; i < n; i++) allKeys[i] = BaseGetKey(i); @@ -415,10 +415,10 @@ namespace System.Collections.Specialized /// Returns an array containing all the values in the /// instance. /// - protected object[] BaseGetAllValues() + protected object?[] BaseGetAllValues() { int n = _entriesArray.Count; - object[] allValues = new object[n]; + object?[] allValues = new object[n]; for (int i = 0; i < n; i++) allValues[i] = BaseGet(i); @@ -430,14 +430,14 @@ namespace System.Collections.Specialized /// Returns an array of the specified type containing /// all the values in the instance. /// - protected object[] BaseGetAllValues(Type type) + protected object?[] BaseGetAllValues(Type type) { int n = _entriesArray.Count; if (type == null) { throw new ArgumentNullException(nameof(type)); } - object[] allValues = (object[])Array.CreateInstance(type, n); + object?[] allValues = (object?[])Array.CreateInstance(type, n); for (int i = 0; i < n; i++) { @@ -471,14 +471,14 @@ namespace System.Collections.Specialized internal class NameObjectEntry { - internal NameObjectEntry(string name, object value) + internal NameObjectEntry(string? name, object? value) { Key = name; Value = value; } - internal string Key; - internal object Value; + internal string? Key; + internal object? Value; } // @@ -522,7 +522,7 @@ namespace System.Collections.Specialized _pos = -1; } - public object Current + public object? Current { get { @@ -559,7 +559,7 @@ namespace System.Collections.Specialized /// /// Gets the key at the specified index of the collection. /// - public virtual string Get(int index) + public virtual string? Get(int index) { return _coll.BaseGetKey(index); } @@ -567,7 +567,7 @@ namespace System.Collections.Specialized /// /// Represents the entry at the specified index of the collection. /// - public string this[int index] + public string? this[int index] { get { diff --git a/src/libraries/System.Collections.Specialized/src/System/Collections/Specialized/NameValueCollection.cs b/src/libraries/System.Collections.Specialized/src/System/Collections/Specialized/NameValueCollection.cs index 38c8813..c8793d3 100644 --- a/src/libraries/System.Collections.Specialized/src/System/Collections/Specialized/NameValueCollection.cs +++ b/src/libraries/System.Collections.Specialized/src/System/Collections/Specialized/NameValueCollection.cs @@ -8,6 +8,7 @@ * */ +using System.Diagnostics; using System.Runtime.Serialization; using System.Text; @@ -19,8 +20,8 @@ namespace System.Collections.Specialized /// public class NameValueCollection : NameObjectCollectionBase { - private string[] _all; // Do not rename (binary serialization) - private string[] _allKeys; // Do not rename (binary serialization) + private string?[]? _all; // Do not rename (binary serialization) + private string?[]? _allKeys; // Do not rename (binary serialization) // // Constructors @@ -43,11 +44,11 @@ namespace System.Collections.Specialized public NameValueCollection(NameValueCollection col) : base(col != null ? col.Comparer : null) { - Add(col); + Add(col!); } [Obsolete("Please use NameValueCollection(IEqualityComparer) instead.")] - public NameValueCollection(IHashCodeProvider hashProvider, IComparer comparer) + public NameValueCollection(IHashCodeProvider? hashProvider, IComparer? comparer) : base(hashProvider, comparer) { } @@ -61,11 +62,11 @@ namespace System.Collections.Specialized { } - public NameValueCollection(IEqualityComparer equalityComparer) : base(equalityComparer) + public NameValueCollection(IEqualityComparer? equalityComparer) : base(equalityComparer) { } - public NameValueCollection(int capacity, IEqualityComparer equalityComparer) + public NameValueCollection(int capacity, IEqualityComparer? equalityComparer) : base(capacity, equalityComparer) { } @@ -89,7 +90,7 @@ namespace System.Collections.Specialized } [Obsolete("Please use NameValueCollection(Int32, IEqualityComparer) instead.")] - public NameValueCollection(int capacity, IHashCodeProvider hashProvider, IComparer comparer) + public NameValueCollection(int capacity, IHashCodeProvider? hashProvider, IComparer? comparer) : base(capacity, hashProvider, comparer) { } @@ -111,22 +112,24 @@ namespace System.Collections.Specialized _allKeys = null; } - private static string GetAsOneString(ArrayList list) + private static string? GetAsOneString(ArrayList? list) { int n = (list != null) ? list.Count : 0; if (n == 1) { - return (string)list[0]; + Debug.Assert(list != null); + return (string?)list[0]; } else if (n > 1) { - StringBuilder s = new StringBuilder((string)list[0]); + Debug.Assert(list != null); + StringBuilder s = new StringBuilder((string?)list[0]); for (int i = 1; i < n; i++) { s.Append(','); - s.Append((string)list[i]); + s.Append((string?)list[i]); } return s.ToString(); @@ -137,14 +140,14 @@ namespace System.Collections.Specialized } } - private static string[] GetAsStringArray(ArrayList list) + private static string[]? GetAsStringArray(ArrayList? list) { int n = (list != null) ? list.Count : 0; if (n == 0) return null; string[] array = new string[n]; - list.CopyTo(0, array, 0, n); + list!.CopyTo(0, array, 0, n); return array; } @@ -168,8 +171,8 @@ namespace System.Collections.Specialized for (int i = 0; i < n; i++) { - string key = c.GetKey(i); - string[] values = c.GetValues(i); + string? key = c.GetKey(i); + string[]? values = c.GetValues(i); if (values != null) { @@ -221,7 +224,7 @@ namespace System.Collections.Specialized int n = Count; if (_all == null) { - string[] all = new string[n]; + string?[] all = new string[n]; for (int i = 0; i < n; i++) { all[i] = Get(i); @@ -262,14 +265,14 @@ namespace System.Collections.Specialized /// Adds an entry with the specified name and value into the /// . /// - public virtual void Add(string name, string value) + public virtual void Add(string? name, string? value) { if (IsReadOnly) throw new NotSupportedException(SR.CollectionReadOnly); InvalidateCachedArrays(); - ArrayList values = (ArrayList)BaseGet(name); + ArrayList? values = (ArrayList?)BaseGet(name); if (values == null) { @@ -290,25 +293,25 @@ namespace System.Collections.Specialized /// /// Gets the values associated with the specified key from the combined into one comma-separated list. /// - public virtual string Get(string name) + public virtual string? Get(string? name) { - ArrayList values = (ArrayList)BaseGet(name); + ArrayList? values = (ArrayList?)BaseGet(name); return GetAsOneString(values); } /// /// Gets the values associated with the specified key from the . /// - public virtual string[] GetValues(string name) + public virtual string[]? GetValues(string? name) { - ArrayList values = (ArrayList)BaseGet(name); + ArrayList? values = (ArrayList?)BaseGet(name); return GetAsStringArray(values); } /// /// Adds a value to an entry in the . /// - public virtual void Set(string name, string value) + public virtual void Set(string? name, string? value) { if (IsReadOnly) throw new NotSupportedException(SR.CollectionReadOnly); @@ -323,7 +326,7 @@ namespace System.Collections.Specialized /// /// Removes the entries with the specified key from the instance. /// - public virtual void Remove(string name) + public virtual void Remove(string? name) { InvalidateCachedArrays(); BaseRemove(name); @@ -333,7 +336,7 @@ namespace System.Collections.Specialized /// Represents the entry with the specified key in the /// . /// - public string this[string name] + public string? this[string? name] { get { @@ -355,25 +358,25 @@ namespace System.Collections.Specialized /// Gets the values at the specified index of the combined into one /// comma-separated list. /// - public virtual string Get(int index) + public virtual string? Get(int index) { - ArrayList values = (ArrayList)BaseGet(index); + ArrayList? values = (ArrayList?)BaseGet(index); return GetAsOneString(values); } /// /// Gets the values at the specified index of the . /// - public virtual string[] GetValues(int index) + public virtual string[]? GetValues(int index) { - ArrayList values = (ArrayList)BaseGet(index); + ArrayList? values = (ArrayList?)BaseGet(index); return GetAsStringArray(values); } /// /// Gets the key at the specified index of the . /// - public virtual string GetKey(int index) + public virtual string? GetKey(int index) { return BaseGetKey(index); } @@ -381,7 +384,7 @@ namespace System.Collections.Specialized /// /// Represents the entry at the specified index of the . /// - public string this[int index] + public string? this[int index] { get { @@ -396,7 +399,7 @@ namespace System.Collections.Specialized /// /// Gets all the keys in the . /// - public virtual string[] AllKeys + public virtual string?[] AllKeys { get { diff --git a/src/libraries/System.Collections.Specialized/src/System/Collections/Specialized/OrderedDictionary.cs b/src/libraries/System.Collections.Specialized/src/System/Collections/Specialized/OrderedDictionary.cs index 30c9b96..870f528 100644 --- a/src/libraries/System.Collections.Specialized/src/System/Collections/Specialized/OrderedDictionary.cs +++ b/src/libraries/System.Collections.Specialized/src/System/Collections/Specialized/OrderedDictionary.cs @@ -24,12 +24,12 @@ namespace System.Collections.Specialized [System.Runtime.CompilerServices.TypeForwardedFrom("System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")] public class OrderedDictionary : IOrderedDictionary, ISerializable, IDeserializationCallback { - private ArrayList _objectsArray; - private Hashtable _objectsTable; + private ArrayList? _objectsArray; + private Hashtable? _objectsTable; private int _initialCapacity; - private IEqualityComparer _comparer; + private IEqualityComparer? _comparer; private bool _readOnly; - private readonly SerializationInfo _siInfo; //A temporary variable which we need during deserialization. + private readonly SerializationInfo? _siInfo; //A temporary variable which we need during deserialization. private const string KeyComparerName = "KeyComparer"; // Do not rename (binary serialization) private const string ArrayListName = "ArrayList"; // Do not rename (binary serialization) @@ -44,11 +44,11 @@ namespace System.Collections.Specialized { } - public OrderedDictionary(IEqualityComparer comparer) : this(0, comparer) + public OrderedDictionary(IEqualityComparer? comparer) : this(0, comparer) { } - public OrderedDictionary(int capacity, IEqualityComparer comparer) + public OrderedDictionary(int capacity, IEqualityComparer? comparer) { _initialCapacity = capacity; _comparer = comparer; @@ -128,26 +128,14 @@ namespace System.Collections.Specialized { get { - EnsureObjectsArray(); - return new OrderedDictionaryKeyValueCollection(_objectsArray, true); + ArrayList objectsArray = EnsureObjectsArray(); + return new OrderedDictionaryKeyValueCollection(objectsArray, true); } } - private void EnsureObjectsArray() - { - if (_objectsArray == null) - { - _objectsArray = new ArrayList(_initialCapacity); - } - } + private ArrayList EnsureObjectsArray() => _objectsArray ??= new ArrayList(_initialCapacity); - private void EnsureObjectsTable() - { - if (_objectsTable == null) - { - _objectsTable = new Hashtable(_initialCapacity, _comparer); - } - } + private Hashtable EnsureObjectsTable() => _objectsTable ??= new Hashtable(_initialCapacity, _comparer); /// /// The SyncRoot object. Not used because IsSynchronized is false @@ -157,12 +145,12 @@ namespace System.Collections.Specialized /// /// Gets or sets the object at the specified index /// - public object this[int index] + public object? this[int index] { get { - EnsureObjectsArray(); - return ((DictionaryEntry)_objectsArray[index]).Value; + ArrayList objectsArray = EnsureObjectsArray(); + return ((DictionaryEntry)objectsArray[index]!).Value; } set { @@ -174,18 +162,18 @@ namespace System.Collections.Specialized { throw new ArgumentOutOfRangeException(nameof(index)); } - EnsureObjectsArray(); - EnsureObjectsTable(); - object key = ((DictionaryEntry)_objectsArray[index]).Key; - _objectsArray[index] = new DictionaryEntry(key, value); - _objectsTable[key] = value; + ArrayList objectsArray = EnsureObjectsArray(); + Hashtable objectsTable = EnsureObjectsTable(); + object key = ((DictionaryEntry)objectsArray[index]!).Key; + objectsArray[index] = new DictionaryEntry(key, value); + objectsTable[key] = value; } } /// /// Gets or sets the object with the specified key /// - public object this[object key] + public object? this[object key] { get { @@ -201,12 +189,12 @@ namespace System.Collections.Specialized { throw new NotSupportedException(SR.OrderedDictionary_ReadOnly); } - EnsureObjectsTable(); - if (_objectsTable.Contains(key)) + Hashtable objectsTable = EnsureObjectsTable(); + if (objectsTable.Contains(key)) { - _objectsTable[key] = value; - EnsureObjectsArray(); - _objectsArray[IndexOfKey(key)] = new DictionaryEntry(key, value); + objectsTable[key] = value; + ArrayList objectsArray = EnsureObjectsArray(); + objectsArray[IndexOfKey(key)] = new DictionaryEntry(key, value); } else { @@ -222,24 +210,24 @@ namespace System.Collections.Specialized { get { - EnsureObjectsArray(); - return new OrderedDictionaryKeyValueCollection(_objectsArray, false); + ArrayList objectsArray = EnsureObjectsArray(); + return new OrderedDictionaryKeyValueCollection(objectsArray, false); } } /// /// Adds a new entry to the table with the lowest-available index. /// - public void Add(object key, object value) + public void Add(object key, object? value) { if (_readOnly) { throw new NotSupportedException(SR.OrderedDictionary_ReadOnly); } - EnsureObjectsTable(); - EnsureObjectsArray(); - _objectsTable.Add(key, value); - _objectsArray.Add(new DictionaryEntry(key, value)); + Hashtable objectsTable = EnsureObjectsTable(); + ArrayList objectsArray = EnsureObjectsArray(); + objectsTable.Add(key, value); + objectsArray.Add(new DictionaryEntry(key, value)); } /// @@ -290,8 +278,8 @@ namespace System.Collections.Specialized /// public void CopyTo(Array array, int index) { - EnsureObjectsTable(); - _objectsTable.CopyTo(array, index); + Hashtable objectsTable = EnsureObjectsTable(); + objectsTable.CopyTo(array, index); } private int IndexOfKey(object key) @@ -302,7 +290,7 @@ namespace System.Collections.Specialized } for (int i = 0; i < _objectsArray.Count; i++) { - object o = ((DictionaryEntry)_objectsArray[i]).Key; + object o = ((DictionaryEntry)_objectsArray[i]!).Key; if (_comparer != null) { if (_comparer.Equals(o, key)) @@ -324,7 +312,7 @@ namespace System.Collections.Specialized /// /// Inserts a new object at the given index with the given key. /// - public void Insert(int index, object key, object value) + public void Insert(int index, object key, object? value) { if (_readOnly) { @@ -334,10 +322,10 @@ namespace System.Collections.Specialized { throw new ArgumentOutOfRangeException(nameof(index)); } - EnsureObjectsTable(); - EnsureObjectsArray(); - _objectsTable.Add(key, value); - _objectsArray.Insert(index, new DictionaryEntry(key, value)); + Hashtable objectsTable = EnsureObjectsTable(); + ArrayList objectsArray = EnsureObjectsArray(); + objectsTable.Add(key, value); + objectsArray.Insert(index, new DictionaryEntry(key, value)); } /// @@ -353,11 +341,11 @@ namespace System.Collections.Specialized { throw new ArgumentOutOfRangeException(nameof(index)); } - // The 'index >= Count' check above ensures - // that the '_objectsArray' and '_objectsTable' objects are initialized. - object key = ((DictionaryEntry)_objectsArray[index]).Key; - _objectsArray.RemoveAt(index); - _objectsTable.Remove(key); + Hashtable objectsTable = EnsureObjectsTable(); + ArrayList objectsArray = EnsureObjectsArray(); + object key = ((DictionaryEntry)objectsArray[index]!).Key; + objectsArray.RemoveAt(index); + objectsTable.Remove(key); } /// @@ -380,25 +368,25 @@ namespace System.Collections.Specialized return; } - EnsureObjectsTable(); - EnsureObjectsArray(); - _objectsTable.Remove(key); - _objectsArray.RemoveAt(index); + Hashtable objectsTable = EnsureObjectsTable(); + ArrayList objectsArray = EnsureObjectsArray(); + objectsTable.Remove(key); + objectsArray.RemoveAt(index); } #region IDictionary implementation public virtual IDictionaryEnumerator GetEnumerator() { - EnsureObjectsArray(); - return new OrderedDictionaryEnumerator(_objectsArray, OrderedDictionaryEnumerator.DictionaryEntry); + ArrayList objectsArray = EnsureObjectsArray(); + return new OrderedDictionaryEnumerator(objectsArray, OrderedDictionaryEnumerator.DictionaryEntry); } #endregion #region IEnumerable implementation IEnumerator IEnumerable.GetEnumerator() { - EnsureObjectsArray(); - return new OrderedDictionaryEnumerator(_objectsArray, OrderedDictionaryEnumerator.DictionaryEntry); + ArrayList objectsArray = EnsureObjectsArray(); + return new OrderedDictionaryEnumerator(objectsArray, OrderedDictionaryEnumerator.DictionaryEntry); } #endregion @@ -415,34 +403,34 @@ namespace System.Collections.Specialized info.AddValue(InitCapacityName, _initialCapacity); object[] serArray = new object[Count]; - EnsureObjectsArray(); - _objectsArray.CopyTo(serArray); + ArrayList objectsArray = EnsureObjectsArray(); + objectsArray.CopyTo(serArray); info.AddValue(ArrayListName, serArray); } #endregion #region IDeserializationCallback implementation - void IDeserializationCallback.OnDeserialization(object sender) + void IDeserializationCallback.OnDeserialization(object? sender) { OnDeserialization(sender); } - protected virtual void OnDeserialization(object sender) + protected virtual void OnDeserialization(object? sender) { if (_siInfo == null) { throw new SerializationException(SR.Serialization_InvalidOnDeser); } - _comparer = (IEqualityComparer)_siInfo.GetValue(KeyComparerName, typeof(IEqualityComparer)); + _comparer = (IEqualityComparer?)_siInfo.GetValue(KeyComparerName, typeof(IEqualityComparer)); _readOnly = _siInfo.GetBoolean(ReadOnlyName); _initialCapacity = _siInfo.GetInt32(InitCapacityName); - object[] serArray = (object[])_siInfo.GetValue(ArrayListName, typeof(object[])); + object[]? serArray = (object[]?)_siInfo.GetValue(ArrayListName, typeof(object[])); if (serArray != null) { - EnsureObjectsTable(); - EnsureObjectsArray(); + Hashtable objectsTable = EnsureObjectsTable(); + ArrayList objectsArray = EnsureObjectsArray(); foreach (object o in serArray) { DictionaryEntry entry; @@ -455,8 +443,8 @@ namespace System.Collections.Specialized { throw new SerializationException(SR.OrderedDictionary_SerializationMismatch); } - _objectsArray.Add(entry); - _objectsTable.Add(entry.Key, entry.Value); + objectsArray.Add(entry); + objectsTable.Add(entry.Key, entry.Value); } } } @@ -483,10 +471,11 @@ namespace System.Collections.Specialized /// /// Retrieves the current DictionaryEntry. This is the same as Entry, but not strongly-typed. /// - public object Current + public object? Current { get { + Debug.Assert(_arrayEnumerator.Current != null); if (_objectReturnType == Keys) { return ((DictionaryEntry)_arrayEnumerator.Current).Key; @@ -506,6 +495,7 @@ namespace System.Collections.Specialized { get { + Debug.Assert(_arrayEnumerator.Current != null); return new DictionaryEntry(((DictionaryEntry)_arrayEnumerator.Current).Key, ((DictionaryEntry)_arrayEnumerator.Current).Value); } } @@ -517,6 +507,7 @@ namespace System.Collections.Specialized { get { + Debug.Assert(_arrayEnumerator.Current != null); return ((DictionaryEntry)_arrayEnumerator.Current).Key; } } @@ -524,10 +515,11 @@ namespace System.Collections.Specialized /// /// Retrieves the value of the current DictionaryEntry /// - public object Value + public object? Value { get { + Debug.Assert(_arrayEnumerator.Current != null); return ((DictionaryEntry)_arrayEnumerator.Current).Value; } } @@ -571,8 +563,9 @@ namespace System.Collections.Specialized throw new ArgumentNullException(nameof(array)); if (index < 0) throw new ArgumentOutOfRangeException(nameof(index), index, SR.ArgumentOutOfRange_NeedNonNegNum_Index); - foreach (object o in _objects) + foreach (object? o in _objects) { + Debug.Assert(o != null); array.SetValue(_isKeys ? ((DictionaryEntry)o).Key : ((DictionaryEntry)o).Value, index); index++; } diff --git a/src/libraries/System.Collections.Specialized/src/System/Collections/Specialized/StringCollection.cs b/src/libraries/System.Collections.Specialized/src/System/Collections/Specialized/StringCollection.cs index 4e4e1eb..9fd899d 100644 --- a/src/libraries/System.Collections.Specialized/src/System/Collections/Specialized/StringCollection.cs +++ b/src/libraries/System.Collections.Specialized/src/System/Collections/Specialized/StringCollection.cs @@ -17,11 +17,11 @@ namespace System.Collections.Specialized /// /// Represents the entry at the specified index of the . /// - public string this[int index] + public string? this[int index] { get { - return ((string)data[index]); + return ((string?)data[index]); } set { @@ -62,7 +62,7 @@ namespace System.Collections.Specialized /// Adds a string with the specified value to the /// . /// - public int Add(string value) + public int Add(string? value) { return data.Add(value); } @@ -93,7 +93,7 @@ namespace System.Collections.Specialized /// contains a string with the specified /// value. /// - public bool Contains(string value) + public bool Contains(string? value) { return data.Contains(value); } @@ -120,7 +120,7 @@ namespace System.Collections.Specialized /// Returns the index of the first occurrence of a string in /// the . /// - public int IndexOf(string value) + public int IndexOf(string? value) { return data.IndexOf(value); } @@ -129,7 +129,7 @@ namespace System.Collections.Specialized /// Inserts a string into the at the specified /// index. /// - public void Insert(int index, string value) + public void Insert(int index, string? value) { data.Insert(index, value); } @@ -162,7 +162,7 @@ namespace System.Collections.Specialized /// Removes a specific string from the /// . /// - public void Remove(string value) + public void Remove(string? value) { data.Remove(value); } @@ -186,7 +186,7 @@ namespace System.Collections.Specialized } } - object IList.this[int index] + object? IList.this[int index] { get { @@ -194,34 +194,34 @@ namespace System.Collections.Specialized } set { - this[index] = (string)value; + this[index] = (string?)value; } } - int IList.Add(object value) + int IList.Add(object? value) { - return Add((string)value); + return Add((string?)value); } - bool IList.Contains(object value) + bool IList.Contains(object? value) { - return Contains((string)value); + return Contains((string?)value); } - int IList.IndexOf(object value) + int IList.IndexOf(object? value) { - return IndexOf((string)value); + return IndexOf((string?)value); } - void IList.Insert(int index, object value) + void IList.Insert(int index, object? value) { - Insert(index, (string)value); + Insert(index, (string?)value); } - void IList.Remove(object value) + void IList.Remove(object? value) { - Remove((string)value); + Remove((string?)value); } void ICollection.CopyTo(Array array, int index) @@ -246,11 +246,11 @@ namespace System.Collections.Specialized _baseEnumerator = _temp.GetEnumerator(); } - public string Current + public string? Current { get { - return (string)(_baseEnumerator.Current); + return (string?)(_baseEnumerator.Current); } } diff --git a/src/libraries/System.Collections.Specialized/src/System/Collections/Specialized/StringDictionary.cs b/src/libraries/System.Collections.Specialized/src/System/Collections/Specialized/StringDictionary.cs index bfec498..011f334 100644 --- a/src/libraries/System.Collections.Specialized/src/System/Collections/Specialized/StringDictionary.cs +++ b/src/libraries/System.Collections.Specialized/src/System/Collections/Specialized/StringDictionary.cs @@ -59,7 +59,7 @@ namespace System.Collections.Specialized /// /// Gets or sets the value associated with the specified key. /// - public virtual string this[string key] + public virtual string? this[string key] { get { @@ -68,7 +68,7 @@ namespace System.Collections.Specialized throw new ArgumentNullException(nameof(key)); } - return (string)contents[key.ToLowerInvariant()]; + return (string?)contents[key.ToLowerInvariant()]; } set { @@ -118,7 +118,7 @@ namespace System.Collections.Specialized /// /// Adds an entry with the specified key and value into the StringDictionary. /// - public virtual void Add(string key, string value) + public virtual void Add(string key, string? value) { if (key == null) { @@ -152,7 +152,7 @@ namespace System.Collections.Specialized /// /// Determines if the StringDictionary contains a specific value. /// - public virtual bool ContainsValue(string value) + public virtual bool ContainsValue(string? value) { return contents.ContainsValue(value); } -- 2.7.4