Why Ternary Operator Doesnt Support Nullable Types(jebarson.info)

submitted by jebarson007jebarson007(150) 6 months, 3 days ago

Nullable types and ternary operators are around for many years now and may be not everybody know that ternary operator doesn't support assigning null values to nullable types.

6 comments |category: |Views: 12

tags: another

new Add a live kick counter to your blog >> liveImage

You can even customize the image by choosing your own colors, and then clicking the button below to update the preview and the html code:

  • "Kick It" text
  • "Kick It" background
  • kick count text
  • kick count background
  • border

Simply copy and paste this HTML into your blog post.


Users who kicked this story:
Comments:

posted by dpetersondpeterson(4397) 6 months, 2 days ago 0

The article is called "Why Ternary Operator Doesnt Support Nullable Types", but you never actually tell us why nullable types aren't supported, just that it's "by design".

Reply

posted by jebarson007jebarson007(150) replied to dpetersondpeterson(4397), 6 months, 2 days ago 0

I have mentioned the reason that its because the types are not implicitly convertible. if you read my article i have mentioned that because you cant convert a null to double or vice versa. The operator expects both the value (in true and false part) to be the same type or convertible types

Reply

posted by dpetersondpeterson(4397) replied to jebarson007jebarson007(150), 6 months, 2 days ago 0

Yes, that's the error the compiler throws. But that doesn't explain what design decision drove the team at MS to throw that error when attempting to assign null using a ternary operator, even though the type being assigned to is nullable. You could just as easily be using object types instead of reference types and your example of double->null.

It's not a personal attack, I'm just pointing out that beyond talking about the compiler error, and that the team made a design decision to disallow it, you didn't go into the reasons why the decision was made.

Reply

posted by jebarson007jebarson007(150) replied to dpetersondpeterson(4397), 6 months, 2 days ago 0

Yes I do understand your question here. May be I should have explained more on the later part of my article.

So if you can understand, the ternary operators where available before the nullable types. So when the ternary operator was designed, it was designed to accept the values (on true and false) to implicitly convertible values. So now the team is however considering to support the same in the future version.

No offense taken :)

Reply

posted by vijaystvijayst(1311) 5 months, 29 days ago 0

Interesting limitation that I was not aware of. I usually use something like this: double? num = true ? (double?)100 : null;

Reply

posted by threenine39threenine39(329) 5 months, 14 days ago 0

Poosibly and interesting debate here! Some would argue that Ternary Ornary operators are the devils work though. I personally like them.

From a maintenance and debugging view I would prefer to see the more explicit definition provided in the work around. However I do know some people who would much rather see an explicit If.. Then statements.

Reply

information Login or create an account to comment on this story