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 7537903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:58:28+00:00 2026-05-30T06:58:28+00:00

I have a collection of objects and I am trying to clone this collection

  • 0

I have a collection of objects and I am trying to clone this collection and trying to understand performance implication of different approaches.

The object in the collection has about 20 properties all strings, ints, floats (this objects doesn’t have any nested objects inside of it). The two approaches are:

  1. Create DeepClone() method:

    public static class ExtensionMethods
    {
        public static T DeepClone<T>(this T a)
        {
           using (var stream = new MemoryStream())
           {
               var formatter = new BinaryFormatter();
               formatter.Serialize(stream, a);
               stream.Position = 0;
              return (T)formatter.Deserialize(stream);
           }
       }
    

    }

  2. Manually write “copy” code where i am looping through the collection and “new”ing a new object and then manually setting all of the 20 properties. something like this

     public MyObject Copy(MyObject myObj)
    {
     var obj = new MyObject();
     obj.Prop1 = myObj.Prop1;
     obj.Prop2 = myObj.Prop2;
     return obj;
    

    }

I am getting very inconsistent results so I wanted to get peoples feedback on:

  1. Should one be much faster that the other? I would have thought choice two but my tests don’t seem to support this so I am trying to figure out if I am doing something wrong.

  2. Is there any way to do this even faster?

  • 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-05-30T06:58:29+00:00Added an answer on May 30, 2026 at 6:58 am

    Well, first of all BinaryFormatter route must definitely be slower, since it uses reflection to get/set properties. The most common method is using the IClonable interface in conjunction with a copy constructor.

    class A : ICloneable
    {
        private readonly int _member;
    
        public A(int member)
        {
            _member = member;
        }
    
        public A(A a)
        {
            _member = a._member;
        }
    
        public object Clone()
        {
            return new A(this);
        }
    }
    

    Of course strictly speaking you only need the copy constructor, which should be the fastest method. If your objects are simple, you should try using in-built MemberwiseClone function.

    class A : ICloneable
    {
        private readonly int _member;
    
        public A(int member)
        {
            _member = member;
        }
    
        public object Clone()
        {
            return MemberwiseClone();
        }
    }
    

    Meanwhile, I wrote some test code to see if MemberwiseClone() was severely faster or slower than using a copy constructor. You can find it here. I found that MemberwiseClone is actually much slower than doing a CopyConstructor, at least on small classes. Note that using the BinaryFormatter is insanely slow.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

C#: I have a collection of objects . T has 2 properties. Property A
I have two Collection objects, I want to associate each object of these two
In my application, I have three collection objects which store data. The data which
I have a collection of objects which describe an image-name, its size and it's
I have a collection of objects of the same type, let's call it DataItem
If I have a collection of objects: public class Party { LinkedList<Guy> partyList =
I'm looking to have a collection of objects that implement a certain interface, but
Let's say I have a collection of objects which can be sorted using a
I have an observable collection of objects that I'd like to display on the
I have a list/collection of objects that may or may not have the same

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.