From 1d1ed62e19a6329aa91692daae2237e0c7903656 Mon Sep 17 00:00:00 2001 From: Lachlan Ennis <2433737+elachlan@users.noreply.github.com> Date: Wed, 9 Nov 2022 10:33:38 +1000 Subject: [PATCH 1/3] refactor AutoCompleteStringCollection to use List instead of ArrayList --- .../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..5bed9efa40f 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 List(); 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.LastIndexOf(value); 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(); } } From 8ae333ded61d7495aaab6ccdd7128b288dd99dae Mon Sep 17 00:00:00 2001 From: Igor Velikorossov Date: Thu, 10 Nov 2022 16:09:54 +1100 Subject: [PATCH 2/3] Update src/System.Windows.Forms/src/System/Windows/Forms/AutoCompleteStringCollection.cs --- .../src/System/Windows/Forms/AutoCompleteStringCollection.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 5bed9efa40f..d3c8b61944a 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 List data = new List(); + private readonly List data = new(); public AutoCompleteStringCollection() { From d64be02a23dd122e4d29ae808616c1f92d83cc9e Mon Sep 17 00:00:00 2001 From: Lachlan Ennis <2433737+elachlan@users.noreply.github.com> Date: Fri, 11 Nov 2022 06:36:31 +1000 Subject: [PATCH 3/3] Changes from review --- .../src/System/Windows/Forms/AutoCompleteStringCollection.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 5bed9efa40f..7417bcb5c7d 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/AutoCompleteStringCollection.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/AutoCompleteStringCollection.cs @@ -63,7 +63,7 @@ public event CollectionChangeEventHandler CollectionChanged public int Add(string value) { data.Add(value); - int index = data.LastIndexOf(value); + int index = data.Count - 1; OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Add, value)); return index; }