
- ์ฃผ์
1. ๊ฐํ, ์ฐธ์กฐํ
2. ๋ฐ์ฑ, ์ธ๋ฐ์ฑ(๊ฐํ๊ณผ ์ฐธ์กฐํ ์ฌ์ด์ ๋ณํ)
3. ๋๋ค & ๋ธ๋ฆฌ๊ฒ์ดํธ
- ๊ณต๋ถ๋ด์ฉ
1. ๊ฐํ, ์ฐธ์กฐํ (๋ณ์๊ฐ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๋ ๋ฐฉ์์ด ์๋ก ๋ค๋ฆ)
1-1. ๊ฐํ
๋ณ์์ ๊ฐ์ ์ง์ ์ ์ฅ --> (์คํ ๋ฉ๋ชจ๋ฆฌ๋ง ์ฌ์ฉ)
๋ณ์๊ฐ ์ค์ ๋ฐ์ดํฐ๋ฅผ ๋ณด์ , ํ ๋นํ๊ฑฐ๋ ์ ๋ฌ ์ ๊ฐ์ด ๋ณต์ฌ๋จ.
ex) int, float, string, double, bool ๋ฑ
struct MyStruct
{
public int Value;
}
MyStruct struct1 = new MyStruct(); // ๋์ ํ ๋น
struct1.Value = 10;
MyStruct struct2 = struct1; // struct2๋ struct1์ ๊ฐ ๋ณต์ฌ
struct2.Value = 20; //(struck1 ๋ณ์์ ์ํฅ์ ์ฃผ์ง ์์)
Console.WriteLine(struct1.Value); // ์ถ๋ ฅ ๊ฒฐ๊ณผ: 10
1-2. ์ฐธ์กฐํ
๋ณ์์ ๋ฐ์ดํฐ์ ๋ํ ์ฐธ์กฐ(๋ฐ์ดํฐ ๊ฐ์ด ์ ์ฅ๋์ด ์๋ ๊ณณ์ ์ฃผ์)๋ฅผ ์ ์ฅ --> (์คํ, ํ ๋๋ค ์ฌ์ฉ)
ํด๋น ๋ณ์๋ฅผ ํ ๋นํ๊ฑฐ๋ ์ ๋ฌ ์ ์ฐธ์กฐ(๋ฉ๋ชจ๋ฆฌ ์์น์ฃผ์)๊ฐ ๋ณต์ฌ๋จ.
ํด๋์ค์ ๋ณ์๊ฐ์ ๋ณต์ฌํ๋ฉด, ์๋ ๋ณ์๊ฐ๊ณผ ๋ณต์ฌ๋ ๋ณ์๊ฐ์ ํ ๋ฐฐ๋ฅผ ํ ๊ฒ์ด๋ค.
๊ทธ๋์ ์๋ ๋ณ์๊ฐ์ด๋ ๋ณต์ฌ๋ ๋ณ์๊ฐ์ด ๋ฐ๋๋ฉด ๊ฐ์ด ๋ฐ๋๊ฒ ๋๋ค.
ex) ํด๋์ค, ๋ฐฐ์ด, ์ธํฐํ์ด์ค ๋ฑ

class Apple
{
public int Value;
}
//static void Main(string[] args)
Apple apple1 = new Apple();
apple1.Value = 10;
Apple apple2 = apple1; // apple2๋ apple1์ ๋ฉ๋ชจ๋ฆฌ์ฃผ์๋ฅผ ๋ณต์ฌ
apple2.Value = 20; // apple1๊ณผ ๊ฐ์ ๋ฉ๋ชจ๋ฆฌ์ฃผ์๋ฅผ ๊ฐ์ง๊ณ ์๋ apple2์ ๋ณ์๊ฐ์ด ๋ณํ๋ฉด apple1๋ ๋ณํจ
Console.WriteLine(apple1.Value); // ์ถ๋ ฅ ๊ฒฐ๊ณผ: 20

