Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7825065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:49:22+00:00 2026-06-02T08:49:22+00:00

I was just wondering why the following code doesn’t work (please keep in mind

  • 0

I was just wondering why the following code doesn’t work (please keep in mind that I set age to be nullable):

myEmployee.age = conditionMet ? someNumber : null;

Yet the following works fine:

if(conditionMet)
{
    myEmployee.age = someNumber;
}
else
{
    myEmployee.age = null;
}

Why can’t I set the value to null in a conditional operator?? All these if statements in my code is not nice.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T08:49:27+00:00Added an answer on June 2, 2026 at 8:49 am

    The types on both sides have to be the same (or be implicitly convertible):

    myEmployee.age = conditionMet ? someNumber : (int?)null;
    

    From the docs:

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was just wondering why my following code doesn't work: EDIT: The following code
Just wondering, would the following code arrangement cause any issues when calling mysql_connect, i.e.:
I'm wondering if this code ... StringBuilder sb = new StringBuilder(Please read the following
Just wondering if the following is considered to be good programming practice or not?
Just wondering how the following sql query would look in linq for Entity Framework...
Just wondering if the following expressions used for Value are equivalent (the column Contrib
Just wondering how do I mimic the following using attributes... <%= Html.EditorFor(x => x.SportProgramIdList,
Just wondering if there was a nice (already implemented/documented) algorithm to do the following
I am using the following which works great. I am just wondering if it
I am wondering if there is any potential security risk from the following code.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.