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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:41:21+00:00 2026-05-14T01:41:21+00:00

I want to copy values from one object to another object. Something similar to

  • 0

I want to copy values from one object to another object. Something similar to pass by value but with assignment.

For example:

PushPin newValPushPin = oldPushPin; //I want to break the reference here.

I was told to write a copy constructor for this. But this class has a lot of properties, it will probably take an hour to write a copy constructor by hand.

  1. Is there a better way to assign an object to another object by value?
  2. If not, is there a copy constructor generator?

Note: ICloneable is not available in Silverlight.

  • 1 1 Answer
  • 1 View
  • 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-14T01:41:22+00:00Added an answer on May 14, 2026 at 1:41 am

    If you can mark the object that is to be cloned as Serializable then you can use in-memory serialization to create a copy. Check the following code, it has the advantage that it will work on other kinds of objects as well and that you don’t have to change your copy constructor or copy code each time an property is added, removed or changed:

        class Program
        {
            static void Main(string[] args)
            {
                var foo = new Foo(10, "test", new Bar("Detail 1"), new Bar("Detail 2"));
    
                var clonedFoo = foo.Clone();
    
                Console.WriteLine("Id {0} Bar count {1}", clonedFoo.Id, clonedFoo.Bars.Count());
            }
        }
    
        public static class ClonerExtensions
        {
            public static TObject Clone<TObject>(this TObject toClone)
            {
                var formatter = new BinaryFormatter();
    
                using (var memoryStream = new MemoryStream())
                {
                    formatter.Serialize(memoryStream, toClone);
    
                    memoryStream.Position = 0;
    
                    return (TObject) formatter.Deserialize(memoryStream);
                }
            }
        }
    
        [Serializable]
        public class Foo
        {
            public int Id { get; private set; }
    
            public string Name { get; private set; }
    
            public IEnumerable<Bar> Bars { get; private set; }
    
            public Foo(int id, string name, params Bar[] bars)
            {
                Id = id;
                Name = name;
                Bars = bars;
            }
        }
    
        [Serializable]
        public class Bar
        {
            public string Detail { get; private set; }
    
            public Bar(string detail)
            {
                Detail = detail;
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.