Reputation: 945
Can anybody help me to write ternary operator code for following code
if(s> b)
{
minlength = s.length;
maxlength = b.length;
}
else
{
minlength = b.length;
maxlength = s.length;
}
i have tried the following code but it gives me an error
s.Length > B.Length ? ( minlength = B.Length,maxlength = s.Length ) : ( minlength = s.Length, maxlength = B.Length);
when i use above code it gives an error
"only assignment call increment decrement and new object expressions can be used as a statement"
Can anybody help me to resolve this...
Upvotes: 0
Views: 212
Reputation: 1460
Do you really need to use a ternary operator? I would have thought that a simple Math.Min/Max would be clearer to anyone reading it:
minlength = Math.Min(s.length, b.length);
maxlength = Math.Max(s.length, b.length);
...but that's just me :-)
Upvotes: 0
Reputation: 2978
why just write it in this way :
minlength = (s > b) ? s.length : b.length;
maxlength = (s > b) ? b.length : s.length;
Upvotes: 1