Ternary Operator (?) Question

R

raiderdav

I understand how the ternary operator (question mark - ?) works in an
if/else setting, but what does it mean when used as a type?

For instance, I'm trying to add a get/set in some existing code, but
the return type doesn't match:

Rectangle? m_textArea = null;
public Rectangle TextArea {
set { m_textArea = value; }
get { return m_textArea; }
}

I get a "can't convert Rectangle? to Rectangle" error on build. What
does the ? operator do when added to a variable declaration like this?

Thanks - d
 
J

Jon Skeet [C# MVP]

I understand how the ternary operator (question mark - ?) works in an
if/else setting, but what does it mean when used as a type?

(Point of pedantry: ?: is *a* ternary operator, and happens to be the
only one, but its name is the conditional operator - it's not
impossible that another one could be added at some point. Note that it
includes the colon as well.)

? on its own at the end of a type name indicates a nullable type - T?
is the equivalent of System.Nullable<T> for any type name T. This is
unrelated to the conditional operator.

See http://pobox.com/~skeet/csharp/csharp2/nullable.html for more
information on nullable types.

Jon
 
R

raiderdav

? on its own at the end of a type name indicates a nullable type - T?
is the equivalent of System.Nullable<T> for any type name T. This is
unrelated to the conditional operator.

Seehttp://pobox.com/~skeet/csharp/csharp2/nullable.htmlfor more
information on nullable types.

Jon

That makes perfect sense then - thanks for the terminology lesson. No
wonder I wasn't finding anything about this nullable type under the
conditional operators. I was thinking it was an 'if this variable
doesn't exist, then declare it' or something.

MSDN should have a note about this on the ?: page linking to the
nullable type since they both use the ? and are completely unrelated.

Thanks Jon-
d
 
F

Fernando Gómez

raiderdav said:
That makes perfect sense then - thanks for the terminology lesson. No
wonder I wasn't finding anything about this nullable type under the
conditional operators. I was thinking it was an 'if this variable
doesn't exist, then declare it' or something.

MSDN should have a note about this on the ?: page linking to the
nullable type since they both use the ? and are completely unrelated.

Thanks Jon-
d


Just for the record, there is also the operator ??, as:

variable = <value1> ?? <value2>;

The operator will return value1 if it is not null, otherwise it'll
return value2.

public void foo(string str)
{
Console.WriteLine(str ?? "There's no message");
}

and so on.
 
J

Jon Skeet [C# MVP]

raiderdav said:
That makes perfect sense then - thanks for the terminology lesson.

Sorry if it was a bit heavy-handed - I'm a terminology pedant. It makes
me seem grumpy and belligerrent, but it helps for communication in the
long run :)
No wonder I wasn't finding anything about this nullable type under
the conditional operators. I was thinking it was an 'if this variable
doesn't exist, then declare it' or something.

Right. I can see why that would be confusing :)
MSDN should have a note about this on the ?: page linking to the
nullable type since they both use the ? and are completely unrelated.

That's a really good idea. I suggest you raise an item on
connect.microsoft.com about it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top