From 75920800c68b163bf8abe36fc3c877b2ae9b182b Mon Sep 17 00:00:00 2001 From: Hugh Bellamy Date: Wed, 6 Feb 2019 03:43:14 +0000 Subject: [PATCH] Cleanup ObjectModel code (dotnet/corefx#35047) * Cleanup ObjectModel code * Improve parameter names Commit migrated from https://github.com/dotnet/corefx/commit/bab7da282613120288759dd7b7d3ff7d42dc00cf --- .../src/System.ObjectModel.csproj | 1 + .../src/System/Collections/Generic/DebugView.cs | 12 +- .../Collections/ObjectModel/KeyedCollection.cs | 72 ++-- .../ObjectModel/ObservableCollection.cs | 128 ++------ .../Collections/ObjectModel/ReadOnlyDictionary.cs | 362 ++++----------------- .../ObjectModel/ReadOnlyObservableCollection.cs | 63 +--- .../Specialized/INotifyCollectionChanged.cs | 5 - .../Specialized/NotifyCollectionChangedAction.cs | 37 +++ .../NotifyCollectionChangedEventArgs.cs | 186 +++++------ .../ComponentModel/DataErrorsChangedEventArgs.cs | 36 +- .../System/ComponentModel/INotifyDataErrorInfo.cs | 1 - .../ComponentModel/PropertyChangedEventArgs.cs | 36 +- .../ComponentModel/PropertyChangedEventHandler.cs | 12 +- .../ComponentModel/PropertyChangingEventArgs.cs | 35 +- .../ComponentModel/PropertyChangingEventHandler.cs | 11 +- .../ComponentModel/TypeConverterAttribute.cs | 47 +-- .../src/System/Reflection/ICustomTypeProvider.cs | 1 - .../src/System/Windows/Input/ICommand.cs | 9 +- .../Windows/Markup/ValueSerializerAttribute.cs | 10 +- .../tests/KeyedCollection/ConcreteTestClasses.cs | 20 +- .../tests/KeyedCollection/TestMethods.cs | 55 ++-- .../KeyedCollection/TestMethods.netcoreapp.cs | 6 +- ...rvableCollection_ConstructorAndPropertyTests.cs | 2 +- .../ObservableCollection_MethodsTest.cs | 22 +- .../ReadOnlyDictionary/ReadOnlyDictionaryTests.cs | 2 +- .../ReadOnlyObservableCollectionTests.cs | 15 +- .../ReadOnlyObservableCollection_EventsTests.cs | 12 +- .../ComponentModel/TypeConverterAttributeTests.cs | 2 +- 28 files changed, 366 insertions(+), 834 deletions(-) create mode 100644 src/libraries/System.ObjectModel/src/System/Collections/Specialized/NotifyCollectionChangedAction.cs diff --git a/src/libraries/System.ObjectModel/src/System.ObjectModel.csproj b/src/libraries/System.ObjectModel/src/System.ObjectModel.csproj index b697c02..e14d6be 100644 --- a/src/libraries/System.ObjectModel/src/System.ObjectModel.csproj +++ b/src/libraries/System.ObjectModel/src/System.ObjectModel.csproj @@ -8,6 +8,7 @@ + diff --git a/src/libraries/System.ObjectModel/src/System/Collections/Generic/DebugView.cs b/src/libraries/System.ObjectModel/src/System/Collections/Generic/DebugView.cs index b00dd79..7e47757 100644 --- a/src/libraries/System.ObjectModel/src/System/Collections/Generic/DebugView.cs +++ b/src/libraries/System.ObjectModel/src/System/Collections/Generic/DebugView.cs @@ -2,7 +2,6 @@ // 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; using System.Diagnostics; namespace System.Collections.Generic @@ -13,10 +12,7 @@ namespace System.Collections.Generic public CollectionDebugView(ICollection collection) { - if (collection == null) - throw new ArgumentNullException(nameof(collection)); - - _collection = collection; + _collection = collection ?? throw new ArgumentNullException(nameof(collection)); } [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)] @@ -37,10 +33,7 @@ namespace System.Collections.Generic public DictionaryDebugView(IDictionary dictionary) { - if (dictionary == null) - throw new ArgumentNullException(nameof(dictionary)); - - _dict = dictionary; + _dict = dictionary ?? throw new ArgumentNullException(nameof(dictionary)); } [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)] @@ -55,4 +48,3 @@ namespace System.Collections.Generic } } } - diff --git a/src/libraries/System.ObjectModel/src/System/Collections/ObjectModel/KeyedCollection.cs b/src/libraries/System.ObjectModel/src/System/Collections/ObjectModel/KeyedCollection.cs index f145b05..896bc8e 100644 --- a/src/libraries/System.ObjectModel/src/System/Collections/ObjectModel/KeyedCollection.cs +++ b/src/libraries/System.ObjectModel/src/System/Collections/ObjectModel/KeyedCollection.cs @@ -2,7 +2,6 @@ // 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; using System.Collections.Generic; using System.Diagnostics; @@ -14,61 +13,47 @@ namespace System.Collections.ObjectModel [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")] public abstract class KeyedCollection : Collection { - private const int defaultThreshold = 0; + private const int DefaultThreshold = 0; private readonly IEqualityComparer comparer; // Do not rename (binary serialization) private Dictionary dict; // Do not rename (binary serialization) private int keyCount; // Do not rename (binary serialization) private readonly int threshold; // Do not rename (binary serialization) - protected KeyedCollection() : this(null, defaultThreshold) { } - - protected KeyedCollection(IEqualityComparer comparer) : this(comparer, defaultThreshold) { } + protected KeyedCollection() : this(null, DefaultThreshold) + { + } + protected KeyedCollection(IEqualityComparer comparer) : this(comparer, DefaultThreshold) + { + } protected KeyedCollection(IEqualityComparer comparer, int dictionaryCreationThreshold) : base(new List()) // Be explicit about the use of List so we can foreach over // Items internally without enumerator allocations. { - if (comparer == null) - { - comparer = EqualityComparer.Default; - } - - if (dictionaryCreationThreshold == -1) - { - dictionaryCreationThreshold = int.MaxValue; - } - if (dictionaryCreationThreshold < -1) { throw new ArgumentOutOfRangeException(nameof(dictionaryCreationThreshold), SR.ArgumentOutOfRange_InvalidThreshold); } - this.comparer = comparer; - threshold = dictionaryCreationThreshold; + this.comparer = comparer ?? EqualityComparer.Default; + threshold = dictionaryCreationThreshold == -1 ? int.MaxValue : dictionaryCreationThreshold; } /// /// Enables the use of foreach internally without allocations using 's struct enumerator. /// - new private List Items + private new List Items { get { Debug.Assert(base.Items is List); - return (List)base.Items; } } - public IEqualityComparer Comparer - { - get - { - return comparer; - } - } + public IEqualityComparer Comparer => comparer; public TItem this[TKey key] { @@ -98,8 +83,12 @@ namespace System.Collections.ObjectModel foreach (TItem item in Items) { - if (comparer.Equals(GetKeyForItem(item), key)) return true; + if (comparer.Equals(GetKeyForItem(item), key)) + { + return true; + } } + return false; } @@ -137,12 +126,11 @@ namespace System.Collections.ObjectModel return Items.Contains(item); } - TItem itemInDict; - bool exist = dict.TryGetValue(key, out itemInDict); - if (exist) + if (dict.TryGetValue(key, out TItem itemInDict)) { return EqualityComparer.Default.Equals(itemInDict, item); } + return false; } @@ -155,8 +143,7 @@ namespace System.Collections.ObjectModel if (dict != null) { - TItem item; - return dict.TryGetValue(key, out item) && Remove(item); + return dict.TryGetValue(key, out TItem item) && Remove(item); } for (int i = 0; i < Items.Count; i++) @@ -167,20 +154,17 @@ namespace System.Collections.ObjectModel return true; } } + return false; } - protected IDictionary Dictionary - { - get { return dict; } - } + protected IDictionary Dictionary => dict; protected void ChangeItemKey(TItem item, TKey newKey) { - // Check if the item exists in the collection if (!ContainsItem(item)) { - throw new ArgumentException(SR.Argument_ItemNotExist); + throw new ArgumentException(SR.Argument_ItemNotExist, nameof(item)); } TKey oldKey = GetKeyForItem(item); @@ -190,7 +174,6 @@ namespace System.Collections.ObjectModel { AddKey(newKey, item); } - if (oldKey != null) { RemoveKey(oldKey); @@ -201,11 +184,7 @@ namespace System.Collections.ObjectModel protected override void ClearItems() { base.ClearItems(); - if (dict != null) - { - dict.Clear(); - } - + dict?.Clear(); keyCount = 0; } @@ -218,6 +197,7 @@ namespace System.Collections.ObjectModel { AddKey(key, item); } + base.InsertItem(index, item); } @@ -228,6 +208,7 @@ namespace System.Collections.ObjectModel { RemoveKey(key); } + base.RemoveItem(index); } @@ -255,6 +236,7 @@ namespace System.Collections.ObjectModel RemoveKey(oldKey); } } + base.SetItem(index, item); } @@ -273,7 +255,7 @@ namespace System.Collections.ObjectModel { if (Contains(key)) { - throw new ArgumentException(SR.Format(SR.Argument_AddingDuplicate, key)); + throw new ArgumentException(SR.Format(SR.Argument_AddingDuplicate, key), nameof(key)); } keyCount++; diff --git a/src/libraries/System.ObjectModel/src/System/Collections/ObjectModel/ObservableCollection.cs b/src/libraries/System.ObjectModel/src/System/Collections/ObjectModel/ObservableCollection.cs index bb5770d..f74a2c8 100644 --- a/src/libraries/System.ObjectModel/src/System/Collections/ObjectModel/ObservableCollection.cs +++ b/src/libraries/System.ObjectModel/src/System/Collections/ObjectModel/ObservableCollection.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.Collections.Specialized; using System.ComponentModel; using System.Diagnostics; +using System.Runtime.CompilerServices; using System.Runtime.Serialization; namespace System.Collections.ObjectModel @@ -21,31 +22,17 @@ namespace System.Collections.ObjectModel [System.Runtime.CompilerServices.TypeForwardedFrom("WindowsBase, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")] public class ObservableCollection : Collection, INotifyCollectionChanged, INotifyPropertyChanged { - //------------------------------------------------------ - // - // Private Fields - // - //------------------------------------------------------ - - #region Private Fields - private SimpleMonitor _monitor; // Lazily allocated only when a subclass calls BlockReentrancy() or during serialization. Do not rename (binary serialization) [NonSerialized] private int _blockReentrancyCount; - #endregion Private Fields - - //------------------------------------------------------ - // - // Constructors - // - //------------------------------------------------------ - #region Constructors /// /// Initializes a new instance of ObservableCollection that is empty and has default initial capacity. /// - public ObservableCollection() { } + public ObservableCollection() + { + } /// /// Initializes a new instance of the ObservableCollection class that contains @@ -58,7 +45,9 @@ namespace System.Collections.ObjectModel /// same order they are read by the enumerator of the collection. /// /// collection is a null reference - public ObservableCollection(IEnumerable collection) : base(CreateCopy(collection, nameof(collection))) { } + public ObservableCollection(IEnumerable collection) : base(CreateCopy(collection, nameof(collection))) + { + } /// /// Initializes a new instance of the ObservableCollection class @@ -70,67 +59,35 @@ namespace System.Collections.ObjectModel /// same order they are read by the enumerator of the list. /// /// list is a null reference - public ObservableCollection(List list) : base(CreateCopy(list, nameof(list))) { } + public ObservableCollection(List list) : base(CreateCopy(list, nameof(list))) + { + } private static List CreateCopy(IEnumerable collection, string paramName) { if (collection == null) + { throw new ArgumentNullException(paramName); + } return new List(collection); } - #endregion Constructors - - - //------------------------------------------------------ - // - // Public Methods - // - //------------------------------------------------------ - - #region Public Methods - /// /// Move item at oldIndex to newIndex. /// - public void Move(int oldIndex, int newIndex) - { - MoveItem(oldIndex, newIndex); - } - - #endregion Public Methods + public void Move(int oldIndex, int newIndex) => MoveItem(oldIndex, newIndex); - //------------------------------------------------------ - // - // Public Events - // - //------------------------------------------------------ - - #region Public Events - - //------------------------------------------------------ - #region INotifyPropertyChanged implementation - /// /// PropertyChanged event (per ). /// event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged { - add - { - PropertyChanged += value; - } - remove - { - PropertyChanged -= value; - } + add => PropertyChanged += value; + remove => PropertyChanged -= value; } - #endregion INotifyPropertyChanged implementation - - //------------------------------------------------------ /// /// Occurs when the collection changes, either by adding or removing an item. /// @@ -140,17 +97,6 @@ namespace System.Collections.ObjectModel [field: NonSerialized] public virtual event NotifyCollectionChangedEventHandler CollectionChanged; - #endregion Public Events - - - //------------------------------------------------------ - // - // Protected Methods - // - //------------------------------------------------------ - - #region Protected Methods - /// /// Called by base class Collection<T> when the list is being cleared; /// raises a CollectionChanged event to any listeners. @@ -225,7 +171,6 @@ namespace System.Collections.ObjectModel OnCollectionChanged(NotifyCollectionChangedAction.Move, removedItem, newIndex, oldIndex); } - /// /// Raises a PropertyChanged event (per ). /// @@ -302,31 +247,15 @@ namespace System.Collections.ObjectModel } } - #endregion Protected Methods - - - //------------------------------------------------------ - // - // Private Methods - // - //------------------------------------------------------ - - #region Private Methods /// /// Helper to raise a PropertyChanged event for the Count property /// - private void OnCountPropertyChanged() - { - OnPropertyChanged(EventArgsCache.CountPropertyChanged); - } + private void OnCountPropertyChanged() => OnPropertyChanged(EventArgsCache.CountPropertyChanged); /// /// Helper to raise a PropertyChanged event for the Indexer property /// - private void OnIndexerPropertyChanged() - { - OnPropertyChanged(EventArgsCache.IndexerPropertyChanged); - } + private void OnIndexerPropertyChanged() => OnPropertyChanged(EventArgsCache.IndexerPropertyChanged); /// /// Helper to raise CollectionChanged event to any listeners @@ -355,10 +284,7 @@ namespace System.Collections.ObjectModel /// /// Helper to raise CollectionChanged event with action == Reset to any listeners /// - private void OnCollectionReset() - { - OnCollectionChanged(EventArgsCache.ResetCollectionChanged); - } + private void OnCollectionReset() => OnCollectionChanged(EventArgsCache.ResetCollectionChanged); private SimpleMonitor EnsureMonitorInitialized() { @@ -381,19 +307,10 @@ namespace System.Collections.ObjectModel _monitor._collection = this; } } - #endregion Private Methods - - //------------------------------------------------------ - // - // Private Types - // - //------------------------------------------------------ - - #region Private Types // this class helps prevent reentrant calls [Serializable] - [System.Runtime.CompilerServices.TypeForwardedFrom("WindowsBase, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")] + [TypeForwardedFrom("WindowsBase, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")] private sealed class SimpleMonitor : IDisposable { internal int _busyCount; // Only used during (de)serialization to maintain compatibility with desktop. Do not rename (binary serialization) @@ -407,13 +324,8 @@ namespace System.Collections.ObjectModel _collection = collection; } - public void Dispose() - { - _collection._blockReentrancyCount--; - } + public void Dispose() => _collection._blockReentrancyCount--; } - - #endregion Private Types } internal static class EventArgsCache diff --git a/src/libraries/System.ObjectModel/src/System/Collections/ObjectModel/ReadOnlyDictionary.cs b/src/libraries/System.ObjectModel/src/System/Collections/ObjectModel/ReadOnlyDictionary.cs index 524cf7d..df2387d 100644 --- a/src/libraries/System.ObjectModel/src/System/Collections/ObjectModel/ReadOnlyDictionary.cs +++ b/src/libraries/System.ObjectModel/src/System/Collections/ObjectModel/ReadOnlyDictionary.cs @@ -2,8 +2,6 @@ // 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; -using System.Collections; using System.Collections.Generic; using System.Diagnostics; @@ -24,77 +22,33 @@ namespace System.Collections.ObjectModel public ReadOnlyDictionary(IDictionary dictionary) { - if (dictionary == null) - { - throw new ArgumentNullException(nameof(dictionary)); - } - m_dictionary = dictionary; + m_dictionary = dictionary ?? throw new ArgumentNullException(nameof(dictionary)); } - protected IDictionary Dictionary - { - get { return m_dictionary; } - } + protected IDictionary Dictionary => m_dictionary; public KeyCollection Keys { - get - { - if (_keys == null) - { - _keys = new KeyCollection(m_dictionary.Keys); - } - return _keys; - } + get => _keys ?? (_keys = new KeyCollection(m_dictionary.Keys)); } public ValueCollection Values { - get - { - if (_values == null) - { - _values = new ValueCollection(m_dictionary.Values); - } - return _values; - } + get => _values ?? (_values = new ValueCollection(m_dictionary.Values)); } - #region IDictionary Members + public bool ContainsKey(TKey key) => m_dictionary.ContainsKey(key); - public bool ContainsKey(TKey key) - { - return m_dictionary.ContainsKey(key); - } - - ICollection IDictionary.Keys - { - get - { - return Keys; - } - } + ICollection IDictionary.Keys => Keys; public bool TryGetValue(TKey key, out TValue value) { return m_dictionary.TryGetValue(key, out value); } - ICollection IDictionary.Values - { - get - { - return Values; - } - } + ICollection IDictionary.Values => Values; - public TValue this[TKey key] - { - get - { - return m_dictionary[key]; - } - } + public TValue this[TKey key] => m_dictionary[key]; void IDictionary.Add(TKey key, TValue value) { @@ -108,24 +62,11 @@ namespace System.Collections.ObjectModel TValue IDictionary.this[TKey key] { - get - { - return m_dictionary[key]; - } - set - { - throw new NotSupportedException(SR.NotSupported_ReadOnlyCollection); - } + get => m_dictionary[key]; + set => throw new NotSupportedException(SR.NotSupported_ReadOnlyCollection); } - #endregion - - #region ICollection> Members - - public int Count - { - get { return m_dictionary.Count; } - } + public int Count => m_dictionary.Count; bool ICollection>.Contains(KeyValuePair item) { @@ -137,10 +78,7 @@ namespace System.Collections.ObjectModel m_dictionary.CopyTo(array, arrayIndex); } - bool ICollection>.IsReadOnly - { - get { return true; } - } + bool ICollection>.IsReadOnly => true; void ICollection>.Add(KeyValuePair item) { @@ -157,34 +95,23 @@ namespace System.Collections.ObjectModel throw new NotSupportedException(SR.NotSupported_ReadOnlyCollection); } - #endregion - - #region IEnumerable> Members - public IEnumerator> GetEnumerator() { return m_dictionary.GetEnumerator(); } - #endregion - - #region IEnumerable Members - - System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() + IEnumerator IEnumerable.GetEnumerator() { return ((IEnumerable)m_dictionary).GetEnumerator(); } - #endregion - - #region IDictionary Members - private static bool IsCompatibleKey(object key) { if (key == null) { throw new ArgumentNullException(nameof(key)); } + return key is TKey; } @@ -213,51 +140,31 @@ namespace System.Collections.ObjectModel return new DictionaryEnumerator(m_dictionary); } - bool IDictionary.IsFixedSize - { - get { return true; } - } + bool IDictionary.IsFixedSize => true; - bool IDictionary.IsReadOnly - { - get { return true; } - } + bool IDictionary.IsReadOnly => true; - ICollection IDictionary.Keys - { - get - { - return Keys; - } - } + ICollection IDictionary.Keys => Keys; void IDictionary.Remove(object key) { throw new NotSupportedException(SR.NotSupported_ReadOnlyCollection); } - ICollection IDictionary.Values - { - get - { - return Values; - } - } + ICollection IDictionary.Values => Values; object IDictionary.this[object key] { get { - if (IsCompatibleKey(key)) + if (!IsCompatibleKey(key)) { - return this[(TKey)key]; + return null; } - return null; - } - set - { - throw new NotSupportedException(SR.NotSupported_ReadOnlyCollection); + + return this[(TKey)key]; } + set => throw new NotSupportedException(SR.NotSupported_ReadOnlyCollection); } void ICollection.CopyTo(Array array, int index) @@ -266,22 +173,18 @@ namespace System.Collections.ObjectModel { throw new ArgumentNullException(nameof(array)); } - if (array.Rank != 1) { - throw new ArgumentException(SR.Arg_RankMultiDimNotSupported); + throw new ArgumentException(SR.Arg_RankMultiDimNotSupported, nameof(array)); } - if (array.GetLowerBound(0) != 0) { - throw new ArgumentException(SR.Arg_NonZeroLowerBound); + throw new ArgumentException(SR.Arg_NonZeroLowerBound, nameof(array)); } - if (index < 0 || index > array.Length) { throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_NeedNonNegNum); } - if (array.Length - index < Count) { throw new ArgumentException(SR.Arg_ArrayPlusOffTooSmall); @@ -307,7 +210,7 @@ namespace System.Collections.ObjectModel object[] objects = array as object[]; if (objects == null) { - throw new ArgumentException(SR.Argument_InvalidArrayType); + throw new ArgumentException(SR.Argument_InvalidArrayType, nameof(array)); } try @@ -319,24 +222,15 @@ namespace System.Collections.ObjectModel } catch (ArrayTypeMismatchException) { - throw new ArgumentException(SR.Argument_InvalidArrayType); + throw new ArgumentException(SR.Argument_InvalidArrayType, nameof(array)); } } } } - bool ICollection.IsSynchronized - { - get { return false; } - } + bool ICollection.IsSynchronized => false; - object ICollection.SyncRoot - { - get - { - return (m_dictionary is ICollection coll) ? coll.SyncRoot : this; - } - } + object ICollection.SyncRoot => (m_dictionary is ICollection coll) ? coll.SyncRoot : this; private struct DictionaryEnumerator : IDictionaryEnumerator { @@ -351,56 +245,23 @@ namespace System.Collections.ObjectModel public DictionaryEntry Entry { - get { return new DictionaryEntry(_enumerator.Current.Key, _enumerator.Current.Value); } - } - - public object Key - { - get { return _enumerator.Current.Key; } + get => new DictionaryEntry(_enumerator.Current.Key, _enumerator.Current.Value); } - public object Value - { - get { return _enumerator.Current.Value; } - } + public object Key => _enumerator.Current.Key; - public object Current - { - get { return Entry; } - } + public object Value => _enumerator.Current.Value; - public bool MoveNext() - { - return _enumerator.MoveNext(); - } + public object Current => Entry; - public void Reset() - { - _enumerator.Reset(); - } - } + public bool MoveNext() => _enumerator.MoveNext(); - #endregion - - #region IReadOnlyDictionary members - - IEnumerable IReadOnlyDictionary.Keys - { - get - { - return Keys; - } + public void Reset() => _enumerator.Reset(); } - IEnumerable IReadOnlyDictionary.Values - { - get - { - return Values; - } - } + IEnumerable IReadOnlyDictionary.Keys => Keys; - #endregion IReadOnlyDictionary members + IEnumerable IReadOnlyDictionary.Values => Values; [DebuggerTypeProxy(typeof(CollectionDebugView<>))] [DebuggerDisplay("Count = {Count}")] @@ -410,15 +271,9 @@ namespace System.Collections.ObjectModel internal KeyCollection(ICollection collection) { - if (collection == null) - { - throw new ArgumentNullException(nameof(collection)); - } - _collection = collection; + _collection = collection ?? throw new ArgumentNullException(nameof(collection)); } - #region ICollection Members - void ICollection.Add(TKey item) { throw new NotSupportedException(SR.NotSupported_ReadOnlyCollection); @@ -439,61 +294,27 @@ namespace System.Collections.ObjectModel _collection.CopyTo(array, arrayIndex); } - public int Count - { - get { return _collection.Count; } - } + public int Count => _collection.Count; - bool ICollection.IsReadOnly - { - get { return true; } - } + bool ICollection.IsReadOnly => true; bool ICollection.Remove(TKey item) { throw new NotSupportedException(SR.NotSupported_ReadOnlyCollection); } - #endregion - - #region IEnumerable Members - - public IEnumerator GetEnumerator() - { - return _collection.GetEnumerator(); - } - - #endregion - - #region IEnumerable Members - - System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() - { - return ((IEnumerable)_collection).GetEnumerator(); - } - - #endregion + public IEnumerator GetEnumerator() => _collection.GetEnumerator(); - #region ICollection Members + IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable)_collection).GetEnumerator(); void ICollection.CopyTo(Array array, int index) { ReadOnlyDictionaryHelpers.CopyToNonGenericICollectionHelper(_collection, array, index); } - bool ICollection.IsSynchronized - { - get { return false; } - } + bool ICollection.IsSynchronized => false; - object ICollection.SyncRoot - { - get - { - return (_collection is ICollection coll) ? coll.SyncRoot : this; - } - } - #endregion + object ICollection.SyncRoot => (_collection is ICollection coll) ? coll.SyncRoot : this; } [DebuggerTypeProxy(typeof(CollectionDebugView<>))] @@ -504,15 +325,9 @@ namespace System.Collections.ObjectModel internal ValueCollection(ICollection collection) { - if (collection == null) - { - throw new ArgumentNullException(nameof(collection)); - } - _collection = collection; + _collection = collection ?? throw new ArgumentNullException(nameof(collection)); } - #region ICollection Members - void ICollection.Add(TValue item) { throw new NotSupportedException(SR.NotSupported_ReadOnlyCollection); @@ -523,80 +338,39 @@ namespace System.Collections.ObjectModel throw new NotSupportedException(SR.NotSupported_ReadOnlyCollection); } - bool ICollection.Contains(TValue item) - { - return _collection.Contains(item); - } + bool ICollection.Contains(TValue item) => _collection.Contains(item); public void CopyTo(TValue[] array, int arrayIndex) { _collection.CopyTo(array, arrayIndex); } - public int Count - { - get { return _collection.Count; } - } + public int Count => _collection.Count; - bool ICollection.IsReadOnly - { - get { return true; } - } + bool ICollection.IsReadOnly => true; bool ICollection.Remove(TValue item) { throw new NotSupportedException(SR.NotSupported_ReadOnlyCollection); } + public IEnumerator GetEnumerator() => _collection.GetEnumerator(); - #endregion - - #region IEnumerable Members - - public IEnumerator GetEnumerator() - { - return _collection.GetEnumerator(); - } - - #endregion - - #region IEnumerable Members - - System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() - { - return ((IEnumerable)_collection).GetEnumerator(); - } - - #endregion - - #region ICollection Members + IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable)_collection).GetEnumerator(); void ICollection.CopyTo(Array array, int index) { ReadOnlyDictionaryHelpers.CopyToNonGenericICollectionHelper(_collection, array, index); } - bool ICollection.IsSynchronized - { - get { return false; } - } - - object ICollection.SyncRoot - { - get - { - return (_collection is ICollection coll) ? coll.SyncRoot : this; - } - } + bool ICollection.IsSynchronized => false; - #endregion ICollection Members + object ICollection.SyncRoot => (_collection is ICollection coll) ? coll.SyncRoot : this; } } // To share code when possible, use a non-generic class to get rid of irrelevant type parameters. internal static class ReadOnlyDictionaryHelpers { - #region Helper method for our KeyCollection and ValueCollection - // Abstracted away to avoid redundant implementations. internal static void CopyToNonGenericICollectionHelper(ICollection collection, Array array, int index) { @@ -604,22 +378,18 @@ namespace System.Collections.ObjectModel { throw new ArgumentNullException(nameof(array)); } - if (array.Rank != 1) { - throw new ArgumentException(SR.Arg_RankMultiDimNotSupported); + throw new ArgumentException(SR.Arg_RankMultiDimNotSupported, nameof(array)); } - if (array.GetLowerBound(0) != 0) { - throw new ArgumentException(SR.Arg_NonZeroLowerBound); + throw new ArgumentException(SR.Arg_NonZeroLowerBound, nameof(array)); } - if (index < 0) { throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_NeedNonNegNum); } - if (array.Length - index < collection.Count) { throw new ArgumentException(SR.Arg_ArrayPlusOffTooSmall); @@ -640,30 +410,12 @@ namespace System.Collections.ObjectModel } else { - /* - FxOverRh: Type.IsAssignableNot() not an api on that platform. - - // - // Catch the obvious case assignment will fail. - // We can found all possible problems by doing the check though. - // For example, if the element type of the Array is derived from T, - // we can't figure out if we can successfully copy the element beforehand. - // - Type targetType = array.GetType().GetElementType(); - Type sourceType = typeof(T); - if (!(targetType.IsAssignableFrom(sourceType) || sourceType.IsAssignableFrom(targetType))) { - throw new ArgumentException(SR.Argument_InvalidArrayType); - } - */ - - // // We can't cast array of value type to object[], so we don't support // widening of primitive types here. - // object[] objects = array as object[]; if (objects == null) { - throw new ArgumentException(SR.Argument_InvalidArrayType); + throw new ArgumentException(SR.Argument_InvalidArrayType, nameof(array)); } try @@ -675,11 +427,9 @@ namespace System.Collections.ObjectModel } catch (ArrayTypeMismatchException) { - throw new ArgumentException(SR.Argument_InvalidArrayType); + throw new ArgumentException(SR.Argument_InvalidArrayType, nameof(array)); } } } - #endregion Helper method for our KeyCollection and ValueCollection } } - diff --git a/src/libraries/System.ObjectModel/src/System/Collections/ObjectModel/ReadOnlyObservableCollection.cs b/src/libraries/System.ObjectModel/src/System/Collections/ObjectModel/ReadOnlyObservableCollection.cs index 734909b..c736d74 100644 --- a/src/libraries/System.ObjectModel/src/System/Collections/ObjectModel/ReadOnlyObservableCollection.cs +++ b/src/libraries/System.ObjectModel/src/System/Collections/ObjectModel/ReadOnlyObservableCollection.cs @@ -2,8 +2,6 @@ // 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; -using System.Collections; using System.Collections.Generic; using System.Collections.Specialized; using System.Diagnostics; @@ -18,17 +16,9 @@ namespace System.Collections.ObjectModel [Serializable] [DebuggerTypeProxy(typeof(CollectionDebugView<>))] [DebuggerDisplay("Count = {Count}")] - [System.Runtime.CompilerServices.TypeForwardedFrom("WindowsBase, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")] + [TypeForwardedFrom("WindowsBase, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")] public class ReadOnlyObservableCollection : ReadOnlyCollection, INotifyCollectionChanged, INotifyPropertyChanged { - #region Constructors - - //------------------------------------------------------ - // - // Constructors - // - //------------------------------------------------------ - /// /// Initializes a new instance of ReadOnlyObservableCollection that /// wraps the given ObservableCollection. @@ -39,25 +29,13 @@ namespace System.Collections.ObjectModel ((INotifyPropertyChanged)Items).PropertyChanged += new PropertyChangedEventHandler(HandlePropertyChanged); } - #endregion Constructors - - #region Interfaces - - //------------------------------------------------------ - // - // Interfaces - // - //------------------------------------------------------ - - #region INotifyCollectionChanged - /// /// CollectionChanged event (per ). /// event NotifyCollectionChangedEventHandler INotifyCollectionChanged.CollectionChanged { - add { CollectionChanged += value; } - remove { CollectionChanged -= value; } + add => CollectionChanged += value; + remove => CollectionChanged -= value; } /// @@ -74,23 +52,16 @@ namespace System.Collections.ObjectModel /// protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs args) { - if (CollectionChanged != null) - { - CollectionChanged(this, args); - } + CollectionChanged?.Invoke(this, args); } - #endregion INotifyCollectionChanged - - #region INotifyPropertyChanged - /// /// PropertyChanged event (per ). /// event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged { - add { PropertyChanged += value; } - remove { PropertyChanged -= value; } + add => PropertyChanged += value; + remove => PropertyChanged -= value; } /// @@ -107,37 +78,17 @@ namespace System.Collections.ObjectModel /// protected virtual void OnPropertyChanged(PropertyChangedEventArgs args) { - if (PropertyChanged != null) - { - PropertyChanged(this, args); - } + PropertyChanged?.Invoke(this, args); } - #endregion INotifyPropertyChanged - - #endregion Interfaces - - #region Private Methods - - //------------------------------------------------------ - // - // Private Methods - // - //------------------------------------------------------ - - // forward CollectionChanged events from the base list to our listeners private void HandleCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { OnCollectionChanged(e); } - // forward PropertyChanged events from the base list to our listeners private void HandlePropertyChanged(object sender, PropertyChangedEventArgs e) { OnPropertyChanged(e); } - #endregion Private Methods } } - - diff --git a/src/libraries/System.ObjectModel/src/System/Collections/Specialized/INotifyCollectionChanged.cs b/src/libraries/System.ObjectModel/src/System/Collections/Specialized/INotifyCollectionChanged.cs index d115c92..0195902 100644 --- a/src/libraries/System.ObjectModel/src/System/Collections/Specialized/INotifyCollectionChanged.cs +++ b/src/libraries/System.ObjectModel/src/System/Collections/Specialized/INotifyCollectionChanged.cs @@ -2,9 +2,6 @@ // 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; -using System.Runtime.CompilerServices; - namespace System.Collections.Specialized { /// @@ -24,5 +21,3 @@ namespace System.Collections.Specialized event NotifyCollectionChangedEventHandler CollectionChanged; } } - - diff --git a/src/libraries/System.ObjectModel/src/System/Collections/Specialized/NotifyCollectionChangedAction.cs b/src/libraries/System.ObjectModel/src/System/Collections/Specialized/NotifyCollectionChangedAction.cs new file mode 100644 index 0000000..97ec5d9 --- /dev/null +++ b/src/libraries/System.ObjectModel/src/System/Collections/Specialized/NotifyCollectionChangedAction.cs @@ -0,0 +1,37 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Collections.Specialized +{ + /// + /// This enum describes the action that caused a CollectionChanged event. + /// + public enum NotifyCollectionChangedAction + { + /// + /// One or more items were added to the collection. + /// + Add, + + /// + /// One or more items were removed from the collection. + /// + Remove, + + /// + /// One or more items were replaced in the collection. + /// + Replace, + + /// + /// One or more items were moved within the collection. + /// + Move, + + /// + /// The contents of the collection changed dramatically. + /// + Reset, + } +} diff --git a/src/libraries/System.ObjectModel/src/System/Collections/Specialized/NotifyCollectionChangedEventArgs.cs b/src/libraries/System.ObjectModel/src/System/Collections/Specialized/NotifyCollectionChangedEventArgs.cs index 08297db..e63d80f 100644 --- a/src/libraries/System.ObjectModel/src/System/Collections/Specialized/NotifyCollectionChangedEventArgs.cs +++ b/src/libraries/System.ObjectModel/src/System/Collections/Specialized/NotifyCollectionChangedEventArgs.cs @@ -2,31 +2,12 @@ // 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; -using System.Collections; using System.Diagnostics; using System.Runtime.CompilerServices; namespace System.Collections.Specialized { /// - /// This enum describes the action that caused a CollectionChanged event. - /// - public enum NotifyCollectionChangedAction - { - /// One or more items were added to the collection. - Add, - /// One or more items were removed from the collection. - Remove, - /// One or more items were replaced in the collection. - Replace, - /// One or more items were moved within the collection. - Move, - /// The contents of the collection changed dramatically. - Reset, - } - - /// /// Arguments for the CollectionChanged event. /// A collection that supports INotifyCollectionChangedThis raises this event /// whenever an item is added or removed, or when the contents of the collection @@ -34,11 +15,11 @@ namespace System.Collections.Specialized /// public class NotifyCollectionChangedEventArgs : EventArgs { - //------------------------------------------------------ - // - // Constructors - // - //------------------------------------------------------ + private NotifyCollectionChangedAction _action; + private IList _newItems; + private IList _oldItems; + private int _newStartingIndex = -1; + private int _oldStartingIndex = -1; /// /// Construct a NotifyCollectionChangedEventArgs that describes a reset change. @@ -47,7 +28,9 @@ namespace System.Collections.Specialized public NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction action) { if (action != NotifyCollectionChangedAction.Reset) + { throw new ArgumentException(SR.Format(SR.WrongActionForCtor, NotifyCollectionChangedAction.Reset), nameof(action)); + } InitializeAdd(action, null, -1); } @@ -61,12 +44,16 @@ namespace System.Collections.Specialized { if ((action != NotifyCollectionChangedAction.Add) && (action != NotifyCollectionChangedAction.Remove) && (action != NotifyCollectionChangedAction.Reset)) + { throw new ArgumentException(SR.MustBeResetAddOrRemoveActionForCtor, nameof(action)); + } if (action == NotifyCollectionChangedAction.Reset) { if (changedItem != null) + { throw new ArgumentException(SR.ResetActionRequiresNullItem, nameof(action)); + } InitializeAdd(action, null, -1); } @@ -86,14 +73,20 @@ namespace System.Collections.Specialized { if ((action != NotifyCollectionChangedAction.Add) && (action != NotifyCollectionChangedAction.Remove) && (action != NotifyCollectionChangedAction.Reset)) + { throw new ArgumentException(SR.MustBeResetAddOrRemoveActionForCtor, nameof(action)); + } if (action == NotifyCollectionChangedAction.Reset) { if (changedItem != null) + { throw new ArgumentException(SR.ResetActionRequiresNullItem, nameof(action)); + } if (index != -1) + { throw new ArgumentException(SR.ResetActionRequiresIndexMinus1, nameof(action)); + } InitializeAdd(action, null, -1); } @@ -112,19 +105,25 @@ namespace System.Collections.Specialized { if ((action != NotifyCollectionChangedAction.Add) && (action != NotifyCollectionChangedAction.Remove) && (action != NotifyCollectionChangedAction.Reset)) + { throw new ArgumentException(SR.MustBeResetAddOrRemoveActionForCtor, nameof(action)); + } if (action == NotifyCollectionChangedAction.Reset) { if (changedItems != null) + { throw new ArgumentException(SR.ResetActionRequiresNullItem, nameof(action)); + } InitializeAdd(action, null, -1); } else { if (changedItems == null) + { throw new ArgumentNullException(nameof(changedItems)); + } InitializeAddOrRemove(action, changedItems, -1); } @@ -140,23 +139,33 @@ namespace System.Collections.Specialized { if ((action != NotifyCollectionChangedAction.Add) && (action != NotifyCollectionChangedAction.Remove) && (action != NotifyCollectionChangedAction.Reset)) + { throw new ArgumentException(SR.MustBeResetAddOrRemoveActionForCtor, nameof(action)); + } if (action == NotifyCollectionChangedAction.Reset) { if (changedItems != null) + { throw new ArgumentException(SR.ResetActionRequiresNullItem, nameof(action)); + } if (startingIndex != -1) + { throw new ArgumentException(SR.ResetActionRequiresIndexMinus1, nameof(action)); + } InitializeAdd(action, null, -1); } else { if (changedItems == null) + { throw new ArgumentNullException(nameof(changedItems)); + } if (startingIndex < -1) + { throw new ArgumentException(SR.IndexCannotBeNegative, nameof(startingIndex)); + } InitializeAddOrRemove(action, changedItems, startingIndex); } @@ -171,7 +180,9 @@ namespace System.Collections.Specialized public NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction action, object newItem, object oldItem) { if (action != NotifyCollectionChangedAction.Replace) + { throw new ArgumentException(SR.Format(SR.WrongActionForCtor, NotifyCollectionChangedAction.Replace), nameof(action)); + } InitializeMoveOrReplace(action, new object[] { newItem }, new object[] { oldItem }, -1, -1); } @@ -186,7 +197,9 @@ namespace System.Collections.Specialized public NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction action, object newItem, object oldItem, int index) { if (action != NotifyCollectionChangedAction.Replace) + { throw new ArgumentException(SR.Format(SR.WrongActionForCtor, NotifyCollectionChangedAction.Replace), nameof(action)); + } InitializeMoveOrReplace(action, new object[] { newItem }, new object[] { oldItem }, index, index); } @@ -200,11 +213,17 @@ namespace System.Collections.Specialized public NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction action, IList newItems, IList oldItems) { if (action != NotifyCollectionChangedAction.Replace) + { throw new ArgumentException(SR.Format(SR.WrongActionForCtor, NotifyCollectionChangedAction.Replace), nameof(action)); + } if (newItems == null) + { throw new ArgumentNullException(nameof(newItems)); + } if (oldItems == null) + { throw new ArgumentNullException(nameof(oldItems)); + } InitializeMoveOrReplace(action, newItems, oldItems, -1, -1); } @@ -219,11 +238,17 @@ namespace System.Collections.Specialized public NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction action, IList newItems, IList oldItems, int startingIndex) { if (action != NotifyCollectionChangedAction.Replace) + { throw new ArgumentException(SR.Format(SR.WrongActionForCtor, NotifyCollectionChangedAction.Replace), nameof(action)); + } if (newItems == null) + { throw new ArgumentNullException(nameof(newItems)); + } if (oldItems == null) + { throw new ArgumentNullException(nameof(oldItems)); + } InitializeMoveOrReplace(action, newItems, oldItems, startingIndex, startingIndex); } @@ -238,9 +263,13 @@ namespace System.Collections.Specialized public NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction action, object changedItem, int index, int oldIndex) { if (action != NotifyCollectionChangedAction.Move) + { throw new ArgumentException(SR.Format(SR.WrongActionForCtor, NotifyCollectionChangedAction.Move), nameof(action)); + } if (index < 0) + { throw new ArgumentException(SR.IndexCannotBeNegative, nameof(index)); + } object[] changedItems = new object[] { changedItem }; InitializeMoveOrReplace(action, changedItems, changedItems, index, oldIndex); @@ -256,9 +285,13 @@ namespace System.Collections.Specialized public NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction action, IList changedItems, int index, int oldIndex) { if (action != NotifyCollectionChangedAction.Move) + { throw new ArgumentException(SR.Format(SR.WrongActionForCtor, NotifyCollectionChangedAction.Move), nameof(action)); + } if (index < 0) + { throw new ArgumentException(SR.IndexCannotBeNegative, nameof(index)); + } InitializeMoveOrReplace(action, changedItems, changedItems, index, oldIndex); } @@ -278,11 +311,14 @@ namespace System.Collections.Specialized private void InitializeAddOrRemove(NotifyCollectionChangedAction action, IList changedItems, int startingIndex) { if (action == NotifyCollectionChangedAction.Add) + { InitializeAdd(action, changedItems, startingIndex); - else if (action == NotifyCollectionChangedAction.Remove) - InitializeRemove(action, changedItems, startingIndex); + } else - Debug.Assert(false, string.Format("Unsupported action: {0}", action.ToString())); + { + Debug.Assert(action == NotifyCollectionChangedAction.Remove, $"Unsupported action: {action}"); + InitializeRemove(action, changedItems, startingIndex); + } } private void InitializeAdd(NotifyCollectionChangedAction action, IList newItems, int newStartingIndex) @@ -305,70 +341,37 @@ namespace System.Collections.Specialized InitializeRemove(action, oldItems, oldStartingIndex); } - //------------------------------------------------------ - // - // Public Properties - // - //------------------------------------------------------ - /// /// The action that caused the event. /// - public NotifyCollectionChangedAction Action - { - get { return _action; } - } + public NotifyCollectionChangedAction Action => _action; /// /// The items affected by the change. /// - public IList NewItems - { - get { return _newItems; } - } + public IList NewItems => _newItems; /// /// The old items affected by the change (for Replace events). /// - public IList OldItems - { - get { return _oldItems; } - } + public IList OldItems => _oldItems; /// /// The index where the change occurred. /// - public int NewStartingIndex - { - get { return _newStartingIndex; } - } + public int NewStartingIndex => _newStartingIndex; /// /// The old index where the change occurred (for Move events). /// - public int OldStartingIndex - { - get { return _oldStartingIndex; } - } - - //------------------------------------------------------ - // - // Private Fields - // - //------------------------------------------------------ - - private NotifyCollectionChangedAction _action; - private IList _newItems, _oldItems; - private int _newStartingIndex = -1; - private int _oldStartingIndex = -1; + public int OldStartingIndex => _oldStartingIndex; } /// - /// The delegate to use for handlers that receive the CollectionChanged event. + /// The delegate to use for handlers that receive the CollectionChanged event. /// public delegate void NotifyCollectionChangedEventHandler(object sender, NotifyCollectionChangedEventArgs e); - internal sealed class ReadOnlyList : IList { private readonly IList _list; @@ -376,46 +379,24 @@ namespace System.Collections.Specialized internal ReadOnlyList(IList list) { Debug.Assert(list != null); - _list = list; } - public int Count - { - get { return _list.Count; } - } + public int Count => _list.Count; - public bool IsReadOnly - { - get { return true; } - } + public bool IsReadOnly => true; - public bool IsFixedSize - { - get { return true; } - } + public bool IsFixedSize => true; - public bool IsSynchronized - { - get { return _list.IsSynchronized; } - } + public bool IsSynchronized => _list.IsSynchronized; public object this[int index] { - get - { - return _list[index]; - } - set - { - throw new NotSupportedException(SR.NotSupported_ReadOnlyCollection); - } + get => _list[index]; + set => throw new NotSupportedException(SR.NotSupported_ReadOnlyCollection); } - public object SyncRoot - { - get { return _list.SyncRoot; } - } + public object SyncRoot => _list.SyncRoot; public int Add(object value) { @@ -427,25 +408,16 @@ namespace System.Collections.Specialized throw new NotSupportedException(SR.NotSupported_ReadOnlyCollection); } - public bool Contains(object value) - { - return _list.Contains(value); - } + public bool Contains(object value) => _list.Contains(value); public void CopyTo(Array array, int index) { _list.CopyTo(array, index); } - public IEnumerator GetEnumerator() - { - return _list.GetEnumerator(); - } + public IEnumerator GetEnumerator() => _list.GetEnumerator(); - public int IndexOf(object value) - { - return _list.IndexOf(value); - } + public int IndexOf(object value) => _list.IndexOf(value); public void Insert(int index, object value) { diff --git a/src/libraries/System.ObjectModel/src/System/ComponentModel/DataErrorsChangedEventArgs.cs b/src/libraries/System.ObjectModel/src/System/ComponentModel/DataErrorsChangedEventArgs.cs index 79c94fe..2ff2da8 100644 --- a/src/libraries/System.ObjectModel/src/System/ComponentModel/DataErrorsChangedEventArgs.cs +++ b/src/libraries/System.ObjectModel/src/System/ComponentModel/DataErrorsChangedEventArgs.cs @@ -2,37 +2,25 @@ // 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; -using System.Diagnostics; - namespace System.ComponentModel { - /// - /// Provides data for the - /// event. - /// + /// + /// Provides data for the event. + /// public class DataErrorsChangedEventArgs : EventArgs { - private readonly string _propertyName; - - /// - /// Initializes a new instance of the - /// class. - /// + /// + /// Initializes a new instance of the + /// class. + /// public DataErrorsChangedEventArgs(string propertyName) { - _propertyName = propertyName; + PropertyName = propertyName; } - /// - /// Indicates the name of the property whose errors changed. - /// - public virtual string PropertyName - { - get - { - return _propertyName; - } - } + /// + /// Indicates the name of the property whose errors changed. + /// + public virtual string PropertyName { get; } } } diff --git a/src/libraries/System.ObjectModel/src/System/ComponentModel/INotifyDataErrorInfo.cs b/src/libraries/System.ObjectModel/src/System/ComponentModel/INotifyDataErrorInfo.cs index 1108b5c..fb0d5a4 100644 --- a/src/libraries/System.ObjectModel/src/System/ComponentModel/INotifyDataErrorInfo.cs +++ b/src/libraries/System.ObjectModel/src/System/ComponentModel/INotifyDataErrorInfo.cs @@ -2,7 +2,6 @@ // 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; using System.Collections; namespace System.ComponentModel diff --git a/src/libraries/System.ObjectModel/src/System/ComponentModel/PropertyChangedEventArgs.cs b/src/libraries/System.ObjectModel/src/System/ComponentModel/PropertyChangedEventArgs.cs index 5ac4a93..0aaaa7b 100644 --- a/src/libraries/System.ObjectModel/src/System/ComponentModel/PropertyChangedEventArgs.cs +++ b/src/libraries/System.ObjectModel/src/System/ComponentModel/PropertyChangedEventArgs.cs @@ -2,37 +2,25 @@ // 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; -using System.Diagnostics; - namespace System.ComponentModel { - /// - /// Provides data for the - /// event. - /// + /// + /// Provides data for the event. + /// public class PropertyChangedEventArgs : EventArgs { - private readonly string _propertyName; - - /// - /// Initializes a new instance of the - /// class. - /// + /// + /// Initializes a new instance of the + /// class. + /// public PropertyChangedEventArgs(string propertyName) { - _propertyName = propertyName; + PropertyName = propertyName; } - /// - /// Indicates the name of the property that changed. - /// - public virtual string PropertyName - { - get - { - return _propertyName; - } - } + /// + /// Indicates the name of the property that changed. + /// + public virtual string PropertyName { get; } } } diff --git a/src/libraries/System.ObjectModel/src/System/ComponentModel/PropertyChangedEventHandler.cs b/src/libraries/System.ObjectModel/src/System/ComponentModel/PropertyChangedEventHandler.cs index 860b943..950b6cd 100644 --- a/src/libraries/System.ObjectModel/src/System/ComponentModel/PropertyChangedEventHandler.cs +++ b/src/libraries/System.ObjectModel/src/System/ComponentModel/PropertyChangedEventHandler.cs @@ -2,15 +2,11 @@ // 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; -using System.Diagnostics; - namespace System.ComponentModel { - /// - /// Represents the method that will handle the - /// event raised when a - /// property is changed on a component. - /// + /// + /// Represents the method that will handle the + /// event raised when a property is changed on a component. + /// public delegate void PropertyChangedEventHandler(object sender, PropertyChangedEventArgs e); } diff --git a/src/libraries/System.ObjectModel/src/System/ComponentModel/PropertyChangingEventArgs.cs b/src/libraries/System.ObjectModel/src/System/ComponentModel/PropertyChangingEventArgs.cs index 7a80aa6..17a33ca 100644 --- a/src/libraries/System.ObjectModel/src/System/ComponentModel/PropertyChangingEventArgs.cs +++ b/src/libraries/System.ObjectModel/src/System/ComponentModel/PropertyChangingEventArgs.cs @@ -2,36 +2,25 @@ // 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; - namespace System.ComponentModel { - /// - /// Provides data for the - /// event. - /// + /// + /// Provides data for the event. + /// public class PropertyChangingEventArgs : EventArgs { - private readonly string _propertyName; - - /// - /// Initializes a new instance of the - /// class. - /// + /// + /// Initializes a new instance of the + /// class. + /// public PropertyChangingEventArgs(string propertyName) { - _propertyName = propertyName; + PropertyName = propertyName; } - /// - /// Indicates the name of the property that is changing. - /// - public virtual string PropertyName - { - get - { - return _propertyName; - } - } + /// + /// Indicates the name of the property that is changing. + /// + public virtual string PropertyName { get; } } } diff --git a/src/libraries/System.ObjectModel/src/System/ComponentModel/PropertyChangingEventHandler.cs b/src/libraries/System.ObjectModel/src/System/ComponentModel/PropertyChangingEventHandler.cs index c7cac66c..3e97919 100644 --- a/src/libraries/System.ObjectModel/src/System/ComponentModel/PropertyChangingEventHandler.cs +++ b/src/libraries/System.ObjectModel/src/System/ComponentModel/PropertyChangingEventHandler.cs @@ -2,14 +2,11 @@ // 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; - namespace System.ComponentModel { - /// - /// Represents the method that will handle the - /// event raised when a - /// property is changing on a component. - /// + /// + /// Represents the method that will handle the + /// event raised when a property is changing on a component. + /// public delegate void PropertyChangingEventHandler(object sender, PropertyChangingEventArgs e); } diff --git a/src/libraries/System.ObjectModel/src/System/ComponentModel/TypeConverterAttribute.cs b/src/libraries/System.ObjectModel/src/System/ComponentModel/TypeConverterAttribute.cs index 91e8f64..cc08c4e 100644 --- a/src/libraries/System.ObjectModel/src/System/ComponentModel/TypeConverterAttribute.cs +++ b/src/libraries/System.ObjectModel/src/System/ComponentModel/TypeConverterAttribute.cs @@ -5,26 +5,21 @@ namespace System.ComponentModel { /// - /// - /// Specifies what type to use as a converter for the object this - /// attribute is bound to. This class cannot be inherited. - /// + /// Specifies what type to use as a converter for the object this attribute is + /// bound to. This class cannot be inherited. /// [AttributeUsage(AttributeTargets.All)] public sealed class TypeConverterAttribute : Attribute { /// - /// - /// Specifies the type to use as a converter for the object this attribute is bound to. This - /// field is read-only. + /// Specifies the type to use as a converter for the object this attribute is + /// bound to. This field is read-only. /// public static readonly TypeConverterAttribute Default = new TypeConverterAttribute(); /// - /// - /// Initializes a new instance of the class with the - /// default type converter, which is an empty string (""). - /// + /// Initializes a new instance of the + /// class with the default type converter, which is an empty string (""). /// public TypeConverterAttribute() { @@ -32,10 +27,9 @@ namespace System.ComponentModel } /// - /// - /// Initializes a new instance of the class, - /// using the specified type as the data converter for the object this attribute is bound to. - /// + /// Initializes a new instance of the + /// class, using the specified type as the data converter for the object this attribute + /// is bound to. /// public TypeConverterAttribute(Type type) { @@ -48,10 +42,9 @@ namespace System.ComponentModel } /// - /// - /// Initializes a new instance of the class, - /// using the specified type name as the data converter for the object this attribute is bound to. - /// + /// Initializes a new instance of the + /// class, using the specified type name as the data converter for the object this attribute + /// is bound to. /// public TypeConverterAttribute(string typeName) { @@ -64,22 +57,18 @@ namespace System.ComponentModel } /// - /// - /// Gets the fully qualified type name of the to use as a converter for - /// the object this attribute is bound to. - /// + /// Gets the fully qualified type name of the to use as a + /// converter for the object this attribute is bound to. /// public string ConverterTypeName { get; } public override bool Equals(object obj) { - TypeConverterAttribute other = obj as TypeConverterAttribute; - return (other != null) && other.ConverterTypeName == ConverterTypeName; + return + obj is TypeConverterAttribute other && + other.ConverterTypeName == ConverterTypeName; } - public override int GetHashCode() - { - return ConverterTypeName.GetHashCode(); - } + public override int GetHashCode() => ConverterTypeName.GetHashCode(); } } diff --git a/src/libraries/System.ObjectModel/src/System/Reflection/ICustomTypeProvider.cs b/src/libraries/System.ObjectModel/src/System/Reflection/ICustomTypeProvider.cs index b38c9f6..be2edec 100644 --- a/src/libraries/System.ObjectModel/src/System/Reflection/ICustomTypeProvider.cs +++ b/src/libraries/System.ObjectModel/src/System/Reflection/ICustomTypeProvider.cs @@ -9,4 +9,3 @@ namespace System.Reflection Type GetCustomType(); } } - diff --git a/src/libraries/System.ObjectModel/src/System/Windows/Input/ICommand.cs b/src/libraries/System.ObjectModel/src/System/Windows/Input/ICommand.cs index 977f0d5..b626119 100644 --- a/src/libraries/System.ObjectModel/src/System/Windows/Input/ICommand.cs +++ b/src/libraries/System.ObjectModel/src/System/Windows/Input/ICommand.cs @@ -2,7 +2,6 @@ // 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; using System.ComponentModel; using System.Runtime.CompilerServices; using System.Windows.Markup; @@ -10,7 +9,7 @@ using System.Windows.Markup; namespace System.Windows.Input { /// - /// An interface that allows an application author to define a method to be invoked. + /// An interface that allows an application author to define a method to be invoked. /// [TypeForwardedFrom("PresentationCore, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")] [TypeConverter("System.Windows.Input.CommandConverter, PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, Custom=null")] @@ -18,19 +17,19 @@ namespace System.Windows.Input public interface ICommand { /// - /// Raised when the ability of the command to execute has changed. + /// Raised when the ability of the command to execute has changed. /// event EventHandler CanExecuteChanged; /// - /// Returns whether the command can be executed. + /// Returns whether the command can be executed. /// /// A parameter that may be used in executing the command. This parameter may be ignored by some implementations. /// true if the command can be executed with the given parameter and current state. false otherwise. bool CanExecute(object parameter); /// - /// Defines the method that should be executed when the command is executed. + /// Defines the method that should be executed when the command is executed. /// /// A parameter that may be used in executing the command. This parameter may be ignored by some implementations. void Execute(object parameter); diff --git a/src/libraries/System.ObjectModel/src/System/Windows/Markup/ValueSerializerAttribute.cs b/src/libraries/System.ObjectModel/src/System/Windows/Markup/ValueSerializerAttribute.cs index 3c47335..46bca95 100644 --- a/src/libraries/System.ObjectModel/src/System/Windows/Markup/ValueSerializerAttribute.cs +++ b/src/libraries/System.ObjectModel/src/System/Windows/Markup/ValueSerializerAttribute.cs @@ -2,7 +2,6 @@ // 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; using System.Runtime.CompilerServices; namespace System.Windows.Markup @@ -18,7 +17,7 @@ namespace System.Windows.Markup public sealed class ValueSerializerAttribute : Attribute { private Type _valueSerializerType; - private string _valueSerializerTypeName; + private readonly string _valueSerializerTypeName; /// /// Constructor for the ValueSerializerAttribute @@ -46,7 +45,10 @@ namespace System.Windows.Markup get { if (_valueSerializerType == null && _valueSerializerTypeName != null) + { _valueSerializerType = Type.GetType(_valueSerializerTypeName); + } + return _valueSerializerType; } } @@ -59,9 +61,13 @@ namespace System.Windows.Markup get { if (_valueSerializerType != null) + { return _valueSerializerType.AssemblyQualifiedName; + } else + { return _valueSerializerTypeName; + } } } } diff --git a/src/libraries/System.ObjectModel/tests/KeyedCollection/ConcreteTestClasses.cs b/src/libraries/System.ObjectModel/tests/KeyedCollection/ConcreteTestClasses.cs index 0125f06..7ff3a34 100644 --- a/src/libraries/System.ObjectModel/tests/KeyedCollection/ConcreteTestClasses.cs +++ b/src/libraries/System.ObjectModel/tests/KeyedCollection/ConcreteTestClasses.cs @@ -18,21 +18,17 @@ namespace System.Collections.ObjectModel.Tests new KeyedItem("foo", 0), new KeyedItem("bar", 1) }; - AssertExtensions.Throws(null, () => collection.Add(new KeyedItem("Foo", 0))); - AssertExtensions.Throws(null, () => collection.Add(new KeyedItem("fOo", 0))); - AssertExtensions.Throws(null, () => collection.Add(new KeyedItem("baR", 0))); + AssertExtensions.Throws("key", null, () => collection.Add(new KeyedItem("Foo", 0))); + AssertExtensions.Throws("key", null, () => collection.Add(new KeyedItem("fOo", 0))); + AssertExtensions.Throws("key", null, () => collection.Add(new KeyedItem("baR", 0))); } - [Fact] - public void ThresholdThrows() + [Theory] + [InlineData(-2)] + [InlineData(int.MinValue)] + public void Ctor_InvalidDictionaryCreationThreshold_ThrowsArgumentOutOfRangeException(int dictionaryCreationThreshold) { - Assert.Throws( - () => - new TestKeyedCollectionOfIKeyedItem(-2)); - Assert.Throws( - () => - new TestKeyedCollectionOfIKeyedItem( - int.MinValue)); + AssertExtensions.Throws("dictionaryCreationThreshold", () => new TestKeyedCollectionOfIKeyedItem(dictionaryCreationThreshold)); } } diff --git a/src/libraries/System.ObjectModel/tests/KeyedCollection/TestMethods.cs b/src/libraries/System.ObjectModel/tests/KeyedCollection/TestMethods.cs index e5e1ec7..d6e4da2 100644 --- a/src/libraries/System.ObjectModel/tests/KeyedCollection/TestMethods.cs +++ b/src/libraries/System.ObjectModel/tests/KeyedCollection/TestMethods.cs @@ -368,7 +368,8 @@ namespace System.Collections.ObjectModel.Tests collection.Add(keyedItem1); - AssertExtensions.Throws(null, () => collection.Add(tmpKeyedItem)); + string expectedParamName = collectionSize < 32 || generateKeyedItem.Name != KeyedCollectionTests.GetNeverNullKeyMethod.Name ? "key" : null; + AssertExtensions.Throws(expectedParamName, null, () => collection.Add(tmpKeyedItem)); collection.Verify(keys, items, itemsWithKeys); } @@ -485,7 +486,8 @@ namespace System.Collections.ObjectModel.Tests collection.Add(keyedItem1); - AssertExtensions.Throws(null, () => nonGenericCollection.Add(tmpKeyedItem)); + string expectedParamName = collectionSize < 32 || generateKeyedItem.Name != KeyedCollectionTests.GetNeverNullKeyMethod.Name ? "key" : null; + AssertExtensions.Throws(expectedParamName, null, () => nonGenericCollection.Add(tmpKeyedItem)); collection.Verify(keys, items, itemsWithKeys); } @@ -610,7 +612,8 @@ namespace System.Collections.ObjectModel.Tests .ToArray >()); - AssertExtensions.Throws(null, () => collection.MyChangeItemKey(keyedItem2, key1)); + string expectedParamName = collectionSize < 32 || generateKeyedItem.Name != KeyedCollectionTests.GetNeverNullKeyMethod.Name ? "key" : null; + AssertExtensions.Throws(expectedParamName, null, () => collection.MyChangeItemKey(keyedItem2, key1)); collection.Verify(keys, items, itemsWithKeys); } @@ -697,10 +700,10 @@ namespace System.Collections.ObjectModel.Tests ki => ki.Key != null) .ToArray >()); - AssertExtensions.Throws(null, () => collection.MyChangeItemKey(keyedItem3, key3)); - AssertExtensions.Throws(null, () => collection.MyChangeItemKey(keyedItem3, key2)); + AssertExtensions.Throws("item", null, () => collection.MyChangeItemKey(keyedItem3, key3)); + AssertExtensions.Throws("item", null, () => collection.MyChangeItemKey(keyedItem3, key2)); var tempKeyedItem = new KeyedItem(key1, item2); - AssertExtensions.Throws(null, () => collection.MyChangeItemKey(tempKeyedItem, key2)); + AssertExtensions.Throws("item", null, () => collection.MyChangeItemKey(tempKeyedItem, key2)); collection.Verify(keys, items, itemsWithKeys); } @@ -870,7 +873,7 @@ namespace System.Collections.ObjectModel.Tests out items, out itemsWithKeys); collection.Add(item1); - AssertExtensions.Throws(null, () => collection.MyChangeItemKey(default(TValue), key2)); + AssertExtensions.Throws("item", null, () => collection.MyChangeItemKey(default(TValue), key2)); collection.Verify( keys.Push(key1), items.Push(item1), @@ -1037,7 +1040,7 @@ namespace System.Collections.ObjectModel.Tests keyedItem2.Key = key3; if (collectionSize >= 32) { - AssertExtensions.Throws(null, () => collection.MyChangeItemKey(keyedItem2, key3)); + AssertExtensions.Throws("item", null, () => collection.MyChangeItemKey(keyedItem2, key3)); } else { @@ -1084,7 +1087,7 @@ namespace System.Collections.ObjectModel.Tests keyedItem2.Key = key3; if (collectionSize >= 32) { - AssertExtensions.Throws(null, () => collection.MyChangeItemKey(keyedItem2, key2)); + AssertExtensions.Throws("item", null, () => collection.MyChangeItemKey(keyedItem2, key2)); } else { @@ -1133,7 +1136,7 @@ namespace System.Collections.ObjectModel.Tests keyedItem2.Key = key3; if (collectionSize >= 32) { - AssertExtensions.Throws(null, () => collection.MyChangeItemKey(keyedItem2, key4)); + AssertExtensions.Throws("item", null, () => collection.MyChangeItemKey(keyedItem2, key4)); } else { @@ -1192,7 +1195,7 @@ namespace System.Collections.ObjectModel.Tests tempKeyedItem.Key = key3; if (collectionSize >= 32) { - AssertExtensions.Throws(null, () => collection.MyChangeItemKey(tempKeyedItem, key3)); + AssertExtensions.Throws("item", null, () => collection.MyChangeItemKey(tempKeyedItem, key3)); } else { @@ -1252,7 +1255,7 @@ namespace System.Collections.ObjectModel.Tests tempKeyedItem.Key = key3; if (collectionSize >= 32) { - AssertExtensions.Throws(null, () => collection.MyChangeItemKey(tempKeyedItem, default(TKey))); + AssertExtensions.Throws("item", null, () => collection.MyChangeItemKey(tempKeyedItem, default(TKey))); } else { @@ -1316,7 +1319,7 @@ namespace System.Collections.ObjectModel.Tests tempKeyedItem.Key = key3; if (collectionSize >= 32) { - AssertExtensions.Throws(null, () => collection.MyChangeItemKey(tempKeyedItem, key4)); + AssertExtensions.Throws("item", null, () => collection.MyChangeItemKey(tempKeyedItem, key4)); } else { @@ -1468,7 +1471,7 @@ namespace System.Collections.ObjectModel.Tests keyedItem2.Key = default(TKey); if (collectionSize >= 32 && keyedItem2.Key != null) { - AssertExtensions.Throws(null, () => collection.MyChangeItemKey(keyedItem2, key4)); + AssertExtensions.Throws("item", null, () => collection.MyChangeItemKey(keyedItem2, key4)); } else { @@ -1531,8 +1534,7 @@ namespace System.Collections.ObjectModel.Tests out itemsWithKeys); if (s_keyNullable) { - Assert.Throws( - () => collection.Contains(default(TKey))); + AssertExtensions.Throws("key", () => collection.Contains(default(TKey))); } else { @@ -1669,8 +1671,7 @@ namespace System.Collections.ObjectModel.Tests TKey keyNotIn = itemNotIn.Key; if (keyNotIn == null) { - Assert.Throws( - () => collection.Contains(keyNotIn)); + AssertExtensions.Throws("key", () => collection.Contains(keyNotIn)); } else { @@ -1681,8 +1682,7 @@ namespace System.Collections.ObjectModel.Tests TKey key = k; if (key == null) { - Assert.Throws( - () => collection.Contains(key)); + AssertExtensions.Throws("key", () => collection.Contains(key)); continue; } Assert.True(collection.Contains(key)); @@ -1718,8 +1718,7 @@ namespace System.Collections.ObjectModel.Tests TKey keyNotIn = itemNotIn.Key; if (keyNotIn == null) { - Assert.Throws( - () => collection.Remove(keyNotIn)); + AssertExtensions.Throws("key", () => collection.Remove(keyNotIn)); } else { @@ -1736,8 +1735,7 @@ namespace System.Collections.ObjectModel.Tests TKey key = keys[i]; if (key == null) { - Assert.Throws( - () => collection.Remove(key)); + AssertExtensions.Throws("key", () => collection.Remove(key)); } else { @@ -1784,8 +1782,7 @@ namespace System.Collections.ObjectModel.Tests TKey keyNotIn = itemNotIn.Key; if (keyNotIn == null) { - Assert.Throws( - () => collection[keyNotIn]); + AssertExtensions.Throws("key", () => collection[keyNotIn]); } else { @@ -1797,8 +1794,7 @@ namespace System.Collections.ObjectModel.Tests TKey key = k; if (key == null) { - Assert.Throws( - () => collection[key]); + AssertExtensions.Throws("key", () => collection[key]); continue; } IKeyedItem tmp = collection[key]; @@ -1987,7 +1983,8 @@ namespace System.Collections.ObjectModel.Tests items = items.Push(keyedItem1); itemsWithKeys = itemsWithKeys.Push(keyedItem1); insert(collection, collection.Count, keyedItem1); - AssertExtensions.Throws(null, () => insert(collection, collection.Count, tempKeyedItem)); + string expectedParamName = collectionSize < 32 || generateKeyedItem.Name != KeyedCollectionTests.GetNeverNullKeyMethod.Name ? "key" : null; + AssertExtensions.Throws(expectedParamName, null, () => insert(collection, collection.Count, tempKeyedItem)); collection.Verify(keys, items, itemsWithKeys); } diff --git a/src/libraries/System.ObjectModel/tests/KeyedCollection/TestMethods.netcoreapp.cs b/src/libraries/System.ObjectModel/tests/KeyedCollection/TestMethods.netcoreapp.cs index a1f62ec..fc67ccf 100644 --- a/src/libraries/System.ObjectModel/tests/KeyedCollection/TestMethods.netcoreapp.cs +++ b/src/libraries/System.ObjectModel/tests/KeyedCollection/TestMethods.netcoreapp.cs @@ -39,8 +39,7 @@ namespace System.Collections.ObjectModel.Tests if (keyNotIn == null) { IKeyedItem item; - Assert.Throws( - () => collection.TryGetValue(keyNotIn, out item)); + AssertExtensions.Throws("key", () => collection.TryGetValue(keyNotIn, out item)); } else { @@ -53,8 +52,7 @@ namespace System.Collections.ObjectModel.Tests TKey key = k; if (key == null) { - Assert.Throws( - () => collection.TryGetValue(key, out item)); + AssertExtensions.Throws("key", () => collection.TryGetValue(key, out item)); continue; } Assert.True(collection.TryGetValue(key, out item)); diff --git a/src/libraries/System.ObjectModel/tests/ObservableCollection/ObservableCollection_ConstructorAndPropertyTests.cs b/src/libraries/System.ObjectModel/tests/ObservableCollection/ObservableCollection_ConstructorAndPropertyTests.cs index 8c58ae1..d01c244 100644 --- a/src/libraries/System.ObjectModel/tests/ObservableCollection/ObservableCollection_ConstructorAndPropertyTests.cs +++ b/src/libraries/System.ObjectModel/tests/ObservableCollection/ObservableCollection_ConstructorAndPropertyTests.cs @@ -107,7 +107,7 @@ namespace System.Collections.ObjectModel.Tests public static void ItemTestSet_Negative_InvalidIndex(int size, int index) { var col = new ObservableCollection(new int[size]); - Assert.Throws(() => col[index]); + AssertExtensions.Throws("index", () => col[index]); } // ICollection.IsReadOnly diff --git a/src/libraries/System.ObjectModel/tests/ObservableCollection/ObservableCollection_MethodsTest.cs b/src/libraries/System.ObjectModel/tests/ObservableCollection/ObservableCollection_MethodsTest.cs index d58bf6d..6f03a97 100644 --- a/src/libraries/System.ObjectModel/tests/ObservableCollection/ObservableCollection_MethodsTest.cs +++ b/src/libraries/System.ObjectModel/tests/ObservableCollection/ObservableCollection_MethodsTest.cs @@ -83,7 +83,7 @@ namespace System.Collections.ObjectModel.Tests Assert.Equal(0, col.Count); Assert.Empty(col); - Assert.Throws(() => col[1]); + AssertExtensions.Throws("index", () => col[1]); //tests that the collectionChanged events are fired. CollectionAndPropertyChangedTester helper = new CollectionAndPropertyChangedTester(); @@ -140,14 +140,14 @@ namespace System.Collections.ObjectModel.Tests int[] iArrInvalidValues = new int[] { -1, -2, -100, -1000, -10000, -100000, -1000000, -10000000, -100000000, -1000000000, int.MinValue }; foreach (var index in iArrInvalidValues) { - Assert.Throws(() => collection.RemoveAt(index)); + AssertExtensions.Throws("index", () => collection.RemoveAt(index)); Assert.Equal(anArray.Length, collection.Count); } int[] iArrLargeValues = new int[] { collection.Count, int.MaxValue, int.MaxValue / 2, int.MaxValue / 10 }; foreach (var index in iArrLargeValues) { - Assert.Throws(() => collection.RemoveAt(index)); + AssertExtensions.Throws("index", () => collection.RemoveAt(index)); Assert.Equal(anArray.Length, collection.Count); } } @@ -213,11 +213,11 @@ namespace System.Collections.ObjectModel.Tests collection.CollectionChanged += (o, e) => { throw new ShouldNotBeInvokedException(); }; // invalid startIndex, valid destination index. - Assert.Throws(() => collection.Move(index, validIndex)); + AssertExtensions.Throws("index", () => collection.Move(index, validIndex)); Assert.Equal(anArray.Length, collection.Count); // valid startIndex, invalid destIndex. - Assert.Throws(() => collection.Move(validIndex, index)); + AssertExtensions.Throws("index", () => collection.Move(validIndex, index)); //NOTE: It actually moves the item right out of the collection.So the count is one less. //Assert.Equal(anArray.Length, collection.Count, "Collection should not have changed. index: " + index); } @@ -228,11 +228,11 @@ namespace System.Collections.ObjectModel.Tests collection.CollectionChanged += (o, e) => { throw new ShouldNotBeInvokedException(); }; // invalid startIndex, valid destination index. - Assert.Throws(() => collection.Move(index, validIndex)); + AssertExtensions.Throws("index", () => collection.Move(index, validIndex)); Assert.Equal(anArray.Length, collection.Count); // valid startIndex, invalid destIndex. - Assert.Throws(() => collection.Move(validIndex, index)); + AssertExtensions.Throws("index", () => collection.Move(validIndex, index)); //NOTE: It actually moves the item right out of the collection. So the count is one less. //Assert.Equal(anArray.Length, collection.Count, "Collection should not have changed."); } @@ -288,14 +288,14 @@ namespace System.Collections.ObjectModel.Tests int[] iArrInvalidValues = new int[] { -1, -2, -100, -1000, -10000, -100000, -1000000, -10000000, -100000000, -1000000000, int.MinValue }; foreach (var index in iArrInvalidValues) { - Assert.Throws(() => collection.Insert(index, itemToInsert)); + AssertExtensions.Throws("index", () => collection.Insert(index, itemToInsert)); Assert.Equal(anArray.Length, collection.Count); } int[] iArrLargeValues = new int[] { collection.Count + 1, int.MaxValue, int.MaxValue / 2, int.MaxValue / 10 }; foreach (var index in iArrLargeValues) { - Assert.Throws(() => collection.Insert(index, itemToInsert)); + AssertExtensions.Throws("index", () => collection.Insert(index, itemToInsert)); Assert.Equal(anArray.Length, collection.Count); } } @@ -416,7 +416,7 @@ namespace System.Collections.ObjectModel.Tests foreach (var index in iArrInvalidValues) { string[] aCopy = new string[collection.Count]; - Assert.Throws(() => collection.CopyTo(aCopy, index)); + AssertExtensions.Throws("destinationIndex", "dstIndex", () => collection.CopyTo(aCopy, index)); } int[] iArrLargeValues = new int[] { collection.Count, int.MaxValue, int.MaxValue / 2, int.MaxValue / 10 }; @@ -426,7 +426,7 @@ namespace System.Collections.ObjectModel.Tests AssertExtensions.Throws("destinationArray", null, () => collection.CopyTo(aCopy, index)); } - Assert.Throws(() => collection.CopyTo(null, 1)); + AssertExtensions.Throws("destinationArray", "dest", () => collection.CopyTo(null, 1)); string[] copy = new string[collection.Count - 1]; AssertExtensions.Throws("destinationArray", "", () => collection.CopyTo(copy, 0)); diff --git a/src/libraries/System.ObjectModel/tests/ReadOnlyDictionary/ReadOnlyDictionaryTests.cs b/src/libraries/System.ObjectModel/tests/ReadOnlyDictionary/ReadOnlyDictionaryTests.cs index 02ef408..2ea3d90 100644 --- a/src/libraries/System.ObjectModel/tests/ReadOnlyDictionary/ReadOnlyDictionaryTests.cs +++ b/src/libraries/System.ObjectModel/tests/ReadOnlyDictionary/ReadOnlyDictionaryTests.cs @@ -62,7 +62,7 @@ namespace System.Collections.ObjectModel.Tests [Fact] public static void CtorTests_Negative() { - Assert.Throws(() => { ReadOnlyDictionary dict = new ReadOnlyDictionary(null); }); + AssertExtensions.Throws("dictionary", () => new ReadOnlyDictionary(null)); } /// diff --git a/src/libraries/System.ObjectModel/tests/ReadOnlyObservableCollection/ReadOnlyObservableCollectionTests.cs b/src/libraries/System.ObjectModel/tests/ReadOnlyObservableCollection/ReadOnlyObservableCollectionTests.cs index 8668ae3..cd32715 100644 --- a/src/libraries/System.ObjectModel/tests/ReadOnlyObservableCollection/ReadOnlyObservableCollectionTests.cs +++ b/src/libraries/System.ObjectModel/tests/ReadOnlyObservableCollection/ReadOnlyObservableCollectionTests.cs @@ -30,8 +30,7 @@ namespace System.Collections.ObjectModel.Tests [Fact] public static void Ctor_Tests_Negative() { - ReadOnlyObservableCollection collection; - Assert.Throws(() => collection = new ReadOnlyObservableCollection(null)); + AssertExtensions.Throws("list", () => new ReadOnlyObservableCollection(null)); } [Fact] @@ -127,7 +126,7 @@ namespace System.Collections.ObjectModel.Tests foreach (var index in iArrInvalidValues) { string[] aCopy = new string[anArray.Length]; - Assert.Throws(() => readOnlyCol.CopyTo(aCopy, index)); + AssertExtensions.Throws("destinationIndex", "dstIndex", () => readOnlyCol.CopyTo(aCopy, index)); } int[] iArrLargeValues = new int[] { anArray.Length, int.MaxValue, int.MaxValue / 2, int.MaxValue / 10 }; @@ -137,7 +136,7 @@ namespace System.Collections.ObjectModel.Tests AssertExtensions.Throws("destinationArray", null, () => readOnlyCol.CopyTo(aCopy, index)); } - Assert.Throws(() => readOnlyCol.CopyTo(null, 1)); + AssertExtensions.Throws("destinationArray", "dest", () => readOnlyCol.CopyTo(null, 1)); string[] copy = new string[anArray.Length - 1]; AssertExtensions.Throws("destinationArray", "", () => readOnlyCol.CopyTo(copy, 0)); @@ -268,13 +267,13 @@ namespace System.Collections.ObjectModel.Tests public void Item_get_Tests_Negative() { // Verify get_Item with index=Int32.MinValue - Assert.Throws(() => { T item = _collection[int.MinValue]; }); + AssertExtensions.Throws("index", () => _collection[int.MinValue]); // Verify that the collection was not mutated VerifyReadOnlyCollection(_collection, _expectedItems); // Verify get_Item with index=-1 - Assert.Throws(() => { T item = _collection[-1]; }); + AssertExtensions.Throws("index", () => _collection[-1]); // Verify that the collection was not mutated VerifyReadOnlyCollection(_collection, _expectedItems); @@ -282,7 +281,7 @@ namespace System.Collections.ObjectModel.Tests if (_expectedItems.Length == 0) { // Verify get_Item with index=0 on Empty collection - Assert.Throws(() => { T item = _collection[0]; }); + AssertExtensions.Throws("index", () => _collection[0]); // Verify that the collection was not mutated VerifyReadOnlyCollection(_collection, _expectedItems); @@ -290,7 +289,7 @@ namespace System.Collections.ObjectModel.Tests else { // Verify get_Item with index=Count on Empty collection - Assert.Throws(() => { T item = _collection[_expectedItems.Length]; }); + AssertExtensions.Throws("index", () => _collection[_expectedItems.Length]); // Verify that the collection was not mutated VerifyReadOnlyCollection(_collection, _expectedItems); diff --git a/src/libraries/System.ObjectModel/tests/ReadOnlyObservableCollection/ReadOnlyObservableCollection_EventsTests.cs b/src/libraries/System.ObjectModel/tests/ReadOnlyObservableCollection/ReadOnlyObservableCollection_EventsTests.cs index 8808eae..b869ba3 100644 --- a/src/libraries/System.ObjectModel/tests/ReadOnlyObservableCollection/ReadOnlyObservableCollection_EventsTests.cs +++ b/src/libraries/System.ObjectModel/tests/ReadOnlyObservableCollection/ReadOnlyObservableCollection_EventsTests.cs @@ -119,14 +119,14 @@ namespace System.Collections.ObjectModel.Tests int[] iArrInvalidValues = new int[] { -1, -2, -100, -1000, -10000, -100000, -1000000, -10000000, -100000000, -1000000000, int.MinValue }; foreach (var index in iArrInvalidValues) { - Assert.Throws(() => collection.RemoveAt(index)); + AssertExtensions.Throws("index", () => collection.RemoveAt(index)); Assert.Equal(anArray.Length, readonlyCol.Count); } int[] iArrLargeValues = new int[] { collection.Count, int.MaxValue, int.MaxValue / 2, int.MaxValue / 10 }; foreach (var index in iArrLargeValues) { - Assert.Throws(() => collection.RemoveAt(index)); + AssertExtensions.Throws("index", () => collection.RemoveAt(index)); Assert.Equal(anArray.Length, readonlyCol.Count); } } @@ -171,14 +171,14 @@ namespace System.Collections.ObjectModel.Tests foreach (var index in iArrInvalidValues) { // invalid startIndex, valid destination index. - Assert.Throws(() => collection.Move(index, validIndex)); + AssertExtensions.Throws("index", () => collection.Move(index, validIndex)); Assert.Equal(anArray.Length, collection.Count); } foreach (var index in iArrLargeValues) { // invalid startIndex, valid destination index. - Assert.Throws(() => collection.Move(index, validIndex)); + AssertExtensions.Throws("index", () => collection.Move(index, validIndex)); Assert.Equal(anArray.Length, collection.Count); } } @@ -217,14 +217,14 @@ namespace System.Collections.ObjectModel.Tests int[] iArrInvalidValues = new int[] { -1, -2, -100, -1000, -10000, -100000, -1000000, -10000000, -100000000, -1000000000, int.MinValue }; foreach (var index in iArrInvalidValues) { - Assert.Throws(() => collection.Insert(index, itemToInsert)); + AssertExtensions.Throws("index", () => collection.Insert(index, itemToInsert)); Assert.Equal(anArray.Length, collection.Count); } int[] iArrLargeValues = new int[] { collection.Count + 1, int.MaxValue, int.MaxValue / 2, int.MaxValue / 10 }; foreach (var index in iArrLargeValues) { - Assert.Throws(() => collection.Insert(index, itemToInsert)); + AssertExtensions.Throws("index", () => collection.Insert(index, itemToInsert)); Assert.Equal(anArray.Length, collection.Count); } } diff --git a/src/libraries/System.ObjectModel/tests/System/ComponentModel/TypeConverterAttributeTests.cs b/src/libraries/System.ObjectModel/tests/System/ComponentModel/TypeConverterAttributeTests.cs index 0249547..a16f92e 100644 --- a/src/libraries/System.ObjectModel/tests/System/ComponentModel/TypeConverterAttributeTests.cs +++ b/src/libraries/System.ObjectModel/tests/System/ComponentModel/TypeConverterAttributeTests.cs @@ -51,7 +51,7 @@ namespace System.ComponentModel.Tests [SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, ".NET Framework has a bug and throws NRE")] public void Ctor_NullTypeNetCore_ThrowsArgumentNullException() { - Assert.Throws("type", () => new TypeConverterAttribute((Type)null)); + AssertExtensions.Throws("type", () => new TypeConverterAttribute((Type)null)); } [Fact] -- 2.7.4