From 77ab2c12d2b865ef99dae60dc8107984ae0fcea3 Mon Sep 17 00:00:00 2001 From: Justin Van Patten Date: Wed, 7 Jan 2015 22:35:51 -0800 Subject: [PATCH] Implement IList and IReadOnlyList --- .../Specialized/StringCollection.cs | 50 +++++++++++++++---- 1 file changed, 40 insertions(+), 10 deletions(-) diff --git a/src/System.Collections.Specialized/src/System/Collections/Specialized/StringCollection.cs b/src/System.Collections.Specialized/src/System/Collections/Specialized/StringCollection.cs index f1ee10745af1..1716e305de3d 100644 --- a/src/System.Collections.Specialized/src/System/Collections/Specialized/StringCollection.cs +++ b/src/System.Collections.Specialized/src/System/Collections/Specialized/StringCollection.cs @@ -3,13 +3,14 @@ using System.Diagnostics; using System.Collections; +using System.Collections.Generic; namespace System.Collections.Specialized { /// /// Represents a collection of strings. /// - public class StringCollection : IList + public class StringCollection : IList, IReadOnlyList, IList { private readonly ArrayList _data = new ArrayList(); @@ -56,7 +57,6 @@ bool IList.IsFixedSize } } - /// /// Adds a string with the specified value to the /// . @@ -112,7 +112,8 @@ public void CopyTo(string[] array, int index) /// public StringEnumerator GetEnumerator() { - return new StringEnumerator(this); + IEnumerator enumerator = _data.GetEnumerator(); + return new StringEnumerator(enumerator); } /// @@ -207,7 +208,6 @@ bool IList.Contains(object value) return Contains((string)value); } - int IList.IndexOf(object value) { return IndexOf((string)value); @@ -232,17 +232,38 @@ IEnumerator IEnumerable.GetEnumerator() { return _data.GetEnumerator(); } + + void ICollection.Add(string item) + { + Add(item); + } + + bool ICollection.Remove(string item) + { + int index = IndexOf(item); + if (index >= 0) + { + RemoveAt(index); + return true; + } + + return false; + } + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } } - public class StringEnumerator + public class StringEnumerator : IEnumerator { - 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 @@ -262,6 +283,15 @@ public void Reset() { _baseEnumerator.Reset(); } + + object IEnumerator.Current + { + get { return _baseEnumerator.Current; } + } + + void IDisposable.Dispose() + { + } } }