본문 바로가기

Study64

[C#][Study][기초다지기] 딜리게이트 event 함수 Event 함수 - event 함수 호출시 연결된 delegate를 이용해 Event Handler(이벤트 처리 함수)를 호출하는 기능 - event 키워드를 사용하여 생성 - Event Handler는 리턴형이 없어 항상 void로 반환함 - 모든 이벤트는 delegate를 기반으로 함 - 이벤트는 객체가 있어야 함 - 이벤트 처리 함수는 이름 앞에 보통 "On"으로 시작하게 함 [이벤트 동작 순서] 1) delegate 정의 2) delegate와 동일한 이름으로 이벤트를 정의 3) 이벤트 발생시 처리될 이벤트 처리 함수(Event Handler)를 정의 4) delegate를 위한 함수가 있어야 함 using System; using System.Collections.Generic; using Sy.. 2023. 9. 23.
[C#][Study][기초다지기] 딜리게이트 Multicast Delegate Multicast Delegate - delegate에 여러 함수를 연결해서 사용가능 - 여러 함수가 연결된 delegate 호출시 FIFO(First In First Out) 순으로 호출 - '+' 또는 '+=' 가 delegate에 함수를 추가할 때 사용 - '-' 또는 '-=' 은 delegate에서 함수를 제거할 때 사용 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Multicast_Delegates { class TestDeleGate { // 선언 public delegate void ShowMessage(strin.. 2023. 9. 23.
[C#][Study][기초다지기] 딜리게이트 Delegate Delegate - 메서드의 주소를 저장하고, 호출할 수 있음 - 내용을 가지지 않음 - 메서드를 캡슐화하기 때문에 안전하고, 객체지향적임 - 함수 포인터 - delegate와 delegate에 추가할 함수는 같은 리턴값을 가져야 함 [3가지 유형] 1) 매개변수가 없고, 리턴타입이 없는 유형 public delegate void TestDelegate(); 2) 매개변수를 가지고, 리턴타입이 없는 유형 public delegate void TestDelegate(object obj1, object obj2); 3) 매개변수와 리턴타입이 모두 있는 유형 public delegate int TestDelegate(object obj1, object obj2); [3가지 단계] 1) Declaration(선.. 2023. 9. 23.
[C#][Study][기초다지기] 봉인 클래스 sealed 봉인 클래스 - sealed - 봉인 클래스는 부모 클래스가 될 수 없음 -> 파생(상속) 방지(자식을 만들 수 없음을 의미) [ex] using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Sealed_Class { class Program { static void Main(string[] args) { SealedClass s = new SealedClass(); s.message(); Console.ReadKey(); } } public sealed class SealedClass { public void message() { Co.. 2023. 9. 23.
[C#][Study][기초다지기] 다중 상속 - 인터페이스 interface 다중 상속 - 인터페이스(Interface) "C#은 아래와 같은 이유로 다중 상속을 지원하지 않음" - 다중 상속은 이점은 거의 없지만 너무 복잡함 - 부모 자식간에 충돌할 가능성이 큼 - 구현에 많은 부담을 주고 실행 속도 저하가 발생함 "따라서, 다중 상속이 아닌 Interface를 이용해 여러 특성을 상속 받음" [ex] using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Interface_Inheritance { class Program { static void Main(string[] args) { child ch = ne.. 2023. 9. 23.
[C#][Study][기초다지기] 가상 함수 virtual 가상 함수 - virtual - 추상 함수와는 달리 상속받은 자식 클래스에서 반드시 재정의 될 필요가 없음 - 해당 가상 함수에만 키워드 virtual이 들어가면 됨 [ex] using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Virtual_Methods { class Program { static void Main(string[] args) { child1 ch1 = new child1(); ch1.message(); child2 ch2 = new child2(); ch2.message(); Console.ReadKey(); } }.. 2023. 9. 23.
[C#][Study][기초다지기] 추상 함수 abstract 추상 함수 - abstract - 기본 클래스가 해당 특징을 가진다는 것을 '명시'하기위해 구현 - abstract 키워드를 사용해 생성되었지만, 본문 내에서 구현하는 것은 따로 없음 - 추상 클래스에서 정의된 추상함수는 본 함수를 상속받은 하위 클래스들에서 '반드시' 재정의(override) 되어야한다. - 클래스 내부에 abstract를 사용한 키워드가 있는 경우, 반드시 클래스에도 abstract 키워드를 사용해서 정의해야 한다. - abstract 클래스는 인스터화 할 수 없음(new 키워드로 생성 불가) [ex] using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Thre.. 2023. 9. 23.
[C#][Study][기초다지기] 생성자, 소멸자 생성자, 소멸자 [생성자] - 객체를 초기화할 때 사용 - 값을 리턴하지 않음(리턴타입이 있으면 안됨) - 클래스 이름과 동일 - 인수없이 초기화 가능 - 여러 생성자가 동시 존재할 수 있음(오버로드) [소멸자] - 클래스의 모든 인스턴스를 제거하고 리소스를 해제하는데 사용 - (~) + 클래스명 - 구조체에서는 사용 불가 - 단 하나만 존재 가능 - 오버로드 되거나 상속될 수 없음 - 호출 불가 -> 자동 호출 - 매개변수 없음 [ex] using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace constructor_initialize {.. 2023. 9. 23.
[C#][Study][기초다지기] 예외처리 예외처리 - try : 예외를 일으킬 수 있는 코드 식별 - catch : 예외처리 - throw : 사용자 정의 예외 메시지 생성 - finally : 예외 발생 여부에 관계없이 실행 [ex] using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Exception_Handling { class Program { static void Main(string[] args) { label: // Try block: 런타임에 발생하는 예외를 확인한다. try { int num1, num2; decimal result; Console.Write.. 2023. 9. 23.
[C#][Study][기초다지기] 배열 배열 [다차원 배열 선언] string[,] arr = new arr[3, 3]; // 자주 헷갈리는 것들 // 1. string[][] arr = new string[][]; // 괄호 2개 x // 2. string arr[,] = new string[3, 2]; // 선언할 때는 괄호가 먼저 와야함 [매개변수 배열] - 매개변수의 개수가 확실하지 않거나, n개의 매개변수를 허용하는 메서드를 생성할 때 사용 - params 키워드 + 1차원 배열 static int add(params int[] allnumber) (ex) 예제 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace p.. 2023. 9. 23.
[C#][Study][기초다지기] out 매개변수 out 매개변수 - 여러 값을 반환해주기 위해 사용 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace out_parameter { class Program { // 네 개의 매개변수 중 두 개는 출력값으로 반환된다. public static void rect(int len, int width, out int area, out int perimeter) { area = len * width; perimeter = 2 * (len + width); } static void Main(string[] args) { int area, perimeter; // 두 개의 반환 값을 가져옴 Prog.. 2023. 9. 23.
[C#][Study][기초다지기] Main 메서드 Main 메서드 - 프로그램 실행이 시작하고 끝나는 위치 - 단 하나만 있을 수 있고, 클래스나 구조체 내부에 있어야 하며, static을 사용해 선언해야 한다. - Main 메서드는 다른 함수 메서드에서 호출할 수 없어야 하기 때문에 항상 static으로 선언된다. 2023. 9. 23.