Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Deep copy Hashtable

Deep copy Hashtable

Scheduled Pinned Locked Moved C#
data-structurestutorialquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RickardB
    wrote on last edited by
    #1

    Hello, Any one who could give me an advice on how to deep copy a Hashtable in as short time as possible? My first implementation is: protected void CopyHash(ref Hashtable src, ref Hashtable dst) { dst.Clear(); Array copy = Array.CreateInstance(typeof(DictionaryEntry), src.Count); src.CopyTo(copy,0); foreach (DictionaryEntry Entry in copy) { dst.Add(Entry.Key, Entry.Value); } } Thanks Rickard

    H 1 Reply Last reply
    0
    • R RickardB

      Hello, Any one who could give me an advice on how to deep copy a Hashtable in as short time as possible? My first implementation is: protected void CopyHash(ref Hashtable src, ref Hashtable dst) { dst.Clear(); Array copy = Array.CreateInstance(typeof(DictionaryEntry), src.Count); src.CopyTo(copy,0); foreach (DictionaryEntry Entry in copy) { dst.Add(Entry.Key, Entry.Value); } } Thanks Rickard

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      First of all, you don't need to ref a Hashtable, it's already a reference Type! Second, your method above isn't a deep copy either because it doesn't actually copy the objects stored in the table. An easy way would be to serialize it to a MemoryStream and deserialize to get the new object. Without an instance manager like .NET Remoting uses, the deserialized objects shouldn't be hooked-up to the ones that they were before. The only stipulation is that the keys and values are serializable, which means they must be attributed with SerializableAttribute, even if they implement ISerializable. If you wanted to use your code above, make sure that you check each key/value for an impementation of ICloneable and call Clone when possible.

      Microsoft MVP, Visual C# My Articles

      R 1 Reply Last reply
      0
      • H Heath Stewart

        First of all, you don't need to ref a Hashtable, it's already a reference Type! Second, your method above isn't a deep copy either because it doesn't actually copy the objects stored in the table. An easy way would be to serialize it to a MemoryStream and deserialize to get the new object. Without an instance manager like .NET Remoting uses, the deserialized objects shouldn't be hooked-up to the ones that they were before. The only stipulation is that the keys and values are serializable, which means they must be attributed with SerializableAttribute, even if they implement ISerializable. If you wanted to use your code above, make sure that you check each key/value for an impementation of ICloneable and call Clone when possible.

        Microsoft MVP, Visual C# My Articles

        R Offline
        R Offline
        RickardB
        wrote on last edited by
        #3

        Thanks! My new implementation: protected void CopyHash(ref Hashtable src, ref Hashtable dst) { MemoryStream ms= new MemoryStream(); BinaryFormatter bf = new BinaryFormatter(); //serialize source to a stream bf.Serialize(ms,src); //deserialize it to the copy ms.Seek(0,0); dst = (Hashtable)bf.Deserialize(ms); } A comment: I had to ref the destination Hashtable otherwise the result of the function was an empty Hashtable. Rickard

        H 1 Reply Last reply
        0
        • R RickardB

          Thanks! My new implementation: protected void CopyHash(ref Hashtable src, ref Hashtable dst) { MemoryStream ms= new MemoryStream(); BinaryFormatter bf = new BinaryFormatter(); //serialize source to a stream bf.Serialize(ms,src); //deserialize it to the copy ms.Seek(0,0); dst = (Hashtable)bf.Deserialize(ms); } A comment: I had to ref the destination Hashtable otherwise the result of the function was an empty Hashtable. Rickard

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          You don't need the ref on the input Hashtable, though, which is what I was trying to get at. And actually, since the deserialization process creates a new Hashtable, you'd be better off using out for the second parameter, that way you don't have to initialize an extraneous instance of a Hashtable that's going to be a new instance after deserialization anyway. So your method signature would look like this:

          protected void CopyHash(Hashtable src, out Hashtable dst);

          Microsoft MVP, Visual C# My Articles

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups