I'm implementing ICloneable
on a ton of custom objects. Some of the objects have a type of DateTime
, which is a struct value. I know this value can be copied using newDateTime = oldDateTime
, but if I use MemberwiseClone()
on my object, does it automatically copy the DateTime
objects? I assume not because DateTime
does not implement ICloneable
itself.
Asked
Active
Viewed 2,082 times
3

Frank V
- 25,141
- 34
- 106
- 144

KrisSodroski
- 2,796
- 3
- 24
- 39
2 Answers
5
DateTime
is a value type, like int
. So, it will be copied by the MemberwiseClone().

alex
- 12,464
- 3
- 46
- 67
3
Since DateTime
is value type, it will be copied.
Object.MemberwiseClone Method - MSDN
The MemberwiseClone method creates a shallow copy by creating a new object, and then copying the nonstatic fields of the current object to the new object. If a field is a value type, a bit-by-bit copy of the field is performed. If a field is a reference type, the reference is copied but the referred object is not; therefore, the original object and its clone refer to the same object.

Habib
- 219,104
- 29
- 407
- 436