2

I am a complete noob, so let me know if I am out of place for asking this, and if needed give me the boot. Anyways... I have been learning about value types and reference types and am wondering if defining an additional constructor such as this (and then calling it later) is a valid way to create a copy of a class. I tried testing it with a simple class and it seemed to work.

public ClassClass(ClassClass exampleClass)
    {
        var1 = exampleClass.var1;
        var2 = exampleClass.var2;
        // ...
    }

...

Rufus L
  • 36,127
  • 5
  • 30
  • 43
  • https://stackoverflow.com/questions/1031023/copy-a-class-c-sharp – Jake Sep 25 '18 at 04:37
  • It seems fine, but you've only posted partial code. You should provide us with a [mcve] so that we can properly comment on your proposition. – Enigmativity Sep 25 '18 at 04:41
  • thanks for the comments! that helped out Jake, i guess i missed that in my search before asking the question. –  Sep 25 '18 at 04:44
  • i will keep that in mind Enigmativity! –  Sep 25 '18 at 04:45
  • 1
    This for sure is a duplicate of about 100 well answered questions – TheGeneral Sep 25 '18 at 05:04
  • Note that while one usually makes a difference between a _shallow vs. deep copy_, there actually is no such thing a 'deep copy'..! Instead it is up to you to __decide on the depth__ of your copy, ie how many levels of references you copy process needs to follow! – TaW Sep 25 '18 at 08:22

1 Answers1

9

There are two kinds of copying objects.

  1. reference copy
  2. value copy

to copy an object by reference you can just use '=' operator.

e.g:

var o1 = new ClassClass();
var o2 = o1;

to copying an object by value, there are several ways, such as:

Copy by a constructor (as you wrote)

    public class Student
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }

        public Student(Student std)
        {
            FirstName = std.FirstName;
            LastName = std.LastName;
        }
    }

Make a helper class an pass the s1 as input and return s2 as result

    static void Main(string[] args)
    {
        var s1 = new Student();
        var s2 = ClassHelper.CopyObject(s1);

    }

    public static class ClassHelper
    {
        public static Student CopyObject(Student std)
        {
            var newStudent = new Student()
            {
                FirstName = std.FirstName,
                LastName = std.LastName
            };
            return newStudent;
        }
    }

Generic Copy Objects (using Refelection)

private static void CopyClass<T>(T copyFrom, T copyTo, bool copyChildren)
{
    if (copyFrom == null || copyTo == null)
        throw new Exception("Must not specify null parameters");

    var properties = copyFrom.GetType().GetProperties();

    foreach (var p in properties.Where(prop => prop.CanRead && prop.CanWrite))
    {
        if (p.PropertyType.IsClass && p.PropertyType != typeof(string))
        {
            if (!copyChildren) continue;

            var destinationClass = Activator.CreateInstance(p.PropertyType);
            object copyValue = p.GetValue(copyFrom);

            CopyClass(copyValue, destinationClass, copyChildren);

            p.SetValue(copyTo, destinationClass);                  
        }
        else
        {
            object copyValue = p.GetValue(copyFrom);
            p.SetValue(copyTo, copyValue);
        }
    }
}

Write an extension method for this class and I recommended to do this.

public static class ExtensionClass
{
    public static Student CopyAsNewObject(this Student std)
    {
        var newStudent = new Student()
        {
            FirstName = std.FirstName,
            LastName = std.LastName
        };
        return newStudent;
    }
}

    static void Main(string[] args)
    {
        var s1 = new Student();
        var s2 = s1.CopyAsNewObject();
    }
Siavash
  • 2,813
  • 4
  • 29
  • 42