Clone a list of cloneable objects : ICloneable « Class « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » Class » ICloneable 
7.51.2.Clone a list of cloneable objects
using System;
using System.Text;
using System.Collections.Generic;

public class Employee : ICloneable
{
    public string Name;
    public string Title;
    public int Age;

    public Employee(string name, string title, int age)
    {
        Name = name;
        Title = title;
        Age = age;
    }

    public object Clone()
    {
        return MemberwiseClone();
    }

    public override string ToString()
    {
        return string.Format("{0} ({1}) - Age {2}", Name, Title, Age);
    }
}

public class EmployeeList : ICloneable
{
    public List<Employee> EmployeeListMembers =  new List<Employee>();

    public EmployeeList()
    {
    }

    private EmployeeList(List<Employee> members)
    {
        foreach (Employee e in members)
        {
            EmployeeListMembers.Add((Employee)e.Clone());
        }
    }

    public void AddMember(Employee member)
    {
        EmployeeListMembers.Add(member);
    }

    public override string ToString()
    {
        StringBuilder str = new StringBuilder();

        foreach (Employee e in EmployeeListMembers)
        {
            str.AppendFormat("  {0}\r\n", e);
        }

        return str.ToString();
    }

    public object Clone()
    {
        return new EmployeeList(this.EmployeeListMembers);
    }
}

public class MainClass
{
    public static void Main()
    {
        EmployeeList team = new EmployeeList();
        team.AddMember(new Employee("A""AA"4));
        team.AddMember(new Employee("B""BB"8));
        team.AddMember(new Employee("C""CC"8));

        EmployeeList clone = (EmployeeList)team.Clone();

        Console.WriteLine("Original EmployeeList:");
        Console.WriteLine(team);

        Console.WriteLine("Clone EmployeeList:");
        Console.WriteLine(clone);

        Console.WriteLine("*** Make a change to original team ***");
        team.EmployeeListMembers[0].Name = "L";
        team.EmployeeListMembers[0].Title = "M";
        team.EmployeeListMembers[0].Age = 4;

        Console.WriteLine("Original EmployeeList:");
        Console.WriteLine(team);

        Console.WriteLine("Clone EmployeeList:");
        Console.WriteLine(clone);
    }
}
Original EmployeeList:
  A (AA) - Age 4
  B (BB) - Age 8
  C (CC) - Age 8

Clone EmployeeList:
  A (AA) - Age 4
  B (BB) - Age 8
  C (CC) - Age 8

*** Make a change to original team ***
Original EmployeeList:
  L (M) - Age 4
  B (BB) - Age 8
  C (CC) - Age 8

Clone EmployeeList:
  A (AA) - Age 4
  B (BB) - Age 8
  C (CC) - Age 8
7.51.ICloneable
7.51.1.Implement ICloneable interface
7.51.2.Clone a list of cloneable objects
7.51.3.Cloneable Point
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.