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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:54:58+00:00 2026-06-01T20:54:58+00:00

I would expect the next three lines of code to be the same: public

  • 0

I would expect the next three lines of code to be the same:

public static void TestVarCoalescing(DateTime? nullableDateTime)
{
  var dateTimeNullable1 = nullableDateTime.HasValue ? nullableDateTime : DateTime.Now;
  var dateTimeNullable2 = nullableDateTime != null ? nullableDateTime : DateTime.Now;
  var dateTimeWhatType = nullableDateTime ?? DateTime.Now;
}

In all cases, I assign nullableDateTime to the new variable. I would expect the type of all variables to become DateTime? since that is the type of nullableDateTime. But to my surprise, the type of dateTimeWhatType just becomes DateTime, so not nullable.

To make things worse, ReSharper suggests to replace the second statement with a null coalescing expression, turning it into expression 3. So if I let ReSharper do its thing, the type of the variable will change from DateTime? to DateTime.

In fact, let’s say that in the remainder of the method, I would use

if (someCondition) dateTimeNullable2 = null;

That would compile just fine, until I let ReSharper replace the second expression with the null coalescing version.

AFAIK, replacing

somevar != null ? somevar : somedefault;

with

somevar ?? somedefault;

should indeed produce the same result. But for implicit typing on a nullable type, the compiler seems to threat ?? as if it means.

somevar != null ? somevar.Value : somedefault;

So I guess my question is why the implicit type is changed when I use ??, and also where in the documentation I could find info on this.

BTW, this isn’t a real world scenario, but I would like to know why using ?? changes the (implicit) type.

  • 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-01T20:55:00+00:00Added an answer on June 1, 2026 at 8:55 pm

    Your first two examples are leading you astray; better would be to consider not your

    var dateTimeNullable1 = nullableDateTime.HasValue 
        ? nullableDateTime 
        : DateTime.Now;
    

    but rather

    var dateTimeNullable1 = nullableDateTime.HasValue 
        ? nullableDateTime.Value 
        : DateTime.Now;
    

    To quote section 7.12 “The null coalescing operator” of the C# 3.0 spec (apologies for slightly ropey formatting):

    The type of the expression a ?? b depends on which implicit
    conversions are available between the types of the operands. In order
    of preference, the type of a ?? b is A0, A, or B,
    where A is the type of a, B is the type of b (provided that
    b has a type), and A0 is the underlying type of A if
    A is a nullable type, or A otherwise.

    So if a is Nullable<Something>, and b can be implicitly converted to Something, the type of the whole expression will be Something. As @Damien_The_Unbeliever suggests, the point of this operator is to coalesce nulls!

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

Sidebar

Related Questions

I would expect the following code snippet to complain about trying to assign something
I would expect a Dictionary object of the form: var dict = new Dictionary<string,string>()
I would expect the following code to start my slider at '30' in it's
I have the following code (ObjA) and it works as I would expect, instance
the following code is not behaving like I would expect. Please help me understand
The following code prints (when invoking MyMethod): 0 0 0 1 I would expect
I would expect subclass always call super() for some method. How can I enforce
One would expect that even though strings are immutable, value-equality and reference-equality would not
As you would expect from a DSL aimed at data analysis, R handles missing/incomplete
Python is doing string multiplication where I would expect it to do numeric multiplication,

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.