Review
Your code does not clone a collection. It creates one and assigns the values of the specified source collection to it. It also lacks proper generics to make this usable for other types of collections.
public T CloneCollection<T>(IEmployees employees) where T : IEmployees, new()
{
// creates a target collection
T collection = new T();
// assigns items from source to target
foreach (IEmployee employee in employees)
{
collection.Add(employee);
}
return collection;
}
Cloning a collection is ambiguously defined. It does not state whether the items should be cloned as well.
Shallow Clone
A shallow clone of a collection is a new instance, holding references to the same items as the source collection.
public static T ShallowClone<T, TItem>(T source) where T : ICollection<TItem>, new()
{
var target = new T();
foreach (var item in source)
{
target.Add(item);
}
return target;
}
I don't really see a use case for this. It's better using common collections or creating overloaded constructors on custom collections that take IEnumerable<T>
as input. If your custom collection inherits from List<T>
you should take advantage of AddRange
.
Deep Clone
A deep clone of a collection is a new instance with clones of the source items. Make sure the items implement ICloneable
. If you want to provide deep-cloning for items that do not implement this interface, you'd have to create an overload of DeepClone
that uses a default strategy for deep-cloning items. This is elaborated in this post.
public static T DeepClone<T, TItem>(T source)
where T : ICollection<TItem>, new()
where TItem : ICloneable
{
var target = new T();
foreach (var item in source)
{
target.Add(item == null ? null : item.Clone());
}
return target;
}
Checking input for null is left out for brevity.
HumanBeing
, or some such base class - perhaps anabstract
class - and implementClone
there. And perhaps the way to clone in this case is toserialize
the object and thenun-serialize
it; which of course effectively makes a copy. \$\endgroup\$