473,239 Members | 1,545 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,239 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 2348
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: praba kar | last post by:
Dear All, I am new to Python. I want to know how to work with ternary operator in Python. I cannot find any ternary operator in Python. So Kindly clear my doubt regarding this ...
4
by: Bob Gregory | last post by:
Hi all, I don't actually have a mac with which to test this, but I'm informed by a colleague that one of my scripts has failed in IE on the Mac; endless twiddling seems to point to the ternary...
24
by: gupta.keshav | last post by:
HI, Is there any situation which can be handled by ternary operator but not with if else blocks? Thanks Keshav
6
by: glongword | last post by:
As the assert macro should evaluate to a void expression, it should not have an 'if statement' in its definition. Does this necessitate the existence of a ternary conditional operator? Is there a...
6
by: Robert Zurer | last post by:
In his paper on Coding Standard found on http://www.idesign.net/idesign/DesktopDefault.aspx Juval Lowy discourages the use of the ternary conditional operator with no specific reason given. ...
48
by: Daniel Crespo | last post by:
Hi! I would like to know how can I do the PHP ternary operator/statement (... ? ... : ...) in Python... I want to something like: a = {'Huge': (quantity>90) ? True : False} Any...
15
by: Arthur Dent | last post by:
Hi all, im just curious if anyone knows..... With .NET 2, VB didnt happen to get a true ternary operator, did it? Stuck away in a corner somewhere? By ternary, i mean something like C's a?b:c...
5
by: PerlPhi | last post by:
hi,,, while ago i was wondering why do some programmers rarely uses the ternary operator. wherein it is less typing indeed. i believe in the classic virtue of Perl which is laziness. well let me show...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.