C# What is the best way to copy a BindingList?
Asked Answered
S

4

3

What is the best way to copy a BindingList?

Just use ForEach()? Or are there better ways?

Saltire answered 3/5, 2010 at 10:10 Comment(8)
Copy how? Into another list? Same instances of the underlying objects, or cloned new instances?Factory
Serialize the object then de-serialize to get a deep cloned non referenced copy.Kooky
I want to copy all instances to the other listSaltire
Martijn, do you want a deep or shallow copy?Vanish
I'm sorry, I am not familiar with those terms (deep and shallow)Saltire
Shallow: new list holds references to the old objects. Deep: New list gets copies of the instances.Vanish
Ah, thanks. I need a deep copySaltire
Then Ismail's answer comes closest. But search around first, deep-cloning isn't as easy as it looks.Vanish
B
4

BindingList has a constructor which can take an IList. And BindingList implements IList. So you can just do the following:

BindingList newBL = new BindingList(oldBL);

Of course that creates a second list that just points at the same objects. If you actually want to clone the objects in the list then you have to do more work.

Brandnew answered 3/5, 2010 at 10:25 Comment(0)
P
2

Foreach pretty much is the easiest way, and the performance overhead is minimal if any.

Pavior answered 3/5, 2010 at 10:12 Comment(3)
And what about this? BindingList list2 = new BindingList(list1.toList())Saltire
If you decompile toList you will see it does pretty much the same ;)Pavior
This really worked for me, so I'm voting up for this solutionDormeuse
V
2

From a deleted answer:

Serialize the object then de-serialize to get a deep cloned non referenced copy

Which is a valid option if the OP wants a deep copy.

Vanish answered 3/5, 2010 at 10:27 Comment(2)
@Ismail S, I think you should undelete.Vanish
Thanks for coming to my rescue :). These people are using bad words against me.Kooky
E
2

We use the Serialize / De-serialize route to get a deep copy of the list. It works well but it does slow performance down in larger lists, such as for search screens, so I'd avoid using it on lists with 5000+ items.

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace ProjectName.LibraryName.Namespace
{
    internal static class ObjectCloner
    {
        /// 
        /// Clones an object by using the .
        /// 
        /// The object to clone.
        /// 
        /// The object to be cloned must be serializable.
        /// 
        public static object Clone(object obj)
        {
            using (MemoryStream buffer = new MemoryStream())
            {
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(buffer, obj);
                buffer.Position = 0;
                object temp = formatter.Deserialize(buffer);
                return temp;
            }
        }
    }
}

Epizoon answered 12/2, 2011 at 13:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.