0

Probably a simple syntax problem. This is an attempt at a console program that reads the length of a string that is received via user input. If the length is greater than 144, the user is notified that the string length is too long, otherwise the string inputted is just output to the console.

string input = Console.ReadLine();
(input.Length > 144) ? Console.WriteLine("The message is too long"); : Console.WriteLine(input);
Console.ReadLine();

Getting syntax errors in the present state on line 2. Am I missing parentheses?

2
  • 2
    remove the semicolon Commented Aug 23, 2016 at 19:11
  • @Rakitić - Removing the semicolon in the middle won't help because Console.WriteLine returns void. The ternary operator in C# is not a shortcut for an If/else Each side of the : in the operator must evaluate to some value and the result must be assigned or used somewhere. Commented Aug 23, 2016 at 21:56

2 Answers 2

8

try:

Console.WriteLine((input.Length > 144) ? "The message is too long" : input);

you need to use the return value of the operator, or receive a compile-time error Only assignment, call, increment, decrement, and new object expressions can be used as a statement.

None of these other answers will compile, I'm not sure what everyone is getting at.

Sign up to request clarification or add additional context in comments.

1 Comment

sure thing @AlexEdwards
-3

You have an extra semi-colon. The ternary expression is ONE expression, so it only has one semi-colon at the end of it.

(input.Length > 144) ? Console.WriteLine("The message is too long") /*No Semi Here*/ : Console.WriteLine(input);

I believe that in C# (unlike C and C++), a ternary expression cannot be standalone.
The result of it must be assigned or used.

The expression overall must have a value, but Console.WriteLine does not return a value (return type void). You cannot have a ternary that evaluates to type void.

You have tried to use the ternary as a standalone statement, which is not legal.

7 Comments

It doesn't compile.
Well you kind explained it your self, "a ternary expression cannot be standalone. The result of it must be assigned or used.", Console.WriteLine returns void, how do you expect to use the result of a void?
Per your edit: It's not that he used it as a stand alone statement, it is the fact that he is calling a function that returns void for the two options. Doing var foo = /*rest of code*/ won't fix the problem, but your answer states that it would.
Then you did not read my complete answer. I first corrected his syntax error (the extra semicolon in the midst of the statement), and then explained his mis-use of the expression overall. I stand by my answer.
How can you stand for something that doesn't even compile? Did you try id?
|

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.