Using ternary operator on Console.WriteLine

别来无恙 提交于 2019-12-06 11:57:27

Try this:

Console.WriteLine("Number is " + ((i == m) ? "valid" : "not valid"));

Move your ternary operation inside WriteLine

Console.WriteLine((i == m) ? "Number is valid" : "Number is not valid");

The conditional operator is an operator. It returns a value. The value it returns is the value from one of its branches.

Console.WriteLine is a void method. It does not return a value. As a result, you cannot use it as one of the branches of the conditional operator.

BTW, this operator is correctly called "the conditional operator". It happens to be a ternary operator, meaning that it is an operator which takes three parameters. ere It runs:

  1. Unary
  2. Binary
  3. Ternary
  4. Quaternary

etc.

There happens to be only a single ternary operator in C# at present - the conditional operator. There happen to be no quaternary or higher-order operators.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!