C#
-
널러블(Nullable)C# 2022. 1. 26. 16:38
1. Nullable int형에 대해 null값을 넣고 싶은 경우가 있다. 0을 null값으로 생각해봐도 되는거 아니냐고 반문할 수 있지만 만약 0마저도 사용해야 하는 경우가 생길 때 사용하는 것이 널러블이다. 선언 int? n = null; 사용방법 static int Find() { return 0; } static void Main(string[] args) { int? n = null; n = 3; // int a = n; 구문오류 int a = n.Value; } 그리고 알아야할 점은 int형과 int?형은 서로 다른 형식이라는 점이다. 그래서 int?형의 값을 int형에 대입할 때는 Value를 사용한다. 주의할 점 int?형식에 null값이 들어간 경우 Value를 int형에 넘겨주게 되면 ..
-
리플렉션(Reflection)C# 2022. 1. 26. 16:12
1. Reflection 리플렉션은 클래스의 정보들을 런타임(프로그램 실행) 중에 뜯어보고 분석할 수 있다. using System; using System.Collections.Generic; using System.Reflection; namespace CSharp { class Monster { public int hp; protected int attack; private float speed; void Attack() { } } class Program { static void Main(string[] args) { Monster monster = new Monster(); Type type = monster.GetType(); // GetType은 Object클래스의 멤버 // public, ..
-
예외처리C# 2022. 1. 26. 15:33
1. try catch c++과 같이 try와 catch를 사용하여 예외를 처리할 수 있다. try의 내용에서 예외가 발생하면 catch에서 처리가 된다. using System; using System.Collections.Generic; namespace CSharp { class Program { static void Main(string[] args) { try { int a = 5; int b = 0; int c = a / b; } catch(DivideByZeroException e) { } catch(Exception e) { } finally { } } } } 위의 코드는 0으로 나누는 경우를 예외처리하는 코드이다. DivideByZeroException은 0으로 나누는 경우를 예외처리하는..
-
람다식(Lambda)C# 2022. 1. 26. 15:11
1. 무명함수(익명함수) using System; using System.Collections.Generic; namespace CSharp { 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(); delegate bool ItemSelector(Item item); static bool IsWeapon(Item item) { return item.ItemType == ItemType.Weapon; } sta..
-
이벤트(Event)C# 2022. 1. 26. 14:12
1. delegate의 문제점 delegate는 사용하기 편하지만 main함수에서도 델리게이트 객체에 직접 접근이 가능하다는 문제점이 있다. public delegate void OnInputKey(); static void Test() { Console.WriteLine("Test 호출"); } static void Test2() { Console.WriteLine("Test2 호출"); } static void UseOnInputKey(OnInputKey inputKey) { inputKey(); } static void Main(string[] args) { OnInputKey inputKey = new OnInputKey(Test); inputKey += Test2; UseOnInputKey(in..
-
대리자(Delegate)C# 2022. 1. 25. 18:58
1. delegate C++에서 함수포인터와 유사하다 함수포인터는 매개변수에 함수를 인자로받아서 함수를 호출하였다. delegate도 마찬가지로 함수를 인자로받아서 함수를 호출할 수 있다. delegate선언 delegate int GetNumber(int n); delegate 사용예시 class Program { delegate int GetNumber(int n); static int AddOne(int number) { return number + 1; } static void Test(int n, GetNumber getnumber) { n = getnumber(n); Console.WriteLine(n); } static void Main(string[] args) { GetNumber get..
-
프로퍼티(Property)C# 2022. 1. 25. 17:56
1. 은닉성 객체지향의 은닉성을 지키기위해서 캡슐화를 통해 메소드를 사용하여 멤버변수에 접근하거나 반환받는 방식을 사용했다. 캡슐화 사용예시 class Orc { protected int hp; protected int attack; int GetHp() { return hp; } void SetHp(int hp) { this.hp = hp; } int GetAttack() { return attack; } void SetAttack(int attack) { this.attack = attack; } } 2. get, set(프로퍼티) 프로퍼티를 사용하면 더 이상 Get과 Set관련된 함수를 따로 만들지 않아도 된다. get, set 선언방법 public int Hp { get { return hp; }..
-
추상클래스와 인터페이스(Interface)C# 2022. 1. 25. 16:02
1. 추상클래스 부모클래스의 멤버함수를 자식클래스가 상속받을 때 멤버함수의 오버라이딩을 강요하고 싶을 때 사용하면 된다. 추상클래스 정의 abstract class MyClass { } 추상함수 정의 abstract class MyClass { public abstract void Coda(); } 추상함수는 자식클래스에서 오버라이딩을 통해 반드시 정의해야함. 2. 인터페이스 인터페이스를 상속받은 클래스는 반드시 인터페이스의 멤버를 재정의 해야한다. 정의 interface IFlyale { void Fly() { } } 사용방법 class Monster { } interface IFlyale { void Fly() { } } class Orc : Monster, IFlyale { } 인터페이스는 상속과 ..