From: buyaa-n Date: Fri, 13 Sep 2019 14:26:27 +0000 (-0700) Subject: Annotate System.Collections.NonGeneric for nullable (dotnet/corefx#41036) X-Git-Tag: submit/tizen/20210909.063632~11031^2~467 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=c0f4ae93f9ccb7c4dabb509fe69c6b27df861a0a;p=platform%2Fupstream%2Fdotnet%2Fruntime.git Annotate System.Collections.NonGeneric for nullable (dotnet/corefx#41036) * Annotate System.Collections.NonGeneric for nullable * Removing unnessecarry constant * Marked CaseInsensitiveHashCodeProvider as Obsolete * Removing NullableAttributes defintion as it was already added * Applying feedback * Revert "Applying feedback" * Applying feedback Commit migrated from https://github.com/dotnet/corefx/commit/001420e80489bf8e456d8bfb9aa22d5a02de0988 --- diff --git a/src/libraries/System.Collections.NonGeneric/ref/System.Collections.NonGeneric.cs b/src/libraries/System.Collections.NonGeneric/ref/System.Collections.NonGeneric.cs index 2c6bd91..e51e872 100644 --- a/src/libraries/System.Collections.NonGeneric/ref/System.Collections.NonGeneric.cs +++ b/src/libraries/System.Collections.NonGeneric/ref/System.Collections.NonGeneric.cs @@ -13,7 +13,7 @@ namespace System.Collections public CaseInsensitiveComparer(System.Globalization.CultureInfo culture) { } public static System.Collections.CaseInsensitiveComparer Default { get { throw null; } } public static System.Collections.CaseInsensitiveComparer DefaultInvariant { get { throw null; } } - public int Compare(object a, object b) { throw null; } + public int Compare(object? a, object? b) { throw null; } } [System.ObsoleteAttribute("Please use StringComparer instead.")] public partial class CaseInsensitiveHashCodeProvider : System.Collections.IHashCodeProvider @@ -36,25 +36,25 @@ namespace System.Collections object System.Collections.ICollection.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 { } } + object? System.Collections.IList.this[int index] { get { throw null; } set { } } public void Clear() { } public System.Collections.IEnumerator GetEnumerator() { throw null; } protected virtual void OnClear() { } protected virtual void OnClearComplete() { } - protected virtual void OnInsert(int index, object value) { } - protected virtual void OnInsertComplete(int index, object value) { } - protected virtual void OnRemove(int index, object value) { } - protected virtual void OnRemoveComplete(int index, object value) { } - protected virtual void OnSet(int index, object oldValue, object newValue) { } - protected virtual void OnSetComplete(int index, object oldValue, object newValue) { } + protected virtual void OnInsert(int index, object? value) { } + protected virtual void OnInsertComplete(int index, object? value) { } + protected virtual void OnRemove(int index, object? value) { } + protected virtual void OnRemoveComplete(int index, object? value) { } + protected virtual void OnSet(int index, object? oldValue, object? newValue) { } + protected virtual void OnSetComplete(int index, object? oldValue, object? newValue) { } protected virtual void OnValidate(object value) { } public void RemoveAt(int index) { } void System.Collections.ICollection.CopyTo(System.Array array, int index) { } - 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 abstract partial class DictionaryBase : System.Collections.ICollection, System.Collections.IDictionary, System.Collections.IEnumerable { @@ -66,7 +66,7 @@ namespace System.Collections object System.Collections.ICollection.SyncRoot { get { throw null; } } bool System.Collections.IDictionary.IsFixedSize { get { throw null; } } bool System.Collections.IDictionary.IsReadOnly { get { throw null; } } - object System.Collections.IDictionary.this[object key] { get { throw null; } set { } } + object? System.Collections.IDictionary.this[object key] { get { throw null; } set { } } System.Collections.ICollection System.Collections.IDictionary.Keys { get { throw null; } } System.Collections.ICollection System.Collections.IDictionary.Values { get { throw null; } } public void Clear() { } @@ -74,15 +74,15 @@ namespace System.Collections public System.Collections.IDictionaryEnumerator GetEnumerator() { throw null; } protected virtual void OnClear() { } protected virtual void OnClearComplete() { } - protected virtual object OnGet(object key, object currentValue) { throw null; } - protected virtual void OnInsert(object key, object value) { } - protected virtual void OnInsertComplete(object key, object value) { } - protected virtual void OnRemove(object key, object value) { } - protected virtual void OnRemoveComplete(object key, object value) { } - protected virtual void OnSet(object key, object oldValue, object newValue) { } - protected virtual void OnSetComplete(object key, object oldValue, object newValue) { } - protected virtual void OnValidate(object key, object value) { } - void System.Collections.IDictionary.Add(object key, object value) { } + protected virtual object? OnGet(object key, object? currentValue) { throw null; } + protected virtual void OnInsert(object key, object? value) { } + protected virtual void OnInsertComplete(object key, object? value) { } + protected virtual void OnRemove(object key, object? value) { } + protected virtual void OnRemoveComplete(object key, object? value) { } + protected virtual void OnSet(object key, object? oldValue, object? newValue) { } + protected virtual void OnSetComplete(object key, object? oldValue, object? newValue) { } + protected virtual void OnValidate(object key, object? value) { } + void System.Collections.IDictionary.Add(object key, object? value) { } bool System.Collections.IDictionary.Contains(object key) { throw null; } void System.Collections.IDictionary.Remove(object key) { } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { throw null; } @@ -98,14 +98,14 @@ namespace System.Collections public virtual object SyncRoot { get { throw null; } } public virtual void Clear() { } public virtual object Clone() { throw null; } - public virtual bool Contains(object obj) { throw null; } + public virtual bool Contains(object? obj) { throw null; } public virtual void CopyTo(System.Array array, int index) { } - public virtual object Dequeue() { throw null; } - public virtual void Enqueue(object obj) { } + public virtual object? Dequeue() { throw null; } + public virtual void Enqueue(object? obj) { } public virtual System.Collections.IEnumerator GetEnumerator() { throw null; } - public virtual object Peek() { throw null; } + public virtual object? Peek() { throw null; } public static System.Collections.Queue Synchronized(System.Collections.Queue queue) { throw null; } - public virtual object[] ToArray() { throw null; } + public virtual object?[] ToArray() { throw null; } public virtual void TrimToSize() { } } public abstract partial class ReadOnlyCollectionBase : System.Collections.ICollection, System.Collections.IEnumerable @@ -121,37 +121,37 @@ namespace System.Collections public partial class SortedList : System.Collections.ICollection, System.Collections.IDictionary, System.Collections.IEnumerable, System.ICloneable { public SortedList() { } - public SortedList(System.Collections.IComparer comparer) { } - public SortedList(System.Collections.IComparer comparer, int capacity) { } + public SortedList(System.Collections.IComparer? comparer) { } + public SortedList(System.Collections.IComparer? comparer, int capacity) { } public SortedList(System.Collections.IDictionary d) { } - public SortedList(System.Collections.IDictionary d, System.Collections.IComparer comparer) { } + public SortedList(System.Collections.IDictionary d, System.Collections.IComparer? comparer) { } public SortedList(int initialCapacity) { } public virtual int Capacity { get { throw null; } set { } } public virtual int Count { get { throw null; } } public virtual bool IsFixedSize { get { throw null; } } public virtual bool IsReadOnly { get { throw null; } } public virtual bool IsSynchronized { get { throw null; } } - public virtual object this[object key] { get { throw null; } set { } } + public virtual object? this[object 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(object key, object value) { } + public virtual void Add(object key, object? value) { } public virtual void Clear() { } public virtual object Clone() { throw null; } public virtual bool Contains(object key) { throw null; } public virtual bool ContainsKey(object key) { throw null; } - public virtual bool ContainsValue(object value) { throw null; } + public virtual bool ContainsValue(object? value) { throw null; } public virtual void CopyTo(System.Array array, int arrayIndex) { } - public virtual object GetByIndex(int index) { throw null; } + public virtual object? GetByIndex(int index) { throw null; } public virtual System.Collections.IDictionaryEnumerator GetEnumerator() { throw null; } public virtual object GetKey(int index) { throw null; } public virtual System.Collections.IList GetKeyList() { throw null; } public virtual System.Collections.IList GetValueList() { throw null; } public virtual int IndexOfKey(object key) { throw null; } - public virtual int IndexOfValue(object value) { throw null; } + public virtual int IndexOfValue(object? value) { throw null; } public virtual void Remove(object key) { } public virtual void RemoveAt(int index) { } - public virtual void SetByIndex(int index, object value) { } + public virtual void SetByIndex(int index, object? value) { } public static System.Collections.SortedList Synchronized(System.Collections.SortedList list) { throw null; } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { throw null; } public virtual void TrimToSize() { } @@ -166,14 +166,14 @@ namespace System.Collections public virtual object SyncRoot { get { throw null; } } public virtual void Clear() { } public virtual object Clone() { throw null; } - public virtual bool Contains(object obj) { throw null; } + public virtual bool Contains(object? obj) { throw null; } public virtual void CopyTo(System.Array array, int index) { } public virtual System.Collections.IEnumerator GetEnumerator() { throw null; } - public virtual object Peek() { throw null; } - public virtual object Pop() { throw null; } - public virtual void Push(object obj) { } + public virtual object? Peek() { throw null; } + public virtual object? Pop() { throw null; } + public virtual void Push(object? obj) { } public static System.Collections.Stack Synchronized(System.Collections.Stack stack) { throw null; } - public virtual object[] ToArray() { throw null; } + public virtual object?[] ToArray() { throw null; } } } namespace System.Collections.Specialized diff --git a/src/libraries/System.Collections.NonGeneric/ref/System.Collections.NonGeneric.csproj b/src/libraries/System.Collections.NonGeneric/ref/System.Collections.NonGeneric.csproj index 4ced3d5..0f04a6a 100644 --- a/src/libraries/System.Collections.NonGeneric/ref/System.Collections.NonGeneric.csproj +++ b/src/libraries/System.Collections.NonGeneric/ref/System.Collections.NonGeneric.csproj @@ -1,6 +1,7 @@ netcoreapp-Debug;netcoreapp-Release;uap-Debug;uap-Release + enable diff --git a/src/libraries/System.Collections.NonGeneric/src/System.Collections.NonGeneric.csproj b/src/libraries/System.Collections.NonGeneric/src/System.Collections.NonGeneric.csproj index ff1a5db..e238dfd 100644 --- a/src/libraries/System.Collections.NonGeneric/src/System.Collections.NonGeneric.csproj +++ b/src/libraries/System.Collections.NonGeneric/src/System.Collections.NonGeneric.csproj @@ -1,9 +1,10 @@ - + System.Collections.NonGeneric System.Collections.NonGeneric true netcoreapp-Debug;netcoreapp-Release;uap-Windows_NT-Debug;uap-Windows_NT-Release + enable diff --git a/src/libraries/System.Collections.NonGeneric/src/System/Collections/CaseInsensitiveComparer.cs b/src/libraries/System.Collections.NonGeneric/src/System/Collections/CaseInsensitiveComparer.cs index ba55861..413e056 100644 --- a/src/libraries/System.Collections.NonGeneric/src/System/Collections/CaseInsensitiveComparer.cs +++ b/src/libraries/System.Collections.NonGeneric/src/System/Collections/CaseInsensitiveComparer.cs @@ -18,7 +18,7 @@ namespace System.Collections public class CaseInsensitiveComparer : IComparer { private readonly CompareInfo _compareInfo; - private static volatile CaseInsensitiveComparer s_InvariantCaseInsensitiveComparer; + private static volatile CaseInsensitiveComparer? s_InvariantCaseInsensitiveComparer; public CaseInsensitiveComparer() { @@ -61,10 +61,10 @@ namespace System.Collections // If a doesn't implement IComparable and b does, -(b.CompareTo(a)) is returned. // Otherwise an exception is thrown. // - public int Compare(object a, object b) + public int Compare(object? a, object? b) { - string sa = a as string; - string sb = b as string; + string? sa = a as string; + string? sb = b as string; if (sa != null && sb != null) return _compareInfo.Compare(sa, sb, CompareOptions.IgnoreCase); else diff --git a/src/libraries/System.Collections.NonGeneric/src/System/Collections/CaseInsensitiveHashCodeProvider.cs b/src/libraries/System.Collections.NonGeneric/src/System/Collections/CaseInsensitiveHashCodeProvider.cs index fc97729..6470d80 100644 --- a/src/libraries/System.Collections.NonGeneric/src/System/Collections/CaseInsensitiveHashCodeProvider.cs +++ b/src/libraries/System.Collections.NonGeneric/src/System/Collections/CaseInsensitiveHashCodeProvider.cs @@ -13,7 +13,7 @@ namespace System.Collections [Obsolete("Please use StringComparer instead.")] public class CaseInsensitiveHashCodeProvider : IHashCodeProvider { - private static volatile CaseInsensitiveHashCodeProvider s_invariantCaseInsensitiveHashCodeProvider = null; + private static volatile CaseInsensitiveHashCodeProvider? s_invariantCaseInsensitiveHashCodeProvider = null; private readonly CompareInfo _compareInfo; public CaseInsensitiveHashCodeProvider() @@ -42,7 +42,7 @@ namespace System.Collections throw new ArgumentNullException(nameof(obj)); } - string s = obj as string; + string? s = obj as string; return s != null ? _compareInfo.GetHashCode(s, CompareOptions.IgnoreCase) : obj.GetHashCode(); diff --git a/src/libraries/System.Collections.NonGeneric/src/System/Collections/CollectionBase.cs b/src/libraries/System.Collections.NonGeneric/src/System/Collections/CollectionBase.cs index 615d76d..8e0bfb5 100644 --- a/src/libraries/System.Collections.NonGeneric/src/System/Collections/CollectionBase.cs +++ b/src/libraries/System.Collections.NonGeneric/src/System/Collections/CollectionBase.cs @@ -73,8 +73,8 @@ namespace System.Collections { if (index < 0 || index >= Count) throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_Index); - object temp = InnerList[index]; - OnValidate(temp); + object? temp = InnerList[index]; + OnValidate(temp!); OnRemove(index, temp); InnerList.RemoveAt(index); try @@ -113,7 +113,7 @@ namespace System.Collections InnerList.CopyTo(array, index); } - object IList.this[int index] + object? IList.this[int index] { get { @@ -125,8 +125,8 @@ namespace System.Collections { if (index < 0 || index >= Count) throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_Index); - OnValidate(value); - object temp = InnerList[index]; + OnValidate(value!); + object? temp = InnerList[index]; OnSet(index, temp, value); InnerList[index] = value; try @@ -141,14 +141,14 @@ namespace System.Collections } } - bool IList.Contains(object value) + bool IList.Contains(object? value) { return InnerList.Contains(value); } - int IList.Add(object value) + int IList.Add(object? value) { - OnValidate(value); + OnValidate(value!); OnInsert(InnerList.Count, value); int index = InnerList.Add(value); try @@ -164,9 +164,9 @@ namespace System.Collections } - void IList.Remove(object value) + void IList.Remove(object? value) { - OnValidate(value); + OnValidate(value!); int index = InnerList.IndexOf(value); if (index < 0) throw new ArgumentException(SR.Arg_RemoveArgNotFound); OnRemove(index, value); @@ -182,16 +182,16 @@ namespace System.Collections } } - int IList.IndexOf(object value) + int IList.IndexOf(object? value) { return InnerList.IndexOf(value); } - void IList.Insert(int index, object value) + void IList.Insert(int index, object? value) { if (index < 0 || index > Count) throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_Index); - OnValidate(value); + OnValidate(value!); OnInsert(index, value); InnerList.Insert(index, value); try @@ -210,11 +210,11 @@ namespace System.Collections return InnerList.GetEnumerator(); } - protected virtual void OnSet(int index, object oldValue, object newValue) + protected virtual void OnSet(int index, object? oldValue, object? newValue) { } - protected virtual void OnInsert(int index, object value) + protected virtual void OnInsert(int index, object? value) { } @@ -222,7 +222,7 @@ namespace System.Collections { } - protected virtual void OnRemove(int index, object value) + protected virtual void OnRemove(int index, object? value) { } @@ -231,11 +231,11 @@ namespace System.Collections if (value == null) throw new ArgumentNullException(nameof(value)); } - protected virtual void OnSetComplete(int index, object oldValue, object newValue) + protected virtual void OnSetComplete(int index, object? oldValue, object? newValue) { } - protected virtual void OnInsertComplete(int index, object value) + protected virtual void OnInsertComplete(int index, object? value) { } @@ -243,7 +243,7 @@ namespace System.Collections { } - protected virtual void OnRemoveComplete(int index, object value) + protected virtual void OnRemoveComplete(int index, object? value) { } } diff --git a/src/libraries/System.Collections.NonGeneric/src/System/Collections/DictionaryBase.cs b/src/libraries/System.Collections.NonGeneric/src/System/Collections/DictionaryBase.cs index e2b6b5b..690ec97 100644 --- a/src/libraries/System.Collections.NonGeneric/src/System/Collections/DictionaryBase.cs +++ b/src/libraries/System.Collections.NonGeneric/src/System/Collections/DictionaryBase.cs @@ -17,7 +17,7 @@ namespace System.Collections // Useful base class for typed read/write collections where items derive from object public abstract class DictionaryBase : IDictionary { - private Hashtable _hashtable; + private Hashtable? _hashtable; protected Hashtable InnerHashtable { @@ -75,11 +75,11 @@ namespace System.Collections InnerHashtable.CopyTo(array, index); } - object IDictionary.this[object key] + object? IDictionary.this[object key] { get { - object currentValue = InnerHashtable[key]; + object? currentValue = InnerHashtable[key]; OnGet(key, currentValue); return currentValue; } @@ -87,7 +87,7 @@ namespace System.Collections { OnValidate(key, value); bool keyExists = true; - object temp = InnerHashtable[key]; + object? temp = InnerHashtable[key]; if (temp == null) { keyExists = InnerHashtable.Contains(key); @@ -119,7 +119,7 @@ namespace System.Collections return InnerHashtable.Contains(key); } - void IDictionary.Add(object key, object value) + void IDictionary.Add(object key, object? value) { OnValidate(key, value); OnInsert(key, value); @@ -146,7 +146,7 @@ namespace System.Collections { if (InnerHashtable.Contains(key)) { - object temp = InnerHashtable[key]; + object? temp = InnerHashtable[key]; OnValidate(key, temp); OnRemove(key, temp); @@ -173,16 +173,16 @@ namespace System.Collections return InnerHashtable.GetEnumerator(); } - protected virtual object OnGet(object key, object currentValue) + protected virtual object? OnGet(object key, object? currentValue) { return currentValue; } - protected virtual void OnSet(object key, object oldValue, object newValue) + protected virtual void OnSet(object key, object? oldValue, object? newValue) { } - protected virtual void OnInsert(object key, object value) + protected virtual void OnInsert(object key, object? value) { } @@ -190,19 +190,19 @@ namespace System.Collections { } - protected virtual void OnRemove(object key, object value) + protected virtual void OnRemove(object key, object? value) { } - protected virtual void OnValidate(object key, object value) + protected virtual void OnValidate(object key, object? value) { } - protected virtual void OnSetComplete(object key, object oldValue, object newValue) + protected virtual void OnSetComplete(object key, object? oldValue, object? newValue) { } - protected virtual void OnInsertComplete(object key, object value) + protected virtual void OnInsertComplete(object key, object? value) { } @@ -210,7 +210,7 @@ namespace System.Collections { } - protected virtual void OnRemoveComplete(object key, object value) + protected virtual void OnRemoveComplete(object key, object? value) { } } diff --git a/src/libraries/System.Collections.NonGeneric/src/System/Collections/Queue.cs b/src/libraries/System.Collections.NonGeneric/src/System/Collections/Queue.cs index e156b2b..5dbb967 100644 --- a/src/libraries/System.Collections.NonGeneric/src/System/Collections/Queue.cs +++ b/src/libraries/System.Collections.NonGeneric/src/System/Collections/Queue.cs @@ -23,7 +23,7 @@ namespace System.Collections [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")] public class Queue : ICollection, ICloneable { - private object[] _array; // Do not rename (binary serialization) + private object?[] _array; // Do not rename (binary serialization) private int _head; // First valid element in the queue. Do not rename (binary serialization) private int _tail; // Last valid element in the queue. Do not rename (binary serialization) private int _size; // Number of elements. Do not rename (binary serialization) @@ -155,7 +155,7 @@ namespace System.Collections // Adds obj to the tail of the queue. // - public virtual void Enqueue(object obj) + public virtual void Enqueue(object? obj) { if (_size == _array.Length) { @@ -183,12 +183,12 @@ namespace System.Collections // Removes the object at the head of the queue and returns it. If the queue // is empty, this method simply returns null. - public virtual object Dequeue() + public virtual object? Dequeue() { if (Count == 0) throw new InvalidOperationException(SR.InvalidOperation_EmptyQueue); - object removed = _array[_head]; + object? removed = _array[_head]; _array[_head] = null; _head = (_head + 1) % _array.Length; _size--; @@ -199,7 +199,7 @@ namespace System.Collections // Returns the object at the head of the queue. The object remains in the // queue. If the queue is empty, this method throws an // InvalidOperationException. - public virtual object Peek() + public virtual object? Peek() { if (Count == 0) throw new InvalidOperationException(SR.InvalidOperation_EmptyQueue); @@ -222,8 +222,7 @@ namespace System.Collections // Returns true if the queue contains at least one object equal to obj. // Equality is determined using obj.Equals(). // - // Exceptions: ArgumentNullException if obj == null. - public virtual bool Contains(object obj) + public virtual bool Contains(object? obj) { int index = _head; int count = _size; @@ -235,7 +234,7 @@ namespace System.Collections if (_array[index] == null) return true; } - else if (_array[index] != null && _array[index].Equals(obj)) + else if (_array[index] != null && _array[index]!.Equals(obj)) { return true; } @@ -245,7 +244,7 @@ namespace System.Collections return false; } - internal object GetElement(int i) + internal object? GetElement(int i) { return _array[(_head + i) % _array.Length]; } @@ -254,7 +253,7 @@ namespace System.Collections // objects in the Queue, or an empty array if the queue is empty. // The order of elements in the array is first in to last in, the same // order produced by successive calls to Dequeue. - public virtual object[] ToArray() + public virtual object?[] ToArray() { if (_size == 0) return Array.Empty(); @@ -356,7 +355,7 @@ namespace System.Collections } } - public override bool Contains(object obj) + public override bool Contains(object? obj) { lock (_root) { @@ -372,7 +371,7 @@ namespace System.Collections } } - public override void Enqueue(object value) + public override void Enqueue(object? value) { lock (_root) { @@ -380,7 +379,7 @@ namespace System.Collections } } - public override object Dequeue() + public override object? Dequeue() { lock (_root) { @@ -396,7 +395,7 @@ namespace System.Collections } } - public override object Peek() + public override object? Peek() { lock (_root) { @@ -404,7 +403,7 @@ namespace System.Collections } } - public override object[] ToArray() + public override object?[] ToArray() { lock (_root) { @@ -430,7 +429,7 @@ namespace System.Collections private readonly Queue _q; private int _index; private readonly int _version; - private object _currentElement; + private object? _currentElement; internal QueueEnumerator(Queue q) { @@ -462,7 +461,7 @@ namespace System.Collections return true; } - public virtual object Current + public virtual object? Current { get { @@ -501,7 +500,7 @@ namespace System.Collections } [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)] - public object[] Items + public object?[] Items { get { diff --git a/src/libraries/System.Collections.NonGeneric/src/System/Collections/ReadOnlyCollectionBase.cs b/src/libraries/System.Collections.NonGeneric/src/System/Collections/ReadOnlyCollectionBase.cs index 097013e..2818083 100644 --- a/src/libraries/System.Collections.NonGeneric/src/System/Collections/ReadOnlyCollectionBase.cs +++ b/src/libraries/System.Collections.NonGeneric/src/System/Collections/ReadOnlyCollectionBase.cs @@ -17,7 +17,7 @@ namespace System.Collections // Useful base class for typed readonly collections where items derive from object public abstract class ReadOnlyCollectionBase : ICollection { - private ArrayList _list; + private ArrayList? _list; protected ArrayList InnerList { diff --git a/src/libraries/System.Collections.NonGeneric/src/System/Collections/SortedList.cs b/src/libraries/System.Collections.NonGeneric/src/System/Collections/SortedList.cs index 776766b..dc929df 100644 --- a/src/libraries/System.Collections.NonGeneric/src/System/Collections/SortedList.cs +++ b/src/libraries/System.Collections.NonGeneric/src/System/Collections/SortedList.cs @@ -64,12 +64,12 @@ namespace System.Collections public class SortedList : IDictionary, ICloneable { private object[] keys; // Do not rename (binary serialization) - private object[] values; // Do not rename (binary serialization) + private object?[] values; // Do not rename (binary serialization) private int _size; // Do not rename (binary serialization) private int version; // Do not rename (binary serialization) private IComparer comparer; // Do not rename (binary serialization) - private KeyList keyList; // Do not rename (binary serialization) - private ValueList valueList; // Do not rename (binary serialization) + private KeyList? keyList; // Do not rename (binary serialization) + private ValueList? valueList; // Do not rename (binary serialization) // Copy of Array.MaxArrayLength internal const int MaxArrayLength = 0X7FEFFFFF; @@ -82,11 +82,6 @@ namespace System.Collections // all entries added to the sorted list. public SortedList() { - Init(); - } - - private void Init() - { keys = Array.Empty(); values = Array.Empty(); _size = 0; @@ -120,7 +115,7 @@ namespace System.Collections // interface, which in that case must be implemented by the keys of all // entries added to the sorted list. // - public SortedList(IComparer comparer) + public SortedList(IComparer? comparer) : this() { if (comparer != null) this.comparer = comparer; @@ -135,7 +130,7 @@ namespace System.Collections // the IComparable interface, which in that case must be implemented // by the keys of all entries added to the sorted list. // - public SortedList(IComparer comparer, int capacity) + public SortedList(IComparer? comparer, int capacity) : this(comparer) { Capacity = capacity; @@ -160,7 +155,7 @@ namespace System.Collections // by the keys of all entries in the given dictionary as well as keys // subsequently added to the sorted list. // - public SortedList(IDictionary d, IComparer comparer) + public SortedList(IDictionary d, IComparer? comparer) : this(comparer, (d != null ? d.Count : 0)) { if (d == null) @@ -182,7 +177,7 @@ namespace System.Collections // Adds an entry with the given key and value to this sorted list. An // ArgumentException is thrown if the key is already present in the sorted list. // - public virtual void Add(object key, object value) + public virtual void Add(object key, object? value) { if (key == null) throw new ArgumentNullException(nameof(key), SR.ArgumentNull_Key); @@ -337,7 +332,7 @@ namespace System.Collections // search and is substantially slower than the Contains // method. // - public virtual bool ContainsValue(object value) + public virtual bool ContainsValue(object? value) { return IndexOfValue(value) >= 0; } @@ -391,7 +386,7 @@ namespace System.Collections // Returns the value of the entry at the given index. // - public virtual object GetByIndex(int index) + public virtual object? GetByIndex(int index) { if (index < 0 || index >= Count) throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_Index); @@ -464,7 +459,7 @@ namespace System.Collections // Returns the value associated with the given key. If an entry with the // given key is not found, the returned value is null. // - public virtual object this[object key] + public virtual object? this[object key] { get { @@ -507,13 +502,13 @@ namespace System.Collections // size of this sorted list. The elements of the list are compared to the // given value using the Object.Equals method. // - public virtual int IndexOfValue(object value) + public virtual int IndexOfValue(object? value) { return Array.IndexOf(values, value, 0, _size); } // Inserts an entry with a given key and value at a given index. - private void Insert(int index, object key, object value) + private void Insert(int index, object key, object? value) { if (_size == keys.Length) EnsureCapacity(_size + 1); if (index < _size) @@ -539,7 +534,7 @@ namespace System.Collections Array.Copy(keys, index + 1, keys, index, _size - index); Array.Copy(values, index + 1, values, index, _size - index); } - keys[_size] = null; + keys[_size] = null!; values[_size] = null; version++; } @@ -558,7 +553,7 @@ namespace System.Collections // Sets the value at an index to a given value. The previous value of // the given entry is overwritten. // - public virtual void SetByIndex(int index, object value) + public virtual void SetByIndex(int index, object? value) { if (index < 0 || index >= Count) throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_Index); values[index] = value; @@ -627,7 +622,7 @@ namespace System.Collections get { return true; } } - public override object this[object key] + public override object? this[object key] { get { @@ -645,7 +640,7 @@ namespace System.Collections } } - public override void Add(object key, object value) + public override void Add(object key, object? value) { lock (_root) { @@ -690,7 +685,7 @@ namespace System.Collections } } - public override bool ContainsValue(object key) + public override bool ContainsValue(object? key) { lock (_root) { @@ -706,7 +701,7 @@ namespace System.Collections } } - public override object GetByIndex(int index) + public override object? GetByIndex(int index) { lock (_root) { @@ -757,7 +752,7 @@ namespace System.Collections } } - public override int IndexOfValue(object value) + public override int IndexOfValue(object? value) { lock (_root) { @@ -781,7 +776,7 @@ namespace System.Collections } } - public override void SetByIndex(int index, object value) + public override void SetByIndex(int index, object? value) { lock (_root) { @@ -806,8 +801,8 @@ namespace System.Collections private class SortedListEnumerator : IDictionaryEnumerator, ICloneable { private readonly SortedList _sortedList; - private object _key; - private object _value; + private object _key = null!; + private object? _value; private int _index; private readonly int _startIndex; // Store for Reset. private readonly int _endIndex; @@ -854,7 +849,7 @@ namespace System.Collections _current = true; return true; } - _key = null; + _key = null!; _value = null; _current = false; return false; @@ -870,7 +865,7 @@ namespace System.Collections } } - public virtual object Current + public virtual object? Current { get { @@ -885,7 +880,7 @@ namespace System.Collections } } - public virtual object Value + public virtual object? Value { get { @@ -900,7 +895,7 @@ namespace System.Collections if (_version != _sortedList.version) throw new InvalidOperationException(SR.InvalidOperation_EnumFailedVersion); _index = _startIndex; _current = false; - _key = null; + _key = null!; _value = null; } } @@ -941,7 +936,7 @@ namespace System.Collections get { return sortedList.SyncRoot; } } - public virtual int Add(object key) + public virtual int Add(object? key) { throw new NotSupportedException(SR.NotSupported_SortedListNestedWrite); // return 0; // suppress compiler warning @@ -952,9 +947,9 @@ namespace System.Collections throw new NotSupportedException(SR.NotSupported_SortedListNestedWrite); } - public virtual bool Contains(object key) + public virtual bool Contains(object? key) { - return sortedList.Contains(key); + return sortedList.Contains(key!); } public virtual void CopyTo(Array array, int arrayIndex) @@ -963,15 +958,15 @@ namespace System.Collections throw new ArgumentException(SR.Arg_RankMultiDimNotSupported, nameof(array)); // defer error checking to Array.Copy - Array.Copy(sortedList.keys, 0, array, arrayIndex, sortedList.Count); + Array.Copy(sortedList.keys, 0, array!, arrayIndex, sortedList.Count); } - public virtual void Insert(int index, object value) + public virtual void Insert(int index, object? value) { throw new NotSupportedException(SR.NotSupported_SortedListNestedWrite); } - public virtual object this[int index] + public virtual object? this[int index] { get { @@ -988,7 +983,7 @@ namespace System.Collections return new SortedListEnumerator(sortedList, 0, sortedList.Count, SortedListEnumerator.Keys); } - public virtual int IndexOf(object key) + public virtual int IndexOf(object? key) { if (key == null) throw new ArgumentNullException(nameof(key), SR.ArgumentNull_Key); @@ -999,7 +994,7 @@ namespace System.Collections return -1; } - public virtual void Remove(object key) + public virtual void Remove(object? key) { throw new NotSupportedException(SR.NotSupported_SortedListNestedWrite); } @@ -1046,7 +1041,7 @@ namespace System.Collections get { return sortedList.SyncRoot; } } - public virtual int Add(object key) + public virtual int Add(object? key) { throw new NotSupportedException(SR.NotSupported_SortedListNestedWrite); } @@ -1056,7 +1051,7 @@ namespace System.Collections throw new NotSupportedException(SR.NotSupported_SortedListNestedWrite); } - public virtual bool Contains(object value) + public virtual bool Contains(object? value) { return sortedList.ContainsValue(value); } @@ -1067,15 +1062,15 @@ namespace System.Collections throw new ArgumentException(SR.Arg_RankMultiDimNotSupported, nameof(array)); // defer error checking to Array.Copy - Array.Copy(sortedList.values, 0, array, arrayIndex, sortedList.Count); + Array.Copy(sortedList.values, 0, array!, arrayIndex, sortedList.Count); } - public virtual void Insert(int index, object value) + public virtual void Insert(int index, object? value) { throw new NotSupportedException(SR.NotSupported_SortedListNestedWrite); } - public virtual object this[int index] + public virtual object? this[int index] { get { @@ -1092,12 +1087,12 @@ namespace System.Collections return new SortedListEnumerator(sortedList, 0, sortedList.Count, SortedListEnumerator.Values); } - public virtual int IndexOf(object value) + public virtual int IndexOf(object? value) { return Array.IndexOf(sortedList.values, value, 0, sortedList.Count); } - public virtual void Remove(object value) + public virtual void Remove(object? value) { throw new NotSupportedException(SR.NotSupported_SortedListNestedWrite); } diff --git a/src/libraries/System.Collections.NonGeneric/src/System/Collections/Stack.cs b/src/libraries/System.Collections.NonGeneric/src/System/Collections/Stack.cs index 32cd4ac..b39fe90 100644 --- a/src/libraries/System.Collections.NonGeneric/src/System/Collections/Stack.cs +++ b/src/libraries/System.Collections.NonGeneric/src/System/Collections/Stack.cs @@ -24,7 +24,7 @@ namespace System.Collections [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")] public class Stack : ICollection, ICloneable { - private object[] _array; // Storage for stack elements. Do not rename (binary serialization) + private object?[] _array; // Storage for stack elements. Do not rename (binary serialization) private int _size; // Number of items in the stack. Do not rename (binary serialization) private int _version; // Used to keep enumerator in sync w/ collection. Do not rename (binary serialization) @@ -96,7 +96,7 @@ namespace System.Collections return s; } - public virtual bool Contains(object obj) + public virtual bool Contains(object? obj) { int count = _size; @@ -107,7 +107,7 @@ namespace System.Collections if (_array[count] == null) return true; } - else if (_array[count] != null && _array[count].Equals(obj)) + else if (_array[count] != null && _array[count]!.Equals(obj)) { return true; } @@ -128,7 +128,7 @@ namespace System.Collections throw new ArgumentException(SR.Argument_InvalidOffLen); int i = 0; - object[] objArray = array as object[]; + object?[]? objArray = array as object[]; if (objArray != null) { while (i < _size) @@ -155,7 +155,7 @@ namespace System.Collections // Returns the top object on the stack without removing it. If the stack // is empty, Peek throws an InvalidOperationException. - public virtual object Peek() + public virtual object? Peek() { if (_size == 0) throw new InvalidOperationException(SR.InvalidOperation_EmptyStack); @@ -165,20 +165,20 @@ namespace System.Collections // Pops an item from the top of the stack. If the stack is empty, Pop // throws an InvalidOperationException. - public virtual object Pop() + public virtual object? Pop() { if (_size == 0) throw new InvalidOperationException(SR.InvalidOperation_EmptyStack); _version++; - object obj = _array[--_size]; + object? obj = _array[--_size]; _array[_size] = null; // Free memory quicker. return obj; } // Pushes an item to the top of the stack. // - public virtual void Push(object obj) + public virtual void Push(object? obj) { if (_size == _array.Length) { @@ -202,12 +202,12 @@ namespace System.Collections // Copies the Stack to an array, in the same order Pop would return the items. - public virtual object[] ToArray() + public virtual object?[] ToArray() { if (_size == 0) return Array.Empty(); - object[] objArray = new object[_size]; + object?[] objArray = new object[_size]; int i = 0; while (i < _size) { @@ -252,7 +252,7 @@ namespace System.Collections } } - public override bool Contains(object obj) + public override bool Contains(object? obj) { lock (_root) { @@ -284,7 +284,7 @@ namespace System.Collections } } - public override void Push(object value) + public override void Push(object? value) { lock (_root) { @@ -292,7 +292,7 @@ namespace System.Collections } } - public override object Pop() + public override object? Pop() { lock (_root) { @@ -308,7 +308,7 @@ namespace System.Collections } } - public override object Peek() + public override object? Peek() { lock (_root) { @@ -316,7 +316,7 @@ namespace System.Collections } } - public override object[] ToArray() + public override object?[] ToArray() { lock (_root) { @@ -330,7 +330,7 @@ namespace System.Collections private readonly Stack _stack; private int _index; private readonly int _version; - private object _currentElement; + private object? _currentElement; internal StackEnumerator(Stack stack) { @@ -367,7 +367,7 @@ namespace System.Collections return retval; } - public virtual object Current + public virtual object? Current { get { @@ -398,7 +398,7 @@ namespace System.Collections } [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)] - public object[] Items + public object?[] Items { get {