250x250
Rainbow๐ŸŒˆCoder
My dev Note๐Ÿ“’
Rainbow๐ŸŒˆCoder
์ „์ฒด ๋ฐฉ๋ฌธ์ž
์˜ค๋Š˜
์–ด์ œ
  • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (411)
    • ๊ณต์ง€์‚ฌํ•ญ (0)
    • Debugger (10)
      • Visual Studio Debugger (1)
      • Chrome DevTools (3)
      • Visual Studio Code Debugger (4)
      • eclipse (1)
      • intelliJ (1)
    • OOP (2)
      • OOP (2)
    • TypeScript (54)
      • ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ TypeScript (54)
    • Javascript (87)
      • Javascript (45)
      • Node.js (19)
      • React (5)
      • FE ๊ฐœ๋ฐœํ™˜๊ฒฝ์„ค์ • (3)
      • React์™€ Node ๊ฐ™์ด ๋•Œ๋ ค์žก๊ธฐ (6)
      • next.js (2)
      • pixi.js (7)
    • ๋งˆํฌ์—… (23)
      • Html & Css (23)
    • C# (80)
      • C# (12)
      • ์ด๊ฒƒ์ด C#์ด๋‹ค (68)
    • C++ (30)
      • c++ (27)
      • win api (3)
    • Unity (18)
      • Unity(๊ธฐ์ดˆ) (8)
      • Unity(C#์ค‘๊ธ‰) (5)
      • ์œ ๋‹ˆํ‹ฐ ํฌํ†ค(๋„คํŠธ์›Œํฌ) (4)
      • unity c# MyCode (1)
    • Java & Spring (29)
      • Java (11)
      • ์Šคํ”„๋ง (8)
      • Java Algorithm (9)
      • Javs Data Structures (1)
    • ์ž๋ฃŒ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ (15)
      • ์ž๋ฃŒ๊ตฌ์กฐ (5)
      • ์•Œ๊ณ ๋ฆฌ์ฆ˜ (10)
    • ํ˜•์ƒ๊ด€๋ฆฌ (15)
      • Git (11)
      • ์†Œ์ŠคํŠธ๋ฆฌ (3)
    • ๊ทธ๋ž˜ํ”ฝ์Šค (7)
      • WebGl (7)
    • AWS (3)
      • aws (3)
    • ๋ฆฌ๋ˆ…์Šค (5)
      • ๋ฆฌ๋ˆ…์Šค (5)
    • ์ฑ… ๋ฆฌ๋ทฐ (13)
      • ํด๋ฆฐ์ฝ”๋“œ(์ฑ…๋ฆฌ๋ทฐ) (3)
      • ์œ ์ง€๋ณด์ˆ˜๊ฐ€๋Šฅํ•œ์ฝ”๋”ฉ์˜๊ธฐ์ˆ C#ํŽธ(์ฑ…๋ฆฌ๋ทฐ) (1)
      • ๋ฆฌํŒฉํ† ๋ง(์ž๋ฐ”์Šคํฌ๋ฆฝํŠธํŒ) (9)
    • Server (2)
      • ๊ฒŒ์ž„ ์„œ๋ฒ„(๋„คํŠธ์›Œํฌ, ๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋“œ,OS) (2)
    • ์„ค๊ณ„, ์•„ํ‚คํ…์ณ (4)
    • ํŒŒ์ด์ฌ (5)
    • ๋””์ž์ธํŒจํ„ด (2)
    • mocha (2)
    • Jest (1)
    • Spine (1)
    • ์ธ๊ณต์ง€๋Šฅ (1)
      • ํ˜ผ์ž๊ณต๋ถ€ํ•˜๋Š”๋จธ์‹ ๋Ÿฌ๋‹+๋”ฅ๋Ÿฌ๋‹ (1)

๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

  • ํ™ˆ
  • ํƒœ๊ทธ
  • ๋ฐฉ๋ช…๋ก

๊ณต์ง€์‚ฌํ•ญ

์ธ๊ธฐ ๊ธ€

ํƒœ๊ทธ

  • ์ปดํฌ์ง€์…˜
  • ใ…ฃใ„ท
  • MySQL
  • ์œ„์ž„

์ตœ๊ทผ ๋Œ“๊ธ€

์ตœ๊ทผ ๊ธ€

ํ‹ฐ์Šคํ† ๋ฆฌ

hELLO ยท Designed By ์ •์ƒ์šฐ.
Rainbow๐ŸŒˆCoder

My dev Note๐Ÿ“’

C#/์ด๊ฒƒ์ด C#์ด๋‹ค

20. ์–•์€ ๋ณต์‚ฌ(Shallow Copy)์™€ ๊นŠ์€ ๋ณต์‚ฌ(Deep Copy)

2022. 2. 10. 14:34
728x90

ํด๋ž˜์Šค๋Š” ํƒœ์ƒ์ด ์ฐธ์กฐ ํ˜•์‹์ด๋‹ค.

์ฐธ์กฐ ํ˜•์‹์€ ํž™ ์˜์—ญ์— ๊ฐ์ฒด๋ฅผ ํ• ๋‹นํ•˜๊ณ ,

์Šคํƒ์— ์žˆ๋Š” ์ฐธ์กฐ๊ฐ€ ํž™ ์˜์—ญ์— ํ• ๋‹น๋œ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.

 

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;
        }
    }
728x90

'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
    'C#/์ด๊ฒƒ์ด C#์ด๋‹ค' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
    • 22. ์€๋‹‰์„ฑ, ์ ‘๊ทผ ํ•œ์ •์ž
    • 21. this, this() ์ƒ์„ฑ์ž
    • 19. [static]์ •์  ํ•„๋“œ์™€ ์ •์  ๋ฉ”์†Œ๋“œ
    • 18. ๊ฐ์ฒด์˜ ์‚ถ๊ณผ ์ฃฝ์Œ(์ƒ์„ฑ์ž,์ข…๋ฃŒ์ž) + ์ƒ์„ฑ์ž ์˜ค๋ฒ„๋กœ๋”ฉ
    Rainbow๐ŸŒˆCoder
    Rainbow๐ŸŒˆCoder
    ๋ชฐ๋ผ๋„ ๊ฒฐ๊ตญ์€ ์•„๋Š” ๊ฐœ๋ฐœ์ž, ๊ทธ๋Ÿฐ ์‚ฌ๋žŒ์ด ๋˜๊ธฐ ์œ„ํ•œ ๋งค์ผ์˜ ํ•œ๊ฑธ์Œ

    ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”