C#

as와 is

잼잼재미 2024. 10. 15. 10:51

as 연산자


  • 객체에서 캐스팅 할 때 형변환이 가능하면 형변환을 수행하고, 그렇지 않으면 null 값을 return
    • 부모 객체를 자식 객체로 캐스팅 하려할 때, null 값을 return
  • call by reference 값 사이에서만 사용할 수 있으며, call by value 값들 사이에선 사용 불가

 

//parent가 부모클래스 children이 자식클래스일 때 
c= parent as children
if(c !=null) Method();

 

 

is 연산자


  • 형변환이 가능한지를 bool 형으로 return

 

object greeting = "Hello, World!";
if (greeting is string message)
{
    Console.WriteLine(message.ToLower());  // output: hello, world!
}

'C#' 카테고리의 다른 글

문자열로 된 수식 계산  (0) 2024.07.23
10진수를 2진수, 8진수, 16진수로 변환  (0) 2024.02.15
상태 패턴  (0) 2024.01.09
제곱근 구하기 (Math.Sqrt)  (0) 2023.12.18
절대값 구하기  (1) 2023.12.06