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 778323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:44:46+00:00 2026-05-14T19:44:46+00:00

I am trying to use the conditional operator, but I am getting hung up

  • 0

I am trying to use the conditional operator, but I am getting hung up on the type it thinks the result should be.

Below is an example that I have contrived to show the issue I am having:

class Program
{
    public static void OutputDateTime(DateTime? datetime)
    {
        Console.WriteLine(datetime);
    }

    public static bool IsDateTimeHappy(DateTime datetime)
    {
        if (DateTime.Compare(datetime, DateTime.Parse("1/1")) == 0)
            return true;

        return false;
    }

    static void Main(string[] args)
    {
        DateTime myDateTime = DateTime.Now;
        OutputDateTime(IsDateTimeHappy(myDateTime) ? null : myDateTime);
        Console.ReadLine();                        ^
    }                                              |
}                                                  |
// This line has the compile issue  ---------------+

On the line indicated above, I get the following compile error:

Type of conditional expression cannot be determined because there is no implicit conversion between ‘< null >’ and ‘System.DateTime’

I am confused because the parameter is a nullable type (DateTime?). Why does it need to convert at all? If it is null then use that, if it is a date time then use that.

I was under the impression that:

condition ? first_expression : second_expression;

was the same as:

if (condition)
   first_expression;
else
   second_expression;

Clearly this is not the case. What is the reasoning behind this?

(NOTE: I know that if I make “myDateTime” a nullable DateTime then it will work. But why does it need it?

As I stated earlier this is a contrived example. In my real example “myDateTime” is a data mapped value that cannot be made nullable.)

  • 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-05-14T19:44:46+00:00Added an answer on May 14, 2026 at 7:44 pm

    The compiler does not infer the type of the result of the conditional operator from the usage of the result, but from the types of its arguments. The compiler fails when it sees this expression because it cannot deduce the type of the result:

    IsDateTimeHappy(myDateTime) ? null : myDateTime;
    

    Since null and DateTime are not compatible, you need to tell the compiler what the type should be. A cast should do the trick:

    DateTime? x = IsDateTimeHappy(myDateTime) ? (DateTime?)null : myDateTime;
    OutputDateTime(x);
    

    Now the compiler will have no problems. You can also write the above on one line if you prefer (but I would probably not do this):

    OutputDateTime(IsDateTimeHappy(myDateTime) ? (DateTime?)null : myDateTime);
    

    Eric Lippert has a good answer that is also relevant here and goes into more details about how the compiler determines types.

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

Sidebar

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.