From 89c99b789d2989bb46df4e14556b51554f1b6048 Mon Sep 17 00:00:00 2001 From: Andrew Arnott Date: Thu, 6 Nov 2014 08:16:28 -0800 Subject: [PATCH] Add indexer to ImmutableSortedSet`1+Builder class. The indexer is available on the immutable type, so its absence from the Builder is notable. Fortunately, it's simple to correct. New tests included. --- .../Immutable/ImmutableSortedSet`1+Builder.cs | 14 ++++++++++++++ .../tests/ImmutableSortedSetBuilderTest.cs | 12 ++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet`1+Builder.cs b/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet`1+Builder.cs index 4add6ac30..7a81b8950 100644 --- a/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet`1+Builder.cs +++ b/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet`1+Builder.cs @@ -97,6 +97,20 @@ namespace System.Collections.Immutable #endregion + /// + /// Gets the element of the set at the given index. + /// + /// The 0-based index of the element in the set to return. + /// The element at the given position. + /// + /// No index setter is offered because the element being replaced may not sort + /// to the same position in the sorted collection as the replacing element. + /// + public T this[int index] + { + get { return this.root[index]; } + } + /// /// Gets the maximum value in the collection, as defined by the comparer. /// diff --git a/src/System.Collections.Immutable/tests/ImmutableSortedSetBuilderTest.cs b/src/System.Collections.Immutable/tests/ImmutableSortedSetBuilderTest.cs index 22043a0c2..2408affc3 100644 --- a/src/System.Collections.Immutable/tests/ImmutableSortedSetBuilderTest.cs +++ b/src/System.Collections.Immutable/tests/ImmutableSortedSetBuilderTest.cs @@ -313,5 +313,17 @@ namespace System.Collections.Immutable.Test Assert.NotNull(builder.SyncRoot); Assert.Same(builder.SyncRoot, builder.SyncRoot); } + + [Fact] + public void Indexer() + { + var builder = ImmutableSortedSet.Create(1, 3, 2).ToBuilder(); + Assert.Equal(1, builder[0]); + Assert.Equal(2, builder[1]); + Assert.Equal(3, builder[2]); + + Assert.Throws(() => builder[-1]); + Assert.Throws(() => builder[3]); + } } }