Suggestions for improving C # program 1: Reasons Why ICloneable is not available

Source: Internet
Author: User

Okay, I admit that this is a title. The actual situation is: I cannot find a reason why I cannot use ICloneable. In fact, the interface ICloneable may cause misunderstanding because it only has one Clone method.

We all know that object copying can be divided into shortest copy and deep copy. ICloneable has only one Clone method, so that we cannot identify which copy is used from the perspective of naming.

Shallow copy: copy the field of the object to the copy (new object), and assign the value to the field. However, the reference type field only copies the reference, rather than the reference type itself. This means that the value of the field of the source object reference type changes, which will affect the corresponding value in the copy;

Deep copy: copy the fields of the object to the copy (new object). Both the value type and the reference type field copy the values of the type. This means that the value of the field of the source object reference type has changed and does not affect the corresponding value in the copy;

So the problem arises: If the type inherits the ICloneable interface, the Clone in the type is a shortest copy or a deep copy. Microsoft's explanation is: You can implement both the shortest copy and the deep copy in the Clone method. So why don't we provide two methods directly? For example, DeepClone or ShallowClone. Or, for general type creation, as long as the implementation of the shallow copy does not need to implement the deep copy (or vice versa), so we do not need to provide two methods.

The following is an example that implements both shallow copy and deep copy:

Code
 [Serializable]
ClassEmployee: ICloneable
{
Public StringIDCode {Get;Set;}
Public IntAge {Get;Set;}
PublicDepartment {Get;Set;}

# RegionICloneable Member

Public ObjectClone ()
{
Return This. MemberwiseClone ();
}

# Endregion

PublicEmployee DeepClone ()
{
Using(Stream objectStream= NewMemoryStream ())
{
IFormatter formatter= NewBinaryFormatter ();
Formatter. Serialize (objectStream,This);
ObjectStream. Seek (0, SeekOrigin. Begin );
ReturnFormatter. Deserialize (objectStream)AsEmployee;
}
}

PublicEmployee ShallowClone ()
{
ReturnClone ()AsEmployee;
}
}

In fact, ICloneable also brings about a problem (this problem Bill Wagner has discussed in Effcitive c #): If the type inherits from ICloneable, but it is not a Sealed type at the same time, the default Clone method of their subclass brings about a BUG (the Clone method of the subclass will return a copy of the parent class, rather than the subclass itself ). This forces all subclasses to rewrite the Clone method;

Another problem with the ICloneable Clone method is that it is not of type security and it returns an Object. When using it, it is also designed for transformation, the Clone method we implemented can avoid this problem (such as the above Code ).

To sum up, the type does not need to inherit the ICloneable interface. If the type itself needs to implement the copy function, simply publish the method. If you think this interface must be implemented in the application, let me know.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.