diff options
author | Jon Skeet <jonskeet@google.com> | 2016-07-01 09:46:45 +0100 |
---|---|---|
committer | Jon Skeet <jonskeet@google.com> | 2016-07-14 22:14:51 +0100 |
commit | b053b9211b03bc334b6b0f9cc5ef22f7c500e6cc (patch) | |
tree | c4bface871108174f5fd3ac43f7ff36b84a1600c /csharp | |
parent | d9334ea8d948ba591fba8b2d2f458b2629f37d93 (diff) | |
download | protobuf-b053b9211b03bc334b6b0f9cc5ef22f7c500e6cc.tar.gz protobuf-b053b9211b03bc334b6b0f9cc5ef22f7c500e6cc.tar.bz2 protobuf-b053b9211b03bc334b6b0f9cc5ef22f7c500e6cc.zip |
Implement RepeatedField.AddRange.
This fixes issue #1730.
Diffstat (limited to 'csharp')
-rw-r--r-- | csharp/src/Google.Protobuf.Test/Collections/RepeatedFieldTest.cs | 10 | ||||
-rw-r--r-- | csharp/src/Google.Protobuf/Collections/RepeatedField.cs | 14 |
2 files changed, 23 insertions, 1 deletions
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 @@ -75,6 +75,16 @@ namespace Google.Protobuf.Collections } [Test] + public void AddRange() + { + var list = new RepeatedField<string>(); + 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() { var list = new RepeatedField<string> { "original" }; 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. /// </summary> /// <param name="values">The values to add to this collection.</param> - public void Add(IEnumerable<T> values) + public void AddRange(IEnumerable<T> values) { ProtoPreconditions.CheckNotNull(values, nameof(values)); // TODO: Check for ICollection and get the Count, to optimize? @@ -318,6 +318,18 @@ namespace Google.Protobuf.Collections } /// <summary> + /// 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 <see cref="AddRange"/> + /// method instead for clarity. + /// </summary> + /// <param name="values">The values to add to this collection.</param> + public void Add(IEnumerable<T> values) + { + AddRange(values); + } + + /// <summary> /// Returns an enumerator that iterates through the collection. /// </summary> /// <returns> |