Why doesn't this compile in C #?

Why does it work:

if ("xx".StartsWith("x")) { } 

But this is not so:

 if ("xx" + "xx".StartsWith("x")) { } 

The compiler says error CS0029: Cannot implicitly convert type 'string' to 'bool'

+6
source share
12 answers

Item access operator . has a higher priority than the + operator.

Check C # Statements (MSDN) for C # statement priorities. In particular, it lists xy as "primary", which is higher than binary + in "additive".

This means that "xx" + "xx".StartsWith("x") interpreted as
"xx" + ("xx".StartsWith("x")) and does not compile. It combines string and a bool , which gives you string . But you cannot use string as a condition in an if , since it is not bool (and does not implement the true operator)

You seemed to expect it to be interpreted as ("xx" + "xx").StartsWith("x")) , where you first StartsWith lines and then call StartsWith on the concatenated line.

+25
source

Because in the second case, you are trying to compile this code:

  if ("xx" + true) { } 
+18
source

Wrap It In Parens

 if (("xx" + "xx").StartsWith("x")) { } 

The cause of the error is that a string plus a bool = a string , and the if statement expects bool .

+16
source

Since the compiler first performs the function call functions, therefore, it will try to associate "xx" with true :

 if("xx" + "xx".StartsWith("x")) { // becomes if("xx" + true) { 

This implicit conversion is not possible at run time. You must tell the compiler to concatenate the lines first:

 if(("xx" + "xx").StartsWith("x")) { 
+5
source

This is due to the priority of the operators. In your case, StartsWith returns bool and, in combination with the line string + bool, does not return bool, it returns a string, and if () expects bool.

You can change the priority of operations using parentheses.

+5
source

"xx".StartsWith("x") returns a boolean, but "xx" + "xx".StartsWith("x") returns a string that will be either "xxTrue" or "xxFalse".

+2
source

Because in the if expression, the boolean expression is not expected to be string.

+2
source
 "xx".StartsWith("x") 

returns bool , and when you try to add "xx" to it, it converts it to string .

Change your code to

 if (("xx" + "xx").StartsWith("x")) { } 
+2
source

if () expects a boolean value that is not your expression. This is different from C / C ++, which expects an int value as a condition.

+2
source

"xx" + "xx" .StartsWith ("x") is not a boolean expression. It evaluates the value of "xxTrue".

0
source

The concatenation operator (+) performs an implicit conversion to a string in the second example. This example also checks if a string is set if it does not start with something.

0
source

The reason is because the String.StartsWith() method returns a boolean value ( ref ). Thus, you apply the + operator to the string and Boolean, which does not work. If you want to instantiate strings, call String.StartsWith() on the newly created; add some brackets:

 if(("xx" + "xx").StartsWith("x")) { } 

Hope this helps

0
source

Source: https://habr.com/ru/post/887381/


All Articles