0

why can't I have something like:

Some_Function ? myList.Add(a) : throw new Exception();

Why can't I throw exception in else part of ?: operator? what is the main purpose of ?: operator?

Suggestion If anybody else wondered about the same thing, beside reading the answers below, i suggest that you read this post as well.. Expression Versus Statement

Community
  • 1
  • 1

4 Answers4

6

The other answers and comments are of course true (we can refer to the documentation), though my understanding of the question is more like, "why does it have to be that way?".

According to the C# 5.0 specification, the conditional operator forms an expression, not a statement. I suspect that the reason why it's not a statement, thus preventing you from doing something like a ? b() : throw e is simply because we already have a statement construct for achieving basically the same thing; namely, if..else.

if (a) { b(); } else { throw e; }

The benefit of the conditional operator is that it can be used within statements or other expressions.

bool? nb = GetValue();
if (nb ?? (a ? b() : c())) { throw e; }
Dave Sexton
  • 2,562
  • 1
  • 17
  • 26
3

Because it is defined as:

null-coalescing-expression ? expression : expression

As you can read in ?: Operator (C# Reference):

The conditional operator (?:) returns one of two values depending on the value of a Boolean expression.

One of both expression is evaluated depending on the boolean value of null-coalescing-expression.

Since neither of your two code fragments are expressions that evaluate to a value (List.Add() is void and an exception doesn't evaluate to a value either), this can't compile.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
2

You need to remember that Ternary operator must return something and List.Add is a void method so it fails. Both the sides must be compatible and should return something.

The MSDN says:

The condition must evaluate to true or false. If condition is true, first_expression is evaluated and becomes the result. If condition is false, second_expression is evaluated and becomes the result.

And myList.Add(a) is not an expression.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1

See ?: Operator on MSDN. Its clearly explained there.

The conditional operator (?:) returns one of two values depending on the value of a Boolean expression. Following is the syntax for the conditional operator.

condition ? first_expression : second_expression;

The condition must evaluate to true or false. If condition is true, first_expression is evaluated and becomes the result. If condition is false, second_expression is evaluated and becomes the result. Only one of the two expressions is evaluated. Either the type of first_expression and second_expression must be the same, or an implicit conversion must exist from one type to the other. You can express calculations that might otherwise require an if-else construction more concisely by using the conditional operator. For example, the following code uses first an if statement and then a conditional operator to classify an integer as positive or negative.

The conditional operator (?:) returns one of two values depending on the value of a Boolean expression. Following is the syntax for the conditional operator.

The condition must evaluate to true or false. If condition is true, first_expression is evaluated and becomes the result. If condition is false, second_expression is evaluated and becomes the result. Only one of the two expressions is evaluated. Either the type of first_expression and second_expression must be the same, or an implicit conversion must exist from one type to the other. You can express calculations that might otherwise require an if-else construction more concisely by using the conditional operator. For example, the following code uses first an if statement and then a conditional operator to classify an integer as positive or negative.

Display name
  • 325
  • 4
  • 16