CSharp examples for Custom Type:interface
Implement a Cloneable Type
using System;//w ww.j a v a2s . c o m 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 Team : ICloneable { public List<Employee> TeamMembers = new List<Employee>(); public Team() { } private Team(List<Employee> members) { foreach (Employee e in members) { TeamMembers.Add((Employee)e.Clone()); } } public void AddMember(Employee member) { TeamMembers.Add(member); } public override string ToString() { StringBuilder str = new StringBuilder(); foreach (Employee e in TeamMembers) { str.AppendFormat(" {0}\r\n", e); } return str.ToString(); } // Implementation of ICloneable.Clone. public object Clone() { return new Team(this.TeamMembers); // The following command would create a shallow copy of the Team. // return MemberwiseClone(); } } class MainClass { public static void Main() { Team team = new Team(); team.AddMember(new Employee("F", "Developer", 34)); team.AddMember(new Employee("K", "Tester", 78)); team.AddMember(new Employee("C", "Support", 18)); Team clone = (Team)team.Clone(); Console.WriteLine("Original Team:"); Console.WriteLine(team); Console.WriteLine("Clone Team:"); Console.WriteLine(clone); team.TeamMembers[0].Name = "Luke"; team.TeamMembers[0].Title = "Manager"; team.TeamMembers[0].Age = 44; Console.WriteLine("Original Team:"); Console.WriteLine(team); Console.WriteLine("Clone Team:"); Console.WriteLine(clone); } }