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

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() : "");

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

It is the ternary conditional operator.

If the condition in the parenthesis before the ? is true, it returns the value to the left of the :, otherwise the value to the right.

Method 3

It’s a ternary operator, or the short form for if..else.

condition ? value if true : value if false

See Microsoft Docs | ?: operator (C# reference).

Method 4

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

can be translated to:
string requestUri="";
if ((OperationURL[1] == "GET")
{
    requestUri = _apiURL + "?e=" + GetRequestSignature();
}
else
{
   requestUri = _apiURL + "?e=";
}

Method 5

This is also known as the “inline if”, or as above the ternary operator.
https://en.wikipedia.org/wiki/%3F:

It’s used to reduce code, though it’s not recommended to use a lot of these on a single line as it may make maintaining code quite difficult.
Imagine:

a = b?c:(d?e:(f?g:h));

and you could go on a while.

It ends up basically the same as writing:

if(b)
  a = c;
else if(d)
  a = e;
else if(f)
  a = g;
else
  a = h;

In your case, “string requestUri = _apiURL + “?e=” + OperationURL[0] + ((OperationURL[1] == “GET”) ? GetRequestSignature() : “”);”

Can also be written as: (omitting the else, since it’s an empty string)

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

or like this:
string requestUri;
if((OperationURL[1] == "GET")
    requestUri = _apiURL + "?e=" + OperationURL[0] + GetRequestSignature();
else
    requestUri = _apiURL + "?e=" + OperationURL[0];

Depending on your preference / the code style your boss tells you to use.

Method 6

In the particular case you’ve provided, it’s a conditional assignment. The part before the question mark (?) is a boolean condition, and the parts either side of the colon (:) are the values to assign based on the result of the condition (left side of the colon is the value for true, right side is the value for false).

Method 7

It means if “OperationURL[1]” evaluates to “GET” then return “GetRequestSignature()” else return “”. I’m guessing “GetRequestSignature()” here returns a string. The syntax CONDITION ? A : B
basically stands for an if-else where A is returned when CONDITION is true and B is returned when CONDITION is false.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x