From b053b9211b03bc334b6b0f9cc5ef22f7c500e6cc Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Fri, 1 Jul 2016 09:46:45 +0100 Subject: Implement RepeatedField.AddRange. This fixes issue #1730. --- .../Google.Protobuf.Test/Collections/RepeatedFieldTest.cs | 10 ++++++++++ csharp/src/Google.Protobuf/Collections/RepeatedField.cs | 14 +++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/csharp/src/Google.Protobuf.Test/Collections/RepeatedFieldTest.cs b/csharp/src/Google.Protobuf.Test/Collections/RepeatedFieldTest.cs index 8ed54cfb..5d23d834 100644 --- a/csharp/src/Google.Protobuf.Test/Collections/RepeatedFieldTest.cs +++ b/csharp/src/Google.Protobuf.Test/Collections/RepeatedFieldTest.cs @@ -74,6 +74,16 @@ namespace Google.Protobuf.Collections Assert.AreEqual("bar", list[1]); } + [Test] + public void AddRange() + { + var list = new RepeatedField(); + list.AddRange(new[] { "foo", "bar" }); + Assert.AreEqual(2, list.Count); + Assert.AreEqual("foo", list[0]); + Assert.AreEqual("bar", list[1]); + } + [Test] public void Add_RepeatedField() { diff --git a/csharp/src/Google.Protobuf/Collections/RepeatedField.cs b/csharp/src/Google.Protobuf/Collections/RepeatedField.cs index 482ca9b6..a13cebdf 100644 --- a/csharp/src/Google.Protobuf/Collections/RepeatedField.cs +++ b/csharp/src/Google.Protobuf/Collections/RepeatedField.cs @@ -307,7 +307,7 @@ namespace Google.Protobuf.Collections /// Adds all of the specified values into this collection. /// /// The values to add to this collection. - public void Add(IEnumerable values) + public void AddRange(IEnumerable values) { ProtoPreconditions.CheckNotNull(values, nameof(values)); // TODO: Check for ICollection and get the Count, to optimize? @@ -317,6 +317,18 @@ namespace Google.Protobuf.Collections } } + /// + /// Adds all of the specified values into this collection. This method is present to + /// allow repeated fields to be constructed from queries within collection initializers. + /// Within non-collection-initializer code, consider using the equivalent + /// method instead for clarity. + /// + /// The values to add to this collection. + public void Add(IEnumerable values) + { + AddRange(values); + } + /// /// Returns an enumerator that iterates through the collection. /// -- cgit v1.2.3