1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace cshapExTwo4 { class Program { static void Main(string[] args) { { // ------ ex1) 얕은복사 TestClass1 tc1 = new TestClass1(); tc1.n1 = 10; tc1.n2 = 20; TestClass1 tc2 = tc1; tc2.n2 = 2000; // tc1은 스택에 저장 tc1.n1과 tc.n2는 힙에 저장 Console.WriteLine("얕은복사"); Console.WriteLine($"{tc1.n1},{tc1.n2}"); Console.WriteLine($"{tc2.n1},{tc2.n2}"); } { // ------ ex2) 깊은복사 TestClass1 tc1 = new TestClass1(); tc1.n1 = 10; tc1.n2 = 20; TestClass1 tc2 = tc1.DeepCopy(); tc2.n2 = 2000; // tc1은 스택에 저장 tc1.n1과 tc.n2는 힙에 저장 Console.WriteLine("깊은복사"); Console.WriteLine($"{tc1.n1},{tc1.n2}"); Console.WriteLine($"{tc2.n1},{tc2.n2}"); } Console.ReadKey(); } } class TestClass1 { public TestClass1 DeepCopy() { TestClass1 tc = new TestClass1(); tc.n1 = this.n1; tc.n2 = this.n2; return tc; } public int n1; public int n2; } } | cs |
----------------------------------결과창----------------------------------
얕은복사
10,2000
10,2000
깊은복사
10,20
10,2000
----------------------------------설명----------------------------------
'프로그래밍' 카테고리의 다른 글
2_5. 접근 제한자/한정자 (0) | 2019.01.18 |
---|---|
2_4.this() (0) | 2019.01.18 |
2_2. static필드 (0) | 2019.01.16 |
2_1. 생성자와 종료자 (0) | 2019.01.16 |
2. 태크(h, p, a, i, sup, ins, del) (0) | 2019.01.15 |
댓글