Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- ubuntu
- YOLO
- VS Code
- 오류
- Numpy
- windows forms
- C++
- C#
- Docker
- Linux
- label
- JSON
- C
- 핑거스타일
- mysql
- 프로그래머스
- 채보
- 컨테이너
- SSH
- pip
- 기타 연주
- Python
- pytorch
- paramiko
- error
- OpenCV
- Visual Studio
- Selenium
- nvidia-smi
- pandas
Archives
- Today
- Total
기계는 거짓말하지 않는다
C# is, as, typeof 키워드 본문
- is
is는 지정된 형식과 호환되는지 확인한다.
public class Animal { } public class Rabbit : Animal { } class Program { static void Main(string[] args) { Animal A = new Animal(); Console.WriteLine(A is Animal); // True Console.WriteLine(A is Rabbit); // False Rabbit R = new Rabbit(); Console.WriteLine(R is Animal); // True Console.WriteLine(R is Rabbit); // True } }
- as
as는 지정된 형식 또는 nullable 값 형식으로 명시적으로 변환한다.
변환할 수 없는 경우 null을 반환한다. 캐스트 식과 달리 as는 예외를 발생시키지 않는다.
as를 사용하지 않는 강제 형변환(cast)보다 권장된다.
public class Animal { } public class Rabbit : Animal { } class Program { static void Main(string[] args) { Animal A = new Animal(); Console.WriteLine(A as Animal == null); // False Console.WriteLine(A as Rabbit == null); // True Rabbit R = new Rabbit(); Console.WriteLine(R as Animal == null); // False Console.WriteLine(R as Rabbit == null); // False } }
- typeof
typeof는 System.Type 인스턴스를 가져온다.
public class Animal { } public class Rabbit : Animal { } class Program { static void Main(string[] args) { Rabbit R = new Rabbit(); Console.WriteLine(R.GetType() == typeof(Animal)); // False Console.WriteLine(R.GetType() == typeof(Rabbit)); // True } }
'C#' 카테고리의 다른 글
C# Thread, Thread 매개변수 (0) | 2022.04.16 |
---|---|
C# Windows Forms 이벤트 등록 (0) | 2021.07.16 |
C# partial class (0) | 2021.07.15 |
C# Windows Forms MenuStrip Check (0) | 2021.07.13 |
C# Windows Forms TextBox 빈 문자 확인 (0) | 2021.07.12 |
Comments