Unity ? 연산자

https://docs.microsoft.com/ko-kr/dotnet/csharp/language-reference/operators/conditional-operator 

?: 연산자(C# 참조)

다음 예제와 같이, 3개로 구성된 조건 연산자라고도 하는 조건 연산자 ?:은 부울 식을 계산하고 부울 식이 true 또는 false로 계산되는지에 따라 두 식 중 하나의 결과를 반환합니다.

C#
string GetWeatherDisplay(double tempInCelsius) => tempInCelsius < 20.0 ? "Cold." : "Perfect!";

Console.WriteLine(GetWeatherDisplay(15));  // output: Cold.
Console.WriteLine(GetWeatherDisplay(27));  // output: Perfect!

위의 예제에서 볼 수 있듯이 조건 연산자의 구문은 다음과 같습니다.

C#
condition ? consequent : alternative

condition 식은 true 또는 false로 계산되어야 합니다. condition이 true로 계산되면 consequent 식이 계산되고 해당 결과가 연산 결과가 됩니다. condition이 false로 계산되면 alternative 식이 계산되고 해당 결과가 연산 결과가 됩니다. consequent 또는 alternative만 계산됩니다.

C# 9.0부터 조건식은 대상으로 유형화됩니다. 즉, 조건식의 대상 유형을 알고 있는 경우 consequent 및 alternative의 형식은 다음 예제와 같이 대상 유형으로 암시적으로 변환 가능해야 합니다.

C#
var rand = new Random();
var condition = rand.NextDouble() > 0.5;

int? x = condition ? 12 : null;

IEnumerable<int> xs = x is null ? new List<int>() { 0, 1 } : new int[] { 2, 3 };

조건식의 대상 유형을 모르는 경우(예: var 키워드 사용) 또는 C# 8.0 이하에서 consequent 및 alternative의 형식이 같아야 하거나 한 형식에서 다른 형식으로 암시적인 변환이 있어야 합니다.

C#
var rand = new Random();
var condition = rand.NextDouble() > 0.5;

var x = condition ? 12 : (int?)null;

조건부 연산자는 오른쪽 결합성입니다. 즉, 다음 형식의 식을 가정해 보세요.

C#
a ? b : c ? d : e

댓글

이 블로그의 인기 게시물

About AActor!!! "UObject" has no member "BeginPlay"

UNREAL Android build information

C++ 생성자 위임 (delegating constructor)