Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
School/C# - Beginner (Denis)/Lesson 57/lesson_57.cs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
103 lines (89 sloc)
1.97 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Person p1 = new Person { Name = "Tom", Age = 23 }; | |
Person p2 = p1; | |
p2.Name = "Alice"; | |
Console.WriteLine(p1.Name); // Alice | |
Console.Read(); | |
} | |
} | |
class Person | |
{ | |
public string Name { get; set; } | |
public int Age { get; set; } | |
} | |
public interface ICloneable | |
{ | |
object Clone(); | |
} | |
class Person : ICloneable | |
{ | |
public string Name { get; set; } | |
public int Age { get; set; } | |
public object Clone() | |
{ | |
return new Person { Name = this.Name, Age = this.Age }; | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Person p1 = new Person { Name = "Tom", Age = 23 }; | |
Person p2 = (Person)p1.Clone(); | |
p2.Name = "Alice"; | |
Console.WriteLine(p1.Name); // Tom | |
Console.Read(); | |
} | |
} | |
class Person : ICloneable | |
{ | |
public string Name { get; set; } | |
public int Age { get; set; } | |
public object Clone() | |
{ | |
return this.MemberwiseClone(); | |
} | |
} | |
class Person : ICloneable | |
{ | |
public string Name { get; set; } | |
public int Age { get; set; } | |
public Company Work { get; set; } | |
public object Clone() | |
{ | |
return this.MemberwiseClone(); | |
} | |
} | |
class Company | |
{ | |
public string Name { get; set; } | |
} | |
Person p1 = new Person { Name = "Tom", Age = 23, Work = new Company { Name = "Microsoft" } }; | |
Person p2 = (Person)p1.Clone(); | |
p2.Work.Name = "Google"; | |
p2.Name = "Alice"; | |
Console.WriteLine(p1.Name); // Tom | |
Console.WriteLine(p1.Work.Name); // Google - а должно быть Microsoft | |
class Person : ICloneable | |
{ | |
public string Name { get; set; } | |
public int Age { get; set; } | |
public Company Work { get; set; } | |
public object Clone() | |
{ | |
Company company = new Company { Name = this.Work.Name }; | |
return new Person | |
{ | |
Name = this.Name, | |
Age = this.Age, | |
Work = company | |
}; | |
} | |
} | |
class Company | |
{ | |
public string Name { get; set; } | |
} |