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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:45:15+00:00 2026-06-14T06:45:15+00:00

MemberwiseClone() will create shallow copy, which will do a bit-by-bit copy of a value

  • 0

MemberwiseClone() will create shallow copy, which will do a bit-by-bit copy of a value type and only copy the references of a reference type. We would need to create a deep copy to have an independent copy of a reference type. This is ok. I get this.
Now my question is: why does a System.Object variable react differently when MemberwiseClone() is called?

Eg:

Public class Test
{
    public int x;
    public object obj;

    public Test MyClone()
    {
         return (Test) this.MemberwiseClone();
    }
}

Now access this in my Main method:

public static void Main()
{
    Test obj1 = new obj1;
     obj1.x = 1;
     obj1.obj = "x";

    Test obj2 = obj1.MyClone();
    obj2.obj = "y" 
}

Should this not change the value of obj1.obj to y? Since obj is an instance of System.Object which is obviously a reference type. But it does not change – obj1.obj gives x and obj2.obj gives y. Why is this?

  • 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-14T06:45:17+00:00Added an answer on June 14, 2026 at 6:45 am

    It’s because you don’t change state of field obj. You assign it.

    public static void Main()
    {
        Test obj1 = new obj1;
         obj1.x = 1;
         obj1.obj = new List<string> {"x"};
    
        Test obj2 = obj1.MyClone();
        ((List<string>) obj2.obj).Add("y"); //now it contains {x, y} in obj1 and obj2
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 2 objects from the same type and i would like to shallow
Possible Duplicate: What is the difference between a deep copy and a shallow copy?
I need to shallow copy a dictionary in c#. For instance: Dictionary<int,int> flags =
I need a semi-shallow copy of an object. Under my original design I used
I have the following class that I would like to do a deep copy
MSDN gives this example of a deep copy ( http://msdn.microsoft.com/en-us/library/system.object.memberwiseclone.aspx ) public class Person
I know to perform a shallow copy in C# we could use MemberwiseClone() function
Strings are considered reference types yet can act like values. When shallow copying something
I am using this.MemberwiseClone() to create shallowcopy but it is not working. Please look
When performing a MemberwiseClone of an array of value types: var arr = new

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.