기초 메모지

C# - 삼항 연산자(Ternary Operator) 본문

Languages/C#

C# - 삼항 연산자(Ternary Operator)

라큐브 2022. 7. 8. 16:38

삼항 연산자 형식은 다음과 같습니다.

(조건 식 ? True에 실행할 코드 : False에 실행할 코드)

 

삼항 연산자와 일반 조건문을 비교했을때 더 간결한 표현이 가능합니다.

int x = 1;
Console.WriteLine(x == 1 ? "참(True)" : "거짓(False)");
int x = 1;

if (x == 1)
{
   Console.WriteLine("참(True)");
}
else
{
    Console.WriteLine("거짓(False)");
}
반응형

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

C# - 큐(Queue)  (0) 2022.07.10
C# - 스택(Stack)  (0) 2022.07.10
C# - 유효 범위(Scope)  (0) 2022.07.07
C# - 상수(Constant), 변수(Variable)  (0) 2022.07.07
C# - 분할 클래스(Partial Class)  (0) 2022.07.05
Comments