Shorthand conditional doesn't evaluate right

  • Thread starter Thread starter John Bailo
  • Start date Start date
J

John Bailo

string s = "ABC";

Debug.WriteLine(

s.Length>5?"greater":"lesser"

);


The compiler reports:

Operator '>' cannot be applied to operands of type 'string' and 'int'



Isn't Length an int ?
 
Works for me.... Is that the exact line you are trying to compile? Usually
the "?" conditional complains about lines such as

s.Length>5?"greater":8
 
John Bailo said:
string s = "ABC";

Debug.WriteLine(

s.Length>5?"greater":"lesser"

);

The compiler reports:

Operator '>' cannot be applied to operands of type 'string' and 'int'

Isn't Length an int ?

I don't think that's your real code. Could you post a short but
complete program which demonstrates the problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Here's an example showing your code working fine:

using System;

class Test
{
static void Main()
{
string s = "ABC";
Console.WriteLine (s.Length>5 ? "greater":"lesser");
}
}
 
Back
Top