Questions tagged [icloneable]

Questions regarding .NET ICloneable Interface

.NET ICloneable Interface

Supports cloning, which creates a new instance of a class with the same value as an existing instance.

Docs: https://msdn.microsoft.com/en-us/library/system.icloneable%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

Famous question: Proper way to implement ICloneable

47 questions
260
votes
8 answers

Why no ICloneable?

Is there a particular reason why a generic ICloneable does not exist? It would be much more comfortable, if I would not need to cast it everytime I clone something.
Bluenuance
  • 4,813
  • 4
  • 23
  • 19
117
votes
4 answers

Why should I implement ICloneable in c#?

Can you explain to me why I should inherit from ICloneable and implement the Clone() method? If I want to do a deep copy, can't I just implement my method? Let's say MyClone()? Why should I inherit from ICloneable? What are the advantages? Is it…
uinc
  • 1,213
  • 2
  • 8
  • 6
76
votes
7 answers

Proper way to implement ICloneable

What is the proper way of implementing ICloneable in a class hierarchy? Say I have an abstract class DrawingObject. Another abstract class RectangularObject inherits from DrawingObject. Then there are multiple concrete classes like Shape, Text,…
dotNET
  • 33,414
  • 24
  • 162
  • 251
21
votes
3 answers

Why does String.Clone() returns the original string and not a copy of it?

Surprisingly, String.Clone() doesn't return a copy of a string as String.Copy() would do. Instead, it returns 'this', the original string. I would like to understand why the .Net Framework team choose to go this way. As per MSDN: The ICloneable…
Sylvain Rodrigue
  • 4,751
  • 5
  • 53
  • 67
15
votes
4 answers

How to workaround missing ICloneable interface when porting .NET library to PCL?

I am porting an existing .NET class library to a Portable Class Library. The .NET library makes extensive use of the ICloneable interface, which is not included in the portable subset. Typically, I am faced with class definitions like this in the…
Anders Gustafsson
  • 15,837
  • 8
  • 56
  • 114
7
votes
6 answers

Will disposable object clone cause memory leak in C#?

Check this code: .. class someclass : IDisposable{ private Bitmap imageObject; public void ImageCrop(int X, int Y, int W, int H) { imageObject = imageObject.Clone(new Rectangle(X, Y, W, H), imageObject.PixelFormat); } …
Eric Yin
  • 8,737
  • 19
  • 77
  • 118
6
votes
3 answers

Clone a List(Of Class)

I've done some reading and cant seem to wrap my head around what the best approach would be to clone a List(of class) in my VB2010 project. I have three classes that are related like so Public Class City 'here are many fields of type string and…
sinDizzy
  • 1,300
  • 7
  • 28
  • 60
5
votes
4 answers

How to use ICloneable when T is List?

I have the following: public class InstanceList : List {} I would like to make this cloneable. Following the example here: Why no ICloneable? I tried the following: public interface ICloneable : ICloneable Where T :…
sapbucket
  • 6,795
  • 15
  • 57
  • 94
5
votes
1 answer

How do I make a derived class cloneable?

I am attempting to create a base class and a derived class that both implement ICloneable. It seems to me that the base class Clone method should take care of all the base class properties and the derived class Clone method all of the derived class…
Brian Hooper
  • 21,544
  • 24
  • 88
  • 139
4
votes
5 answers

cloning an object in C#

I want to clone an object with using the ICloneable interface and for some reason I can't clone in my program. Here is my code: public class GeoInfo : ICloneable { private long InfoID; private string InfoName; private Location…
Nadav
  • 2,589
  • 9
  • 44
  • 63
4
votes
2 answers

Resharper, ICloneable and never null

Resharper complains about the following code, saying that the last null check is redundant as the 'expression is always false': ICloneable data = item as ICloneable; if (data == null) throw new InvalidCastException("blah blah, some error…
David Rutten
  • 4,716
  • 6
  • 43
  • 72
4
votes
1 answer

Is it possible to force ReSharper to implement ICloneable interface?

Is there a possibility to use ReSharper 6.1 to to make my class implement ICloneable interface for me? How?
pencilCake
  • 51,323
  • 85
  • 226
  • 363
4
votes
1 answer

Cloneable in Derived Classes

Assume I have a class A, and B which derives from A: class A : ICloneable { public object Clone() {...} } class B : A, ICloneable { public object Clone() {...} } which gives 'B.Clone()' hides inherited member 'A.Clone()'. Use the new…
paul simmons
  • 5,568
  • 13
  • 51
  • 78
3
votes
1 answer

C# Cloning including Lambda Expression

I've got a Lambda Expression which is a function that does some operation in a previous context. Consequently, I will need to clone this object and carry over the expression to the new context, but I am concerned that there would be local values…
Michael
  • 57
  • 1
  • 7
3
votes
4 answers

ICloneable question

If a class implemented ICloneable, what does that mean?
user496949
  • 83,087
  • 147
  • 309
  • 426
1
2 3 4