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

  • Home
  • SEARCH
  • 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 8540235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:33:03+00:00 2026-06-11T11:33:03+00:00

When I write Nullable<Nullable<DateTime>> test = null; I get a compilation error: The type

  • 0

When I write

Nullable<Nullable<DateTime>> test = null;

I get a compilation error:

The type 'System.Datetime?' must be a non-nullable value type in order to use it as a paramreter 'T' in the generic type or method 'System.Nullable<T>'

But Nullable<T> is a struct so it’s supposed to be non-nullable.

So I tried to create this struct:

public struct Foo<T> where T : struct
{
    private T value;

    public Foo(T value)
    {
        this.value = value;
    }

    public static explicit operator Foo<T>(T? value)
    {
        return new Foo<T>(value.Value);
    }

    public static implicit operator T?(Foo<T> value)
    {
        return new Nullable<T>(value.value);
    }
}

Now when I write

        Nullable<Foo<DateTime>> test1 = null;
        Foo<Nullable<DateTime>> test2 = null;
        Foo<DateTime> test3 = null;

The first line is ok but for the second and third lines I get the two following compilation error:

The type 'System.DateTime?' must be a non-nullable value type in order to use it as a parameter 'T' in the generic type or method 'MyProject.Foo<T>' (second line only)

and

Cannot convert null to 'MyProject.Foo<System.DateTime?> because it is a non-nullable value type'

        Foo<Nullable<DateTime>> test = new Foo<DateTime?>();

doesn’t work neither event if Nullable<DateTime> is a struct.

Conceptually, I can understand why Nullable<T> is nullable, it avoids having stuffs like DateTime?????????? however I can still have List<List<List<List<List<DateTime>>>>>…

So why this limitation and why can’t I reproduce this behavior in Foo<T>? Is this limitation enforced by the compiler or is it intrinsic in Nullable<T> code?

I read this question but it just says that it is not possible none of the answers say fundamentally why it’s not possible.

  • 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-11T11:33:05+00:00Added an answer on June 11, 2026 at 11:33 am

    But Nullable is a struct so it’s supposed to be non-nullable.

    Nullable<T> is indeed a struct, but the precise meaning of the generic struct constraint as stated in the docs is:

    The type argument must be a value type. Any value type except Nullable can be specified. See Using Nullable Types (C# Programming Guide) for more information.

    For the same reason, your line

    Foo<Nullable<DateTime>> test2 = null;
    

    results in the compiler error you are seeing, because your generic struct constraint restricts your generic T argument in a way so Nullable<DateTime> must not be specified as an actual argument.

    A rationale for this may have been to make calls such as

    Nullable<Nullable<DateTime>> test = null;
    

    less ambiguous: Does that mean you want to set test.HasValue to false, or do you actually want to set test.HasValue to true and test.Value.HasValue to false? With the given restriction to non-nullable type arguments, this confusion does not occur.

    Lastly, the null assignment works with Nullable<T> because – as implied by the selected answers and their comments to this SO question and this SO question – the Nullable<T> type is supported by some compiler magic.

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

Sidebar

Related Questions

I have a nullable DateTime Variable. And I want to write it to SQL
I want to use a custom aplified type (think Nullable) in a DataContract class.
My ERP database uses non-nullable datetime fields. However, it enters '' for the datetime
Can anyone explain why it is assumed that a non nullable type property should
Possible Duplicate: C# okay with comparing value types to null Why value type as
i am trying to write a function that will make DataRow[column] return nullable typed
I write game. I use RequestWindowFeature(WindowFeatures.NoTitle); at splash screen, but how can I disable
I'm trying to write an extension method that I can use to copy values
There must be a simple solution out there, I'm trying to use NHibernate Validator
I'm trying to write a route with a nullable int in it. It should

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.