From 094bbe94904f2cf2bf87c7af0b5bff2076cdeec9 Mon Sep 17 00:00:00 2001 From: Lachlan Ennis <2433737+elachlan@users.noreply.github.com> Date: Fri, 11 Nov 2022 07:17:58 +1000 Subject: [PATCH] Refactor `AutoCompleteStringCollection` to use `List` instead of `ArrayList` (#8142) * refactor AutoCompleteStringCollection to use List instead of ArrayList * Update src/System.Windows.Forms/src/System/Windows/Forms/AutoCompleteStringCollection.cs * Changes from review Co-authored-by: Igor Velikorossov --- .../Forms/AutoCompleteStringCollection.cs | 114 ++++-------------- 1 file changed, 23 insertions(+), 91 deletions(-) diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/AutoCompleteStringCollection.cs b/src/System.Windows.Forms/src/System/Windows/Forms/AutoCompleteStringCollection.cs index afefc9417e4..440d09718e9 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/AutoCompleteStringCollection.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/AutoCompleteStringCollection.cs @@ -15,7 +15,7 @@ namespace System.Windows.Forms public class AutoCompleteStringCollection : IList { CollectionChangeEventHandler onCollectionChanged; - private readonly ArrayList data = new ArrayList(); + private readonly List data = new(); public AutoCompleteStringCollection() { @@ -28,7 +28,7 @@ public string this[int index] { get { - return ((string)data[index]); + return data[index]; } set { @@ -42,29 +42,11 @@ public string this[int index] /// Gets the number of strings in the /// . /// - public int Count - { - get - { - return data.Count; - } - } + public int Count => data.Count; - bool IList.IsReadOnly - { - get - { - return false; - } - } + bool IList.IsReadOnly => false; - bool IList.IsFixedSize - { - get - { - return false; - } - } + bool IList.IsFixedSize => false; public event CollectionChangeEventHandler CollectionChanged { @@ -72,10 +54,7 @@ public event CollectionChangeEventHandler CollectionChanged remove => onCollectionChanged -= value; } - protected void OnCollectionChanged(CollectionChangeEventArgs e) - { - onCollectionChanged?.Invoke(this, e); - } + protected void OnCollectionChanged(CollectionChangeEventArgs e) => onCollectionChanged?.Invoke(this, e); /// /// Adds a string with the specified value to the @@ -83,7 +62,8 @@ protected void OnCollectionChanged(CollectionChangeEventArgs e) /// public int Add(string value) { - int index = data.Add(value); + data.Add(value); + int index = data.Count - 1; OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Add, value)); return index; } @@ -114,28 +94,19 @@ public void Clear() /// contains a string with the specified /// value. /// - public bool Contains(string value) - { - return data.Contains(value); - } + public bool Contains(string value) => data.Contains(value); /// /// Copies the values to a one-dimensional instance at the /// specified index. /// - public void CopyTo(string[] array, int index) - { - data.CopyTo(array, index); - } + public void CopyTo(string[] array, int index) => data.CopyTo(array, index); /// /// Returns the index of the first occurrence of a string in /// the . /// - public int IndexOf(string value) - { - return data.IndexOf(value); - } + public int IndexOf(string value) => data.IndexOf(value); /// /// Inserts a string into the at the specified @@ -150,25 +121,13 @@ public void Insert(int index, string value) /// /// Gets a value indicating whether the is read-only. /// - public bool IsReadOnly - { - get - { - return false; - } - } + public bool IsReadOnly => false; /// /// Gets a value indicating whether access to the /// is synchronized (thread-safe). /// - public bool IsSynchronized - { - get - { - return false; - } - } + public bool IsSynchronized => false; /// /// Removes a specific string from the . @@ -184,7 +143,7 @@ public void Remove(string value) /// public void RemoveAt(int index) { - string value = (string)data[index]; + string value = data[index]; data.RemoveAt(index); OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Remove, value)); } @@ -196,49 +155,22 @@ public void RemoveAt(int index) object IList.this[int index] { - get - { - return this[index]; - } - set - { - this[index] = (string)value; - } + get => this[index]; + set => this[index] = (string)value; } - int IList.Add(object value) - { - return Add((string)value); - } + int IList.Add(object value) => Add((string)value); - bool IList.Contains(object value) - { - return Contains((string)value); - } + bool IList.Contains(object value) => Contains((string)value); - int IList.IndexOf(object value) - { - return IndexOf((string)value); - } + int IList.IndexOf(object value) => IndexOf((string)value); - void IList.Insert(int index, object value) - { - Insert(index, (string)value); - } + void IList.Insert(int index, object value) => Insert(index, (string)value); - void IList.Remove(object value) - { - Remove((string)value); - } + void IList.Remove(object value) => Remove((string)value); - void ICollection.CopyTo(Array array, int index) - { - data.CopyTo(array, index); - } + void ICollection.CopyTo(Array array, int index) => data.CopyTo((string[])array, index); - public IEnumerator GetEnumerator() - { - return data.GetEnumerator(); - } + public IEnumerator GetEnumerator() => data.GetEnumerator(); } }