Create copy and not duplicate of object

I have a dictionary from which I "copy" a object form like this:

public class MyClass
{
    public string objectId;
    public string name;
}

Dictionary<string, MyClass> dictionary = new Dictionary<string, MyClass>();
dictionary.Add("1234", new MyClass() { objectId = "1234", name = "Peter" });
dictionary.Add("2314", new MyClass() { objectId = "2314", name = "Tim" });
dictionary.Add("4321", new MyClass() { objectId = "4321", name = "Viggo" });

MyClass newObject = new MyClass();

newObject = dictionary["1234"];

newObject.name = "Tommy";

Now, what I want to accomplish is that the name only changes in the newObject and not in the dictionary... As it is now, any changes made to the newObject also changes inside the dictionary...

How do I separate them?

Hoping for help and thanks in advance :-)

-------- EDIT ---------

Ok tried to make a function like this:

public static object CopyObject(object obj)
{
    Dictionary<string, object> copy = new Dictionary<string, object>();

    foreach (var field in obj.GetType().GetFields())
    {
        if (!field.IsNotSerialized && field.GetValue(obj) != null)
        {
            // everything in here can be serialized!
            if (field.FieldType == typeof(string))
            {
                copy.Add(field.Name, field.GetValue(obj).ToString());
            }
            else if (field.FieldType == typeof(bool))
            {
                copy.Add(field.Name, bool.Parse(field.GetValue(obj).ToString()));
            }
            else if (field.FieldType == typeof(int))
            {
                copy.Add(field.Name, (int)Convert.ToInt32(field.GetValue(obj)));
            }
        }
    }

    return copy;
}

And then tried to create the new MyClass object like this:

newObject = CopyObject(dictionary["1234"]) as MyClass;

It return empty... What am I doing wrong here?


You should consider using record instead of the class.

Then you could use Nondestructive mutation to copy the record to a new one with required changes like that

var newObject = dictionary["1234"] with { name = "Tommy" };

You could read more about it here


Class is reference type. When you try to copy Object value from dictionary like this:

newObject = dictionary["1234"];

you are copying a reference to an object, not the fields values of the object.

When you change the value of the fields of a new object (which stores a reference to the object from the dictionary) - you change the values by reference, i.e. they will change in the original object.

The easiest way to create a new class object is by using the fields values of the object from the dictionary:

MyClass newObject = new MyClass() 
{
    objectId = dictionary["1234"].objectId, 
    name = dictionary["1234"].name
};

or

MyClass newObject = new MyClass();
newObject.objectId = dictionary["1234"].objectId;
newObject.name = dictionary["1234"].name;

In this case, changes to the new object will not affect the "base object from the dictionary"