aboutsummaryrefslogtreecommitdiff
path: root/csharp/src/ProtocolBuffers/Collections/RepeatedField.cs
diff options
context:
space:
mode:
authorJon Skeet <jonskeet@google.com>2015-06-23 11:54:19 +0100
committerJon Skeet <jonskeet@google.com>2015-06-23 12:42:20 +0100
commit6c1fe6ea3e4e3915fc4164c43230210f9a0ac24f (patch)
tree3acd6ea7eebd05c6e12958ec3cf901d098ad9b14 /csharp/src/ProtocolBuffers/Collections/RepeatedField.cs
parent45b70328f218dc2b3e20191c2cfa92872ef10d04 (diff)
downloadprotobuf-6c1fe6ea3e4e3915fc4164c43230210f9a0ac24f.tar.gz
protobuf-6c1fe6ea3e4e3915fc4164c43230210f9a0ac24f.tar.bz2
protobuf-6c1fe6ea3e4e3915fc4164c43230210f9a0ac24f.zip
Implement Clone.
Fixes issue #527.
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);