diff options
author | Jan Tattermusch <jtattermusch@users.noreply.github.com> | 2015-06-23 12:09:21 -0700 |
---|---|---|
committer | Jan Tattermusch <jtattermusch@users.noreply.github.com> | 2015-06-23 12:09:21 -0700 |
commit | 659a43b5935f4afc9d00e1dd742e4210e000ab53 (patch) | |
tree | 8cc30b648793795d3eb72df7130c241d094df318 /csharp/src/ProtocolBuffers/Collections/RepeatedField.cs | |
parent | 45b70328f218dc2b3e20191c2cfa92872ef10d04 (diff) | |
parent | 785e13e3c002ccd0a1174839f47041ef81a33640 (diff) | |
download | protobuf-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.cs | 30 |
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); |