Shallow and deep copies in C#

public class Student
{
    public int age;

    public Student(int age)
    {
        this.age = age;
    }
}

public class Grade
{
    public int gradeIndex;
    public Student stu;

    public Grade(Grade grade)
    {
        gradeIndex = grade.gradeIndex;

    }

    // Shallow copy 
    public Grade WiseCopy()
    {
        return (Grade)this.MemberwiseClone();
    }

    // Deep table copy 
    public Grade DeepCopy()
    {
        Grade grade = (Grade)this.MemberwiseClone();
        grade.stu = new Student(this.stu.age);
        return grade;
    }
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325267007&siteId=291194637