***ํด๋์ค์ ์๋ ๋ณ์๊ฐ๊ณผ ๋ณต์ฌ๋ ๋ณ์๊ฐ์ด ํ ๋ฐฐ๋ฅผ ํ์ง ์๊ณ , ๊ฐ๊ฐ ๋ค๋ฅธ ๋ฐฐ๋ฅผ ํ๋ ค๋ฉด ์ด๋ป๊ฒ ํด์ผํ ๊น?
-->๊น์ ๋ณต์ฌ๋ก ๋ค๋ฅธ ์ฃผ์์ ๋ณ์๊ฐ์ ์ ์ฅํ๋ฉด ๋๋ค.
***๊น์ ๋ณต์ฌ๋?
2024.05.03 - [์ ๋ํฐ] - Day 18 - C# (์์ ๋ณต์ฌ, ๊น์ ๋ณต์ฌ, ICloneable)
2. ๋ฐ์ฑ, ์ธ๋ฐ์ฑ(๊ฐํ๊ณผ ์ฐธ์กฐํ ์ฌ์ด์ ๋ณํ)
-๋ฐ์ฑ: ๊ฐํ --> ์ฐธ์กฐํ์ผ๋ก ๋ณํํ๋ ๊ณผ์ (object๋ฅผ ์ฌ์ฉ)
-์ธ๋ฐ์ฑ: ์ฐธ์กฐํ --> ๊ฐํ์ผ๋ก ๋ณํํ๋ ๊ณผ์ (๊ฐํ์ ๋ณธ๋ ์๋ฃํ์ ์ฌ์ฉ)
//๊ฐํ์ ์ฐธ์กฐํ์ผ๋ก ๋ณํํ๋ ๋ฆฌ์คํธ ์์ฑ
List<object> myList = new List<object>();
int intValue = 10;
myList.Add(intValue); // int๋ฅผ object๋ก ๋ฐ์ฑ(์ฐธ์กฐํ์ผ๋ก ๋ณํ)ํ์ฌ ์ถ๊ฐ
float floatValue = 3.14f;
myList.Add(floatValue); // float๋ฅผ object๋ก ๋ฐ์ฑํ์ฌ ์ถ๊ฐ
int value1 = (int)myList[0]; // object๋ฅผ int๋ก ์ธ๋ฐ์ฑ(๊ฐํ์ผ๋ก ๋ณํ)
float value2 = (float)myList[1]; // object๋ฅผ float๋ก ์ธ๋ฐ์ฑ
3. ๋ธ๋ฆฌ๊ฒ์ดํธ & ๋๋ค
3-1. ๋ธ๋ฆฌ๊ฒ์ดํธ
๋ธ๋ฆฌ๊ฒ์ดํธ๋ ๋ฉ์๋๋ฅผ ์ฐธ์กฐ(๋ฉ์๋๊ฐ ์ ์ฅ๋์ด ์๋ ์์น๋ฅผ ์ ์ฅ)ํ์ฌ,
๋ธ๋ฆฌ๊ฒ์ดํธ๋ฅผ ์ฌ์ฉํ๋ฉด ํด๋น ๋ฉ์๋๋ฅผ ๋งค๊ฐ๋ณ์๋ก ์ ๋ฌํ๊ฑฐ๋ ๋ณ์์ ํ ๋นํ ์ ์๋ค.
3-2. ๋ธ๋ฆฌ๊ฒ์ดํธ ๊ตฌํ(๋ฉ์๋ ๋ฑ๋ก -> ์ฌ์ฉ)
๋ธ๋ฆฌ๊ฒ์ดํธ๊ฐ ์ฐธ์กฐํ ๋ฉ์๋์ ํ์์ ๋ธ๋ฆฌ๊ฒ์ดํธ์ ๊ฐ์์ผ ํ๋ค.
public delegate int ๋ธ๋ฆฌ๊ฒ์ดํธ๋ช (int x, int y)
public int ๋ฉ์๋๋ช (int x, int y)
delegate int Calculate(int x, int y);
static int Add(int x, int y)
{
return x + y;
}
class Program
{
static void Main()
{
// ๋ธ๋ฆฌ๊ฒ์ดํธ ๋ณ์(calc)์ ๋ฉ์๋ ๋ฑ๋ก
Calculate calc = Add;
// ๋ธ๋ฆฌ๊ฒ์ดํธ ์ฌ์ฉ
int result = calc(3, 5);
Console.WriteLine("๊ฒฐ๊ณผ: " + result);
}
}
3-3. event ์ ํจ๊ป ์ฌ์ฉ
event๋ ํ ๋น์ฐ์ฐ์( = )๋ฅผ ์ฌ์ฉํ ์ ์์ผ๋ฉฐ, ํด๋์ค ์ธ๋ถ์์๋ ํธ์ถ์ด ๋ถ๊ฐ๋ฅํ๋ค.
(ํ ์คํธ ๊ฒ์์ ํ์ฉํด๋ณผ ์ ์์ ๊ฒ ๊ฐ๋ค)
// ๋ธ๋ฆฌ๊ฒ์ดํธ ์ ์ธ
public delegate void EnemyAttackHandler(float damage);
// ์ ํด๋์ค
public class Enemy
{
// ๊ณต๊ฒฉ ์ด๋ฒคํธ
public event EnemyAttackHandler OnAttack;
// ์ ์ ๊ณต๊ฒฉ ๋ฉ์๋
public void Attack(float damage)
{
// ์ด๋ฒคํธ ํธ์ถ
OnAttack?.Invoke(damage);
// null ์กฐ๊ฑด๋ถ ์ฐ์ฐ์
// null ์ฐธ์กฐ๊ฐ ์๋ ๊ฒฝ์ฐ์๋ง ๋ฉค๋ฒ์ ์ ๊ทผํ๊ฑฐ๋ ๋ฉ์๋๋ฅผ ํธ์ถ
}
}
// ํ๋ ์ด์ด ํด๋์ค
public class Player
{
// ํ๋ ์ด์ด๊ฐ ๋ฐ์ ๋ฐ๋ฏธ์ง ์ฒ๋ฆฌ ๋ฉ์๋
public void HandleDamage(float damage)
{
// ํ๋ ์ด์ด์ ์ฒด๋ ฅ ๊ฐ์ ๋ฑ์ ์ฒ๋ฆฌ ๋ก์ง
Console.WriteLine("ํ๋ ์ด์ด๊ฐ {0}์ ๋ฐ๋ฏธ์ง๋ฅผ ์
์์ต๋๋ค.", damage);
}
}
// ๊ฒ์ ์คํ
static void Main()
{
// ์ ๊ฐ์ฒด ์์ฑ
Enemy enemy = new Enemy();
// ํ๋ ์ด์ด ๊ฐ์ฒด ์์ฑ
Player player = new Player();
// ํ๋ ์ด์ด์ ๋ฐ๋ฏธ์ง ์ฒ๋ฆฌ ๋ฉ์๋๋ฅผ ์ ์ ๊ณต๊ฒฉ ์ด๋ฒคํธ์ ์ถ๊ฐ
enemy.OnAttack += player.HandleDamage;
// ์ ์ ๊ณต๊ฒฉ
enemy.Attack(10.0f);
}
3-4. ๋ธ๋ฆฌ๊ฒ์ดํธ๋ฅผ ๋๋ค์์ผ๋ก ํ ๋น
๋๋ค๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฉ์๋์ ์ด๋ฆ ์์ด ๋ฉ์๋๋ฅผ ๋ง๋ค ์ ์๋ค.
๊ทธ๋ฆฌ๊ณ ๋ธ๋ฆฌ๊ฒ์ดํธ๋ฅผ ํ๋ก ์ฌ์ฉํ์ ๋๋ณด๋ค ์ฝ๋๋ ๊ฐ๊ฒฐํด์ก๋ค.
using System;
// ๋ธ๋ฆฌ๊ฒ์ดํธ ์ ์ธ
delegate void MyDelegate(string message);
class Program
{
static void Main()
{
// ๋ธ๋ฆฌ๊ฒ์ดํธ ์ธ์คํด์ค ์์ฑ ๋ฐ ๋๋ค์ ํ ๋น
MyDelegate myDelegate = (message) =>
{
Console.WriteLine("๋๋ค์์ ํตํด ์ ๋ฌ๋ ๋ฉ์์ง: " + message);
};
// ๋ธ๋ฆฌ๊ฒ์ดํธ ํธ์ถ
myDelegate("์๋
ํ์ธ์!");
Console.ReadKey();
}
}'๐ ์ ๋ํฐ (Unity)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| Day 18 - C# (์์ ๋ณต์ฌ, ๊น์ ๋ณต์ฌ, ICloneable) (1) | 2024.05.03 |
|---|---|
| Day 17 - C#(Func & Action, Where๋ฌธ) (0) | 2024.05.02 |
| Day 15 - C# (์ธํฐํ์ด์ค, Enums ์ด๊ฑฐํ, ์์ธ ์ฒ๋ฆฌ try-catch) (2) | 2024.04.30 |
| Day 14 - ์ปค๋ฐ ์ปจ๋ฒค์ ์ค์ & README.md ์์ฑ (1) | 2024.04.29 |
| Day 13 - C# (์ ๋๋ฆญ, out & ref ํค์๋) (0) | 2024.04.28 |