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 to only mean reference types, but when .NET 2.0 came out, it introduced the concept of a nullable value type. Since these types can be assigned a null value, they are valid to use with the as operator.
https://stackoverflow.com/questions/4926677/c-sharp-as-cast-vs-classic-cast
'유니티 > 팁, 정보 노트' 카테고리의 다른 글
[Unity]씬 버튼 하나로 준비하기 (0) | 2019.10.20 |
---|---|
[Unity]에디터 상태에서의 프리팹 Instantiate (0) | 2019.10.20 |
[Unity]EventSystem을 이용해 아이템UI 드래그 및 다른 슬롯에 등록하기(IDragHandler, IDropHandler) (3) | 2019.09.27 |
[Unity]UI뒤로 클릭 막기 (0) | 2019.07.02 |
[Unity]Array를 List로 변환 (0) | 2019.04.15 |