본문 바로가기

C#5

[C#]delegate와 event 정리 delegate는 메소드에 대한 참조로 하나의 타입 event는 delegate에 event 키워드를 선언해서 사용 둘의 차이점은 "event는 외부에서 직접 호출이 불가능하다"는 것이다. 이벤트 기반 프로그래밍을 할 때 보다 안정적으로 사용할 수 있다. 따라서 Delegate는 콜백용도로 사용하고, event는 객체 상태변화나 사건의 발생을 용도로 주로 사용한다. //이벤트 선언 public event Action OnSomethingValueChanged; //외부에서 이벤트 등록 OnSomethingValueChanged += method; //외부에서 직접 호출 불가 EventClass.OnSomethingValueChanged?.Invoke(); //Error!!​ 2021. 12. 5.
C#) null 관련 연산자 (?.) 1. ?. - 널 조건부 연산자, Elvis 연산자 라고 부른다. - 변수에 액세스할때 null 인지 확인하는 역할을 한다. 예) Sytsem.Action _beginCallback; //C# 5 및 그 이전 코드 if (null != _beginCallback) _beginCallback(); 이런 null 체크 코드를 간단하게 _beginCallback?.Invoke(); 로 표현할 수 있다. 2020. 2. 19.
[C#] as vs classic casting With the "classic" method, if the cast fails, an exception is thrown. With the as method, it results in null, which can be checked for, and avoid an exception being thrown. Also, you can only use "as" with reference types, so if you are typecasting to a value type, you must still use the "classic" method. Note: The as method can only be used for types that can be assigned a null value. That use .. 2019. 10. 18.
[Unity]Array를 List로 변환 https://stackoverflow.com/questions/1603170/conversion-of-system-array-to-list 2019. 4. 15.