C # Introduction to replication and cloning

Source: Internet
Author: User

C # Introduction to replication and cloning

If there are similarities, we will be honored. If you reprint them, please note

In C #, we will talk about using HashTable, DataTable, and other copies and clones. Let's look at the example below.

HashTable ht = null;

Ht = new HashTable ();

Foreach (string s in ht)

{

//...

}

// The key value in HashTable needs to be modified during the previous traversal. Generally, an exception is reported, prompting you that the set has been changed to XXX or something, because the set after in cannot be changed during foreach traversal.

// I should have thought of it at this time. I should copy it before convenience,

HashTable ht2 = new HashTable ();

Ht. Copy (ht2, 0 );


The above code can be debugged, but the problem arises. After I modified the key value in ht2, I found that the key value in ht was also modified, obviously this is not the result I want, and then I think a little bit, using Clone ()

Problem solving,

(Same as DataTable, DataTable dt2 = dt. Copy () copying or directly giving a value is not acceptable, it will also change the value in the original datatble,)... not complete...



// Here, you need to modify the key value in HashTable. Generally, an exception is reported, prompting you that your set has been modified XXX or something, because the set after in cannot be changed during foreach traversal.

// I should have thought of it at this time. I should have copied one copy before convenience.

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.