ํด๋์ค๋ ํ์์ด ์ฐธ์กฐ ํ์์ด๋ค.
์ฐธ์กฐ ํ์์ ํ ์์ญ์ ๊ฐ์ฒด๋ฅผ ํ ๋นํ๊ณ ,
์คํ์ ์๋ ์ฐธ์กฐ๊ฐ ํ ์์ญ์ ํ ๋น๋ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace UsingOut
{
class MyClass
{
public int MyField1;
public int MyField2;
}
class MainApp
{
static void Main(string[] args)
{
MyClass source = new MyClass();
source.MyField1 = 10;
source.MyField2 = 20;
MyClass target = source;
target.MyField2 = 30;
WriteLine($"{source.MyField1} {source.MyField2}"); //10 30
WriteLine($"{target.MyField1} {target.MyField2}"); //10 30
source.MyField1 = 5;
WriteLine($"{source.MyField1} {source.MyField2}"); //5 30
WriteLine($"{target.MyField1} {target.MyField2}"); //5 30
target.MyField1 = 15;
WriteLine($"{source.MyField1} {source.MyField2}"); //15 30
WriteLine($"{target.MyField1} {target.MyField2}"); //15 30
}
}
}
source๋ฅผ ๋ณต์ฌํด์ ๋ฐ์ target์ ํ์ ์๋ ์ค์ฒด ๊ฐ์ฒด๊ฐ ์๋ ์คํ์ ์๋ ์ฐธ์กฐ๋ฅผ ๋ณต์ฌํด์ ๋ฐ๋๋ค.
์ฆ, source์ target์ด ์ฌ์ด ์ข๊ฒ ๊ฐ์ ๊ณณ์ ๋ฐ๋ผ๋ณด๊ฒ ๋๋ค.
target์ MtField2๋ฅผ 30์ผ๋ก ๋ฐ๊ฟจ๋๋ฐ
source์ MyField2๋ 30์ผ๋ก ๋ฐ๋๋ ๋ฌธ์ ๊ฐ ์๊ธด ๊ฒ์ด๋ค.
์ด๋ ๊ฒ ๊ฐ์ฒด๋ฅผ ๋ณต์ฌํ ๋ ์ฐธ์กฐ๋ง ์ด์ง ๋ณต์ฌํ๋ ๊ฒ์ ์์ ๋ณต์ฌ(Shallow Copy)๋ผ๊ณ ํ๋ค.
์ด๊ฒ์ ์ํ์ง ์๋๋ค๋ฉด ๊น์ ๋ณต์ฌ(Deep Copy)๋ฅผ ํด์ผํ๋ค.
target์ด ํ์ ๋ณด๊ด๋์ด ์๋ ๋ด์ฉ์ source๋ก๋ถํฐ ๋ณต์ฌํด์ ๋ฐ์
๋ณ๋์ ํ ๊ณต๊ฐ์ ๊ฐ์ฒด๋ฅผ ๋ณด๊ดํ๊ธฐ๋ฅผ ๋ฐ๋๋ค๋ฉด ๊น์ ๋ณต์ฌ๋ฅผ ํด์ผํ๋ค.
์ํ๊น๊ฒ๋, C#์์๋ ๊น์ ๋ณต์ฌ๋ฅผ ์๋์ผ๋ก ํด์ฃผ๋ ๊ตฌ๋ฌธ์ด ์๊ธฐ ๋๋ฌธ์ ์ฝ๋๋ฅผ ๋ง๋ค์ด์ผ ํ๋ค.
class MyClass
{
public int MyField1;
public int MyField2;
public MyClass DeepCopy() //๊ฐ์ฒด๋ฅผ ํ์ ์๋ก ํ ๋นํด์ ๊ทธ๊ณณ์ ์์ ์ ๋ฉค๋ฒ๋ฅผ ์ผ์ผ์ด ๋ณต์ฌํด ๋ฃ๋๋ค.
{
MyClass newCopy = new MyClass();
newCopy.MyField1 = this.MyField1;
newCopy.MyField2 = this.MyField2;
return newCopy;
}
}
๋ค์์ ์์ ๋ณต์ฌ์ ๊น์ ๋ณต์ฌ๋ฅผ ๋น๊ตํ๋ ์์ ํ๋ก๊ทธ๋จ์ด๋ค.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace UsingOut
{
class MyClass
{
public int MyField1;
public int MyField2;
public MyClass DeepCopy()
{
MyClass newCopy = new MyClass();
newCopy.MyField1 = this.MyField1;
newCopy.MyField2 = this.MyField2;
return newCopy;
}
}
class MainApp
{
static void Main(string[] args)
{
WriteLine("Shallow Copy");
{
MyClass source = new MyClass();
source.MyField1 = 10;
source.MyField2 = 20;
MyClass target = source;
target.MyField2 = 30;
WriteLine($"{source.MyField1} {source.MyField2}"); //10 30
WriteLine($"{target.MyField1} {target.MyField2}"); //10 30
}
WriteLine("Deep Copy");
{
MyClass source = new MyClass();
source.MyField1 = 10;
source.MyField2 = 20;
MyClass target = source.DeepCopy();
target.MyField2 = 30;
WriteLine($"{source.MyField1} {source.MyField2}"); //10 20
WriteLine($"{target.MyField1} {target.MyField2}"); //10 30
}
}
}
}
<์ถ๋ ฅ ๊ฒฐ๊ณผ>
Shallow Copy
10 30
10 30
Deep Copy
10 20
10 30
<ICloneable.Clone() ๋ฉ์๋>
System ๋ค์์คํ์ด์ค์๋ ICloneable์ด๋ผ๋ ์ธํฐํ์ด์ค๊ฐ ์๋ค.
์ธํฐํ์ด์ค๋ ํด๋์ค๊ฐ ๊ตฌํํด์ผ ํ๋ ๋ฉ์๋ ๋ชฉ๋ก์ด๋ผ๊ณ ํ ์ ์๋ค.
'๊น์ ๋ณต์ฌ'๊ธฐ๋ฅ์ ๊ฐ์ง ํด๋์ค๊ฐ .NET์ ๋ค๋ฅธ ์ ํธ๋ฆฌํฐ ํด๋์ค๋ ๋ค๋ฅธ ํ๋ก๊ทธ๋๋จธ๊ฐ ์์ฑํ ์ฝ๋์ ํธํ๋๋๋ก ํ๊ณ ์ถ๋ค๋ฉด ICloneable์ ์์ํ๋ ๊ฒ์ด ์ข๋ค.
ICloneable ์ธํฐํ์ด์ค๋ Clone() ๋ฉ์๋ ํ๋๋ง ๊ฐ๊ณ ์๋ค.
class MyClass : ICloneable
{
public int MyField1;
public int MyField2;
public MyClass Clone() //๊ฐ์ฒด๋ฅผ ํ์ ์๋ก ํ ๋นํด์ ๊ทธ๊ณณ์ ์์ ์ ๋ฉค๋ฒ๋ฅผ ์ผ์ผ์ด ๋ณต์ฌํด ๋ฃ๋๋ค.
{
MyClass newCopy = new MyClass();
newCopy.MyField1 = this.MyField1;
newCopy.MyField2 = this.MyField2;
return newCopy;
}
}
'C# > ์ด๊ฒ์ด C#์ด๋ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
22. ์๋์ฑ, ์ ๊ทผ ํ์ ์ (0) | 2022.02.10 |
---|---|
21. this, this() ์์ฑ์ (0) | 2022.02.10 |
19. [static]์ ์ ํ๋์ ์ ์ ๋ฉ์๋ (0) | 2022.02.10 |
18. ๊ฐ์ฒด์ ์ถ๊ณผ ์ฃฝ์(์์ฑ์,์ข ๋ฃ์) + ์์ฑ์ ์ค๋ฒ๋ก๋ฉ (0) | 2022.02.10 |
17. C#์ ํด๋์ค (0) | 2022.02.10 |