Chaining Conditional Operator in C#

What is conditional operator?

A conditional operator which is a ternary operator; takes the condition as first part, the second part is the value to be returned if first part is true and finally the third part value is returned if the first part value is false.

In the following example, the file is checked for extensions and if it is true the file is a valid image. Notice that true is returned for the following example.



You might feel chaining of the conditional operator difficult to understand, but for some developers its short hand code for evaluation logic. The third part of the conditional operator statement could be extended to achieve the chaining.

In the following example, the 2nd conditional statement is chained.


And the file with extension png is checked which is returning a true value for the chained conditional statement.


Hope this post is useful.

Thanks for reading. Like Method Of Tried in Facebook.