问号和冒号是什么意思?这是什么意思?

时间:2022-06-05 22:26:48

What does the ? and colon mean?

的什么?和结肠的意思吗?

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

In the following statement...

在接下来的声明……

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

7 个解决方案

#1


88  

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".

因此,如果布尔表达式为真,value将保存“Alpha”,否则,它将保存“Beta”。

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

要了解人们常犯的一个错误,请参阅c#标签wiki中的这个问题。

#2


14  

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.

如果括号中的条件是?为true,它返回:左边的值,否则返回右边的值。

#3


6  

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

它是三元运算符,或者是if else的缩写形式。

condition ? value if true : value if false

条件?如果为真值:如果为假值

Here is a link to more information on the subject

这里有一个关于这个主题的更多信息的链接

edit: link fixed

编辑:链接固定

#4


1  

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).

在你提供的特定情况下,它是一个条件分配。问号(?)前面的部分是一个布尔条件,冒号(:)两边的部分是根据条件的结果分配的值(冒号的左边为true,右边为false)。

#5


1  

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=";
}

#6


1  

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

这也被称为“内联if”,或在三元运算符之上。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() : "");"

在您的例子中,“string requestUri = _apiURL +”?e=" + OperationURL[0] + (OperationURL[1] =" GET") ?GetRequestSignature():");"

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

也可以写成:(省略else,因为它是一个空字符串)

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.

取决于你的喜好/你老板告诉你的代码风格。

#7


0  

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.

它意味着如果“OperationURL[1]”计算为“GET”,那么返回“GetRequestSignature()”“else return”。我猜这里的“GetRequestSignature()”返回一个字符串。的语法条件?A: B基本上代表一个if-else,当条件为真时返回A,当条件为false时返回B。

#1


88  

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".

因此,如果布尔表达式为真,value将保存“Alpha”,否则,它将保存“Beta”。

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

要了解人们常犯的一个错误,请参阅c#标签wiki中的这个问题。

#2


14  

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.

如果括号中的条件是?为true,它返回:左边的值,否则返回右边的值。

#3


6  

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

它是三元运算符,或者是if else的缩写形式。

condition ? value if true : value if false

条件?如果为真值:如果为假值

Here is a link to more information on the subject

这里有一个关于这个主题的更多信息的链接

edit: link fixed

编辑:链接固定

#4


1  

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).

在你提供的特定情况下,它是一个条件分配。问号(?)前面的部分是一个布尔条件,冒号(:)两边的部分是根据条件的结果分配的值(冒号的左边为true,右边为false)。

#5


1  

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=";
}

#6


1  

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

这也被称为“内联if”,或在三元运算符之上。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() : "");"

在您的例子中,“string requestUri = _apiURL +”?e=" + OperationURL[0] + (OperationURL[1] =" GET") ?GetRequestSignature():");"

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

也可以写成:(省略else,因为它是一个空字符串)

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.

取决于你的喜好/你老板告诉你的代码风格。

#7


0  

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.

它意味着如果“OperationURL[1]”计算为“GET”,那么返回“GetRequestSignature()”“else return”。我猜这里的“GetRequestSignature()”返回一个字符串。的语法条件?A: B基本上代表一个if-else,当条件为真时返回A,当条件为false时返回B。