Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- 자료구조
- 힙영역
- c#
- thread
- std::unordered_map
- 큐
- 차이점
- 리스트
- 공부
- Queue
- 기술면접
- 객체지향
- static_cast
- vector
- 스택영역
- std::map
- 반복문
- rookiss
- 배열
- 인프런
- 벡터
- 알고리즘
- 트리
- 프래그멘테이션
- map
- MonoBehaviour
- 해쉬맵
- 멀티쓰레드
- list
- 스택
Archives
- Today
- Total
호빵의 IT 개발소
[C#] Lambda 본문
Lambda : 일회용 함수를 만드는데 사용하는 문법입니다.
enum ItemType //아이템
{
Weapon,
Armor,
Amulet,
Ring
}
enum Rarity //희귀도
{
Normal,
Uncommon,
Rare
}
class Item
{
public ItemType ItemType;
public Rarity Rarity;
}
class Program
{
static List<Item> _items = new List<Item>();
delegate bool ItemSelector(Item item); //delegate타입
static Item FindItem(ItemSelector selector)
{
foreach (Item item in _items)
{
if (selector(item)) //아이템이 무사히 통과하면
return item; //아이템을 넘긴다.
}
return null;
}
static void Main(string[] args)
{
_items.Add(new Item() { ItemType = ItemType.Weapon, Rarity = Rarity.Normal });
_items.Add(new Item() { ItemType = ItemType.Armor, Rarity = Rarity.Uncommon });
_items.Add(new Item() { ItemType = ItemType.Ring, Rarity = Rarity.Rare });
//Lambda
Item item = FindItem((Item item) => { return item.ItemType == ItemType.Weapon; });
//Anonymous Function : 무명 함수 / 익명 함수
Item item = FindItem(delegate (Item item) { return item.ItemType == ItemType.Weapon; });
}
}
delegate를 직접 선언하지 않아도, 이미 만들어진 애들이 존재한다면
-> 반환 타입이 있을 경우 Func
-> 반환 타입이 없으면 Action
---------------------------------------------------------------------------------------------------------------------------
참고 : [인프런] Rookiss님의 [C#과 유니티로 만드는 MMORPG 게임 개발 시리즈] Part1: C# 기초 프로그래밍 입문
'C#, 유니티 > C# 기초' 카테고리의 다른 글
| [C#] Event (0) | 2022.01.09 |
|---|---|
| [C#] Delegate (대리자) (0) | 2022.01.09 |
| [C#] Property (0) | 2022.01.09 |
| [C#] Interface (0) | 2022.01.09 |
| [C#] Generic(일반화), object (0) | 2022.01.09 |
Comments