Question mark and colon sign in a statement? what does it mean?

What is he doing? and colon means?

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

In the following statement ...

 string requestUri = _apiURL + "?e=" + OperationURL[0] + ((OperationURL[1] == "GET") ? GetRequestSignature() : ""); 
+53
c # web-services
Aug 05 2018-11-11T00:
source share
7 answers

This is a conditional statement of the operator.

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

for example

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

So, if the logical expression is true, the value will contain "Alpha", otherwise it contains "Beta".

For a common mistake people get in, see this question in the C # tag wikita .

+81
Aug 05 2018-11-11T00:
source share

This is a ternary conditional statement .

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

+13
Aug 05 2018-11-11T00:
source share

This is a ternary operator or short form for if else.

condition? value if true: value if false

Here is a link to additional information on the topic

Edit: link fixed

+6
Aug 05 2018-11-11T00:
source share

In the specific case that you have provided, this is a conditional assignment. The part before the question mark (?) Is a logical condition, and the parts on both sides of the colon (:) are the values ​​assigned based on the result of the condition (the left part of the colon is the value for true, the right side is the value for false).

+1
Aug 05 2018-11-11T00:
source share
 string requestUri = _apiURL + "?e=" + OperationURL[0] + ((OperationURL[1] == "GET") ? GetRequestSignature() : ""); 

can be translated into:

 string requestUri=""; if ((OperationURL[1] == "GET") { requestUri = _apiURL + "?e=" + GetRequestSignature(); } else { requestUri = _apiURL + "?e="; } 
+1
Aug 05 2018-11-11T00:
source share

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

He used to shorten the code, although it is not recommended to use many of them on one line, as this can complicate code execution. Imagine:

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

and you can continue.

It ends basically the same as the entry:

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

It can also be written as: (else exception, since this is 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 / code style, your boss tells you to use.

+1
Apr 08 '16 at 14:17
source share

This means that "OperationURL [1]" evaluates to "GET" and then returns "GetRequestSignature ()" else return "". I assume that "GetRequestSignature ()" here returns a string. Syntax CONDITION? A: B basically means if-else, where A is returned when CONDITION is true, and B is returned when CONDITION is false.

0
Aug 05 2018-11-11T00:
source share



All Articles