using System.Xml;
namespace CSharp1
{
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>();
static Item FindItem(Func<Item,bool> 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 : 일회용 함수를 만드는데 사용하는 문법이다.
//Anonymous Function 무명 함수 /익명 함수
//Item item = FindItem(delegate(Item item) { return item.itemType == ItemType.Weapon; });
//람다식 일회성 함수
//delegate를 직접 선언하지 않아도, 이미 만들어지 애들이 존재한다.
//반환타입이 있을 경우 Func
//반환타입이 없을 경우 Action
Item item = FindItem((Item item) => { return item.itemType == ItemType.Weapon; });
}
}
}
'C#' 카테고리의 다른 글
Reflection(리플렌션) (0) | 2023.07.12 |
---|---|
Exception(예외 처리) (0) | 2023.07.12 |
Event(이벤트) (0) | 2023.07.12 |
Delegate(대리자) (0) | 2023.07.10 |
Property(프로퍼티) (0) | 2023.07.10 |