๊น์ ๋ณต์ฌ/์์ ๋ณต์ฌ
์์ ๋ณต์ฌ(Shallow Copy): ๋์ผํ ๋ฉ๋ชจ๋ฆฌ ์ฃผ์๋ฅผ ๊ณ ์ ํจ -> ๊ฐ์ด ๋ณํจ
using System;
class Program
{
static void Main()
{
int[] originalArray = { 1, 2, 3 };
int[] shallowCopyArray = (int[])originalArray.Clone();
shallowCopyArray[0] = 99;
Console.WriteLine("Original Array: " + string.Join(", ", originalArray)); // ์ถ๋ ฅ: Original Array: 1, 2, 3
Console.WriteLine("Shallow Copy Array: " + string.Join(", ", shallowCopyArray)); // ์ถ๋ ฅ: Shallow Copy Array: 99, 2, 3
}
}
๊น์ ๋ณต์ฌ(Deep Copy): ์๋ก์ด ๋ฉ๋ชจ๋ฆฌ ์ฃผ์์ ๋ณต์ฌ๋์ด์ ์๋ณธ ๊ฐ์ฒด์๋ ์์ ํ ๋ถ๋ฆฌ
using System;
using System.Linq;
class Program
{
static void Main()
{
int[] originalArray = { 1, 2, 3 };
int[] deepCopyArray = originalArray.Select(x => x).ToArray();
deepCopyArray[0] = 99;
Console.WriteLine("Original Array: " + string.Join(", ", originalArray)); // ์ถ๋ ฅ: Original Array: 1, 2, 3
Console.WriteLine("Deep Copy Array: " + string.Join(", ", deepCopyArray)); // ์ถ๋ ฅ: Deep Copy Array: 99, 2, 3
}
}
์ฝ๊ธฐ ์ ์ฉ: readonly
์์ฑ์๋ก ์ด๊ธฐํ๋ ํ์๋ ๊ฐ์ ๋ณ๊ฒฝํ ์ ์๋ ์ฝ๊ธฐ ์ ์ฉ ๋ณ์์ด๋ค. (๋ณ๊ฒฝํ๋ ค๊ณ ํ๋ฉด ์๋ฌ๊ฐ ๋ฌ๋ค)
class MyClass
{
public readonly int MyReadOnlyField;
public MyClass(int value)
{
MyReadOnlyField = value;
}
}
class Program
{
static void Main()
{
MyClass instance = new MyClass(10);
Console.WriteLine(instance.MyReadOnlyField); // 10 ์ถ๋ ฅ
// ์๋ ์ฝ๋๋ ์ค๋ฅ๋ฅผ ๋ฐ์์ํต๋๋ค.
// instance.MyReadOnlyField = 20;
}
}
readonly์ const์ ๊ณตํต์ ์ ๋ ๋ค ๋ณ์ ๊ฐ์ ๋ณ๊ฒฝํ์ง ๋ชปํ๋๋ก ๋ง๋ ๋ค๋ ๊ฒ์ด๊ณ ,
์ฐจ์ด์ ์ readonly๋ ์์ฑ์ ๋ด์์ ์ด๊ธฐํํ ์ ์๊ธฐ ๋๋ฌธ์ ํด๋์ค์ ์ธ์คํด์ค๋ง๋ค ๋ค๋ฅธ ๊ฐ์ ๊ฐ์ง ์ ์๋ค๋ ๊ฒ์ด๋ค.
๊ตฌ์กฐ์ฒด (struct)
struct:: ๊ฐ ํ์
(Value Type) => ๊น์ ๋ณต์ฌ
class: ์ฐธ์กฐ ํ์
(Reference Type) => ์์ ๋ณต์ฌ
using System;
struct MyStruct
{
public int Value;
}
class MyClass
{
public int Value;
}
class Program
{
static void ModifyStruct(MyStruct s)
{
s.Value = 100;
}
static void ModifyClass(MyClass c)
{
c.Value = 100;
}
static void Main()
{
MyStruct structInstance = new MyStruct();
structInstance.Value = 10; //10์ผ๋ก ์ค์ ํ๊ณ
ModifyStruct(structInstance); //๊น์๋ณต์ฌํด์ 100
Console.WriteLine($"Struct Value: {structInstance.Value}"); // ์ถ๋ ฅ: 10 (๊ฐ์ด ๋ณ๊ฒฝ๋์ง ์์)
MyClass classInstance = new MyClass();
classInstance.Value = 10; //10์ผ๋ก ์ค์ ํ๊ณ
ModifyClass(classInstance); //์์๋ณต์ฌํด์ 100
Console.WriteLine($"Class Value: {classInstance.Value}"); // ์ถ๋ ฅ: 100 (๊ฐ์ด ๋ณ๊ฒฝ๋จ)
}
}
struct:: readonly ์ ์ธํด์ ๋ณ๊ฒฝ ๋ถ๊ฐ๋ฅํ๊ฒ ๊ฐ๋ฅ (๋ด๋ถ์๋ ๋ชจ๋ readonly๋ก ์ ์ธํด์ค์ผํจ)
class: ๋ณ๊ฒฝ ๋ถ๊ฐ๋ฅ์ผ๋ก ์ ์ธ ์๋จ, ํด๋์ค์ ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ๊ฑฐ๋ ๋ค์ค ์์ํ ์ ์์
ํํ
C# 7.0๋ถํฐ ๋์ ๋ ๋ฐ์ดํฐ ๊ตฌ์กฐ๋ก, ๊ด๋ จ๋ ๋ฐ์ดํฐ๋ฅผ ํ๋์ ๋ฌถ์์ผ๋ก ๋ง๋ค์ด์ค๋ค.
using System;
class Program
{
static (string, int) GetPersonInfo()
{
string name = "John";
int age = 30;
return (name, age); // ํํ ๋ฐํ
}
static void Main()
{
var person = GetPersonInfo();
Console.WriteLine($"Name: {person.Item1}, Age: {person.Item2}");
}
}
'๐ฅ๏ธ > C#' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[C#] ์ธํฐํ์ด์ค์ ์ถ์ํด๋์ค (0) | 2023.08.18 |
---|---|
[์ด๊ฒ์ด C#์ด๋ค] Chapter07: ์ฐ์ต๋ฌธ์ (0) | 2023.08.17 |
[C#] ๋ถํ ํด๋์ค, ํ์ฅ ๋ฉ์๋ (0) | 2023.08.17 |
[C#] ์์, ๋คํ์ฑ, ์ค๋ฒ๋ผ์ด๋ฉ (0) | 2023.08.17 |
[์ด๊ฒ์ด C#์ด๋ค] Chapter06: ์ฐ์ต๋ฌธ์ (0) | 2023.08.16 |