Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8244341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:49:21+00:00 2026-06-07T21:49:21+00:00

I came across few articles / explanations on shallow copying and deep copy of

  • 0

I came across few articles / explanations on shallow copying and deep copy of hashtables, the more I read, the more I am confused.

Hashtable ht = new Hashtable();
ht.Add("1", "hello");

Hashtable ht2 = new Hashtable();
ht2 = ht;                          // case1: is this shallow copy?
ht2["1"] = "H2";

Hashtable ht3 = new Hashtable(ht); // case2: is this shallow copy?
ht3["1"] = "H3";

Hashtable ht4 = new Hashtable();
ht4 = (Hashtable)ht.Clone();       // case3: is this shallow copy?
ht4["1"] = "H4";
  • Case1: result, ht content change become same with ht2.
  • Case2: result, ht content different with ht3.
  • Case3: result, ht content different with ht4.

If Case2 and Case3 are shallow copying, shouldn’t the result be the same as Case1?

Does this happen to List, ArrayList, etc as well?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T21:49:23+00:00Added an answer on June 7, 2026 at 9:49 pm

    In case 1, both ht2 and ht refer to the same instance of Hashtable.

    In cases 2 and 3, ht3 and ht4 refer to different objects created by copying the original Hashtable entries.

    Note that even when taking a “deep” copy (creating a new mapping) you’d still be copying references. So for example:

    var original = new Dictionary<int, StringBuilder>();
    original[10] = new StringBuilder();
    
    var copy = new Dictoinary<int, StringBuilder>(original);
    copy[20] = new StringBuilder();
    
    // We have two different maps...
    Assert.IsFalse(original.ContainsKey(20));
    
    // But they both refer to a single StringBuilder in the entry for 10...
    copy[10].Append("Foo");
    Assert.AreEqual("Foo", original[10].ToString());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just came across few articles, while selecting a wiki for my personal site.
I came across a few articles like this one , which suggest that some
After getting past a few hurdles learning Git, I came across a new challenge:
i came across few articles about performance and readdir here is the php script:
I came across a few articles referring to a C++ blob. What this is?
I would like to add authentication to my Rails app. I came across few
While creating my horizontal menu using the <ul><li>....</li></ul> I came across a few behaviors
I´m studing the code of OpenCV, and I came across the next few lines:
I came across a competitor product to VisualSVN a few days back. It provides
A few years ago I came across a T-SQL technique to validate a stored

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.