Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi...

my name is mina i want to ask when i create a copy constractor do it create a deep copy or a shallow copy of the data as in this code :

public class person
{
    public string name, location;



    public person()
    {

    }



    public person(string name)
    {
        this.name = name;
    }

    public person(string location ,string name)
    {
        this.location = location;
        this.name = name;
    }

    public person(person args)
    {
        this.name = args.name;
    }
}


What I have tried:

i just used the break point at this code but this thing is still missy for me
Posted
Updated 4-Aug-19 21:19pm

1 solution

That code creates a shallow copy - but it doesn't even create a full shallow copy because the location field is not copied at all!

The problem is that a shallow copy duplicates value types but copies references to reference types, and a deep copy duplicates both; so with a shallow copy if you modify part of the reference type properties in the copy it affects both copy and original, but with a deep copy it doesn't - but a string is a bad selection to use here because although it is a reference type it's a special case as it is immutable: you cannot change it onces created.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900