C# – Meaning of Question Mark and Colon in Statements

c++

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

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

It appears in the following statement:

string requestUri = _apiURL + "?e=" + OperationURL[0] + ((OperationURL[1] == "GET") ? GetRequestSignature() : "");

Best Answer

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.