Skip to content

Commit

Permalink
Refactor AutoCompleteStringCollection to use List<string> instead…
Browse files Browse the repository at this point in the history
… of `ArrayList` (#8142)

* refactor AutoCompleteStringCollection to use List<string> instead of ArrayList

* Update src/System.Windows.Forms/src/System/Windows/Forms/AutoCompleteStringCollection.cs

* Changes from review

Co-authored-by: Igor Velikorossov <[email protected]>
  • Loading branch information
elachlan and RussKie authored Nov 10, 2022
1 parent 77f47d3 commit 094bbe9
Showing 1 changed file with 23 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace System.Windows.Forms
public class AutoCompleteStringCollection : IList
{
CollectionChangeEventHandler onCollectionChanged;
private readonly ArrayList data = new ArrayList();
private readonly List<string> data = new();

public AutoCompleteStringCollection()
{
Expand All @@ -28,7 +28,7 @@ public string this[int index]
{
get
{
return ((string)data[index]);
return data[index];
}
set
{
Expand All @@ -42,48 +42,28 @@ public string this[int index]
/// Gets the number of strings in the
/// <see cref="AutoCompleteStringCollection"/> .
/// </summary>
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
{
add => onCollectionChanged += value;
remove => onCollectionChanged -= value;
}

protected void OnCollectionChanged(CollectionChangeEventArgs e)
{
onCollectionChanged?.Invoke(this, e);
}
protected void OnCollectionChanged(CollectionChangeEventArgs e) => onCollectionChanged?.Invoke(this, e);

/// <summary>
/// Adds a string with the specified value to the
/// <see cref="AutoCompleteStringCollection"/> .
/// </summary>
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;
}
Expand Down Expand Up @@ -114,28 +94,19 @@ public void Clear()
/// <see cref="AutoCompleteStringCollection"/> contains a string with the specified
/// value.
/// </summary>
public bool Contains(string value)
{
return data.Contains(value);
}
public bool Contains(string value) => data.Contains(value);

/// <summary>
/// Copies the <see cref="AutoCompleteStringCollection"/> values to a one-dimensional <see cref="Array"/> instance at the
/// specified index.
/// </summary>
public void CopyTo(string[] array, int index)
{
data.CopyTo(array, index);
}
public void CopyTo(string[] array, int index) => data.CopyTo(array, index);

/// <summary>
/// Returns the index of the first occurrence of a string in
/// the <see cref="AutoCompleteStringCollection"/> .
/// </summary>
public int IndexOf(string value)
{
return data.IndexOf(value);
}
public int IndexOf(string value) => data.IndexOf(value);

/// <summary>
/// Inserts a string into the <see cref="AutoCompleteStringCollection"/> at the specified
Expand All @@ -150,25 +121,13 @@ public void Insert(int index, string value)
/// <summary>
/// Gets a value indicating whether the <see cref="AutoCompleteStringCollection"/> is read-only.
/// </summary>
public bool IsReadOnly
{
get
{
return false;
}
}
public bool IsReadOnly => false;

/// <summary>
/// Gets a value indicating whether access to the <see cref="AutoCompleteStringCollection"/>
/// is synchronized (thread-safe).
/// </summary>
public bool IsSynchronized
{
get
{
return false;
}
}
public bool IsSynchronized => false;

/// <summary>
/// Removes a specific string from the <see cref="AutoCompleteStringCollection"/> .
Expand All @@ -184,7 +143,7 @@ public void Remove(string value)
/// </summary>
public void RemoveAt(int index)
{
string value = (string)data[index];
string value = data[index];
data.RemoveAt(index);
OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Remove, value));
}
Expand All @@ -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();
}
}

0 comments on commit 094bbe9

Please sign in to comment.