471,355 Members | 1,589 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,355 software developers and data experts.

Ternary Operator (?) Question

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
Jun 27 '08 #1
4 2268
On Jun 20, 5:31*pm, raiderdav <raider...@gmail.comwrote:
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<Tfor 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
Jun 27 '08 #2
On Jun 20, 11:45 am, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
? on its own at the end of a type name indicates a nullable type - T?
is the equivalent of System.Nullable<Tfor 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
Jun 27 '08 #3
raiderdav wrote:
On Jun 20, 11:45 am, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
>? on its own at the end of a type name indicates a nullable type - T?
is the equivalent of System.Nullable<Tfor 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

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.
Jun 27 '08 #4
raiderdav <ra*******@gmail.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by praba kar | last post: by
4 posts views Thread by Bob Gregory | last post: by
6 posts views Thread by glongword | last post: by
14 posts views Thread by Josh | last post: by
48 posts views Thread by Daniel Crespo | last post: by
15 posts views Thread by Arthur Dent | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.