본문 바로가기
반응형

C#41

Nullable(널러블) using System; using System.Collections; using System.Reflection; namespace CSharp1 { class Program { static int Find() { return 0; } class Monster { public int Id { get; set; } } static void Main(string[] args) { Monster monster = null; if (monster != null) { int monsterId = monster.Id; } int? id = monster?.Id; //위아래 같은 뜻 if (monster == null) { id = null; } else { id = monster.Id; } //Nullable -.. 2023. 7. 12.
Reflection(리플렌션) using System; using System.Collections; using System.Reflection; namespace CSharp1 { class Program { class Important : System.Attribute { string message; public Important(string message) { this.message = message; } } class Monster { //hp입니다. 중요한 정보 입니다. [Important("Very Important")] public int hp; protected int attack; private float speed; void Attack() { } } static void Main(string[] args) { //.. 2023. 7. 12.
Exception(예외 처리) using System.Xml; namespace CSharp1 { class TestException : Exception { } class Program { static void Main(string[] args) { /* int a = 10; int b = 0; int result = a / b;*///오류남 try { //1. 0으로 나눌 때 //2. 잘못된 메모리를 참조(null) //3. 오버플로우 int a = 10; int b = 0; int result = a / b; throw new TestException(); int c = 0;//이부분은 실행 안 됨 } catch (DivideByZeroException e)//{"Attempted to divide by zero."} { } c.. 2023. 7. 12.
Lambda(람다식) 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 _items = new List(); static Item FindItem(Func selector) { foreach(Item item in _items) { if(selector(item)) return item; } return null; } static void Main(string[] args) { _items.Add(.. 2023. 7. 12.
Event(이벤트) using System.Xml; namespace CSharp1 { //Observer Pattern internal class Program { static void OnInputTest() { Console.WriteLine(); Console.WriteLine("Input Received!"); } static void Main(string[] args) { InputManager inputManager = new InputManager(); inputManager.InputKey += OnInputTest; while (true) { inputManager.Update(); } } } } using System; namespace CSharp1 { class InputManager { public.. 2023. 7. 12.
Delegate(대리자) using System.Xml; namespace CSharp1 { internal class Program { delegate int OnClicked(); //delegete ->형식은 형식인데, 함수 자체를 인자로 넘겨주는 그런 형식 //반확:int 입력:void //OnClicked 이 delegate 형식의 이름이다! //UI static void ButtonPressed(OnClicked clickedFunction) { clickedFunction(); } static int TestDelegate() { Console.WriteLine("Hello Delegate"); return 0; } static int TestDelegate2() { Console.WriteLine("Hello De.. 2023. 7. 10.
반응형