Reputation: 14970
Why does this work:
if ("xx".StartsWith("x"))
{
}
But this doesn't:
if ("xx" + "xx".StartsWith("x"))
{
}
Compiler says error CS0029: Cannot implicitly convert type 'string' to 'bool'
Upvotes: 5
Views: 1051
Reputation: 25694
Wrap it in Parens
if (("xx" + "xx").StartsWith("x"))
{
}
The reason for the error is that a string
plus a bool
= a string
, and the if statement is expecting a bool
.
Upvotes: 16
Reputation: 11576
Because the Compiler is evaluating function-calls first, so he will try to concatenate "xx"
with true
:
if("xx" + "xx".StartsWith("x")) {
// becomes
if("xx" + true) {
This implicit conversion is not possible during runtime. You'll need to tell the compiler to concatenate the strings first:
if(("xx" + "xx").StartsWith("x")) {
Upvotes: 5
Reputation: 108880
The member access operator .
has higher priority than the +
operator.
Check C# Operators (MSDN) for the C# operator priorities. In particular it lists x.y
as "primary" which is higher than binary +
in "additive".
This means "xx" + "xx".StartsWith("x")
is interpreted as
"xx" + ("xx".StartsWith("x"))
and that doesn't compile. It concats a string
and a bool
which gives you a string
. But you can't use a string
as a condition in an if
statement since it's not bool
(and doesn't implement the true
operator either)
It seems you expected it to be interpreted as ("xx" + "xx").StartsWith("x"))
where you first concat the strings and then call StartsWith
on the combined string.
Upvotes: 25
Reputation: 76
It has to do with the precedence of operators. In your case, StartsWith returns bool and when combined with a string addition string + bool does not return a bool, it returns a string while the if ( ) expects a bool.
You can change the precedence of operations by using parenthesis.
Upvotes: 5
Reputation: 8036
if () expects a boolean value which your expression isnt. This is in contrast from C/C++ which expects an int value as a condition.
Upvotes: 2
Reputation: 24102
"xx".StartsWith("x")
is returning a bool
and when you try and add "xx" to it, it's converting it to a string
.
Change your code to
if (("xx" + "xx").StartsWith("x"))
{
}
Upvotes: 2
Reputation: 19619
The reason is because the String.StartsWith()
method returns a boolean (ref). So you are applying the +
operator on a string and boolean, which doesn't work. If you wanted to concate the strings, then call String.StartsWith()
on the newly created one; add some parenthesis:
if(("xx" + "xx").StartsWith("x"))
{
}
Hope this helps
Upvotes: 0
Reputation: 30875
Because in the if statement a logical expression is expected not string.
Upvotes: 2
Reputation: 3894
"xx".StartsWith("x")
returns a boolean value, but "xx" + "xx".StartsWith("x")
returns a string that would be either "xxTrue" or "xxFalse".
Upvotes: 2
Reputation: 255
The concatenation operator (+) is doing an implicit cast to a string in the second example. That example is also checking to see if a string is set not if it starts with something.
Upvotes: 0
Reputation: 1756
"xx" + "xx".StartsWith("x") is not a boolean expression. It evaluates to "xxTrue".
Upvotes: 0
Reputation: 705
Because in the second case you try to compile such code:
if ("xx" + true)
{
}
Upvotes: 18