0

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

1

2 Answers 2

1

why just write it in this way :

minlength = (s > b) ?  s.length : b.length;
maxlength = (s > b) ?  b.length : s.length;
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.