Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor AutoCompleteStringCollection to use List<string> instead of ArrayList #8142

Merged
merged 4 commits into from
Nov 10, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.LastIndexOf(value);
elachlan marked this conversation as resolved.
Show resolved Hide resolved
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();
}
}