Question mark and colon in statement. What does it mean?

后端 未结 7 1422
南方客
南方客 2020-11-27 14:03

What do the question mark (?) and colon (:) mean?

((OperationURL[1] == "GET") ? GetRequestSignature() : "")
         


        
7条回答
  •  失恋的感觉
    2020-11-27 14:35

    This is the conditional operator expression.

    (condition) ? [true path] : [false path];
    

    For example

     string value = someBooleanExpression ? "Alpha" : "Beta";
    

    So if the boolean expression is true, value will hold "Alpha", otherwise, it holds "Beta".

    For a common pitfall that people fall into, see this question in the C# tag wiki.

提交回复
热议问题