aboutsummaryrefslogtreecommitdiff
path: root/csharp/src/ProtocolBuffers/Collections/RepeatedField.cs
diff options
context:
space:
mode:
authorJan Tattermusch <jtattermusch@users.noreply.github.com>2015-06-23 12:09:21 -0700
committerJan Tattermusch <jtattermusch@users.noreply.github.com>2015-06-23 12:09:21 -0700
commit659a43b5935f4afc9d00e1dd742e4210e000ab53 (patch)
tree8cc30b648793795d3eb72df7130c241d094df318 /csharp/src/ProtocolBuffers/Collections/RepeatedField.cs
parent45b70328f218dc2b3e20191c2cfa92872ef10d04 (diff)
parent785e13e3c002ccd0a1174839f47041ef81a33640 (diff)
downloadprotobuf-659a43b5935f4afc9d00e1dd742e4210e000ab53.tar.gz
protobuf-659a43b5935f4afc9d00e1dd742e4210e000ab53.tar.bz2
protobuf-659a43b5935f4afc9d00e1dd742e4210e000ab53.zip
Merge pull request #532 from jskeet/proto3-clone
Implement Clone.
Diffstat (limited to 'csharp/src/ProtocolBuffers/Collections/RepeatedField.cs')
-rw-r--r--csharp/src/ProtocolBuffers/Collections/RepeatedField.cs30
1 files changed, 30 insertions, 0 deletions
diff --git a/csharp/src/ProtocolBuffers/Collections/RepeatedField.cs b/csharp/src/ProtocolBuffers/Collections/RepeatedField.cs
index 25651784..5100e4db 100644
--- a/csharp/src/ProtocolBuffers/Collections/RepeatedField.cs
+++ b/csharp/src/ProtocolBuffers/Collections/RepeatedField.cs
@@ -12,6 +12,36 @@ namespace Google.Protobuf.Collections
private T[] array = EmptyArray;
private int count = 0;
+ /// <summary>
+ /// Creates a deep clone of this repeated field.
+ /// </summary>
+ /// <remarks>
+ /// If the field type is
+ /// a message type, each element is also cloned; otherwise, it is
+ /// assumed that the field type is primitive (including string and
+ /// bytes, both of which are immutable) and so a simple copy is
+ /// equivalent to a deep clone.
+ /// </remarks>
+ /// <returns>A deep clone of this repeated field.</returns>
+ public RepeatedField<T> Clone()
+ {
+ RepeatedField<T> clone = new RepeatedField<T>();
+ if (array != EmptyArray)
+ {
+ clone.array = (T[])array.Clone();
+ IDeepCloneable<T>[] cloneableArray = clone.array as IDeepCloneable<T>[];
+ if (cloneableArray != null)
+ {
+ for (int i = 0; i < count; i++)
+ {
+ clone.array[i] = cloneableArray[i].Clone();
+ }
+ }
+ }
+ clone.count = count;
+ return clone;
+ }
+
private void EnsureSize(int size)
{
size = Math.Max(size, MinArraySize);