Skip to content

Commit

Permalink
Implement IList<string> and IReadOnlyList<string>
Browse files Browse the repository at this point in the history
  • Loading branch information
justinvp committed Jan 8, 2015
1 parent 616cf19 commit 77ab2c1
Showing 1 changed file with 40 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@

using System.Diagnostics;
using System.Collections;
using System.Collections.Generic;

namespace System.Collections.Specialized
{
/// <devdoc>
/// <para>Represents a collection of strings.</para>
/// </devdoc>
public class StringCollection : IList
public class StringCollection : IList<string>, IReadOnlyList<string>, IList
{
private readonly ArrayList _data = new ArrayList();

Expand Down Expand Up @@ -56,7 +57,6 @@ bool IList.IsFixedSize
}
}


/// <devdoc>
/// <para>Adds a string with the specified value to the
/// <see cref='System.Collections.Specialized.StringCollection'/> .</para>
Expand Down Expand Up @@ -112,7 +112,8 @@ public void CopyTo(string[] array, int index)
/// </devdoc>
public StringEnumerator GetEnumerator()
{
return new StringEnumerator(this);
IEnumerator enumerator = _data.GetEnumerator();
return new StringEnumerator(enumerator);
}

/// <devdoc>
Expand Down Expand Up @@ -207,7 +208,6 @@ bool IList.Contains(object value)
return Contains((string)value);
}


int IList.IndexOf(object value)
{
return IndexOf((string)value);
Expand All @@ -232,17 +232,38 @@ IEnumerator IEnumerable.GetEnumerator()
{
return _data.GetEnumerator();
}

void ICollection<string>.Add(string item)
{
Add(item);
}

bool ICollection<string>.Remove(string item)
{
int index = IndexOf(item);
if (index >= 0)
{
RemoveAt(index);
return true;
}

return false;
}

IEnumerator<string> IEnumerable<string>.GetEnumerator()
{
return GetEnumerator();
}
}

public class StringEnumerator
public class StringEnumerator : IEnumerator<string>
{
private System.Collections.IEnumerator _baseEnumerator;
private System.Collections.IEnumerable _temp;
private readonly IEnumerator _baseEnumerator;

internal StringEnumerator(StringCollection mappings)
internal StringEnumerator(IEnumerator enumerator)
{
_temp = (IEnumerable)(mappings);
_baseEnumerator = _temp.GetEnumerator();
Debug.Assert(enumerator != null, "enumerator must not be null.");
_baseEnumerator = enumerator;
}

public string Current
Expand All @@ -262,6 +283,15 @@ public void Reset()
{
_baseEnumerator.Reset();
}

object IEnumerator.Current
{
get { return _baseEnumerator.Current; }
}

void IDisposable.Dispose()
{
}
}
}

0 comments on commit 77ab2c1

Please sign in to comment.