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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:47:48+00:00 2026-05-28T16:47:48+00:00

I was just looking at this answer , which contains the code for Nullable<T>

  • 0

I was just looking at this answer, which contains the code for Nullable<T> from .NET Reflector, and I noticed two things:

  1. An explicit conversion is required when going from Nullable<T> to T.
  2. The == operator is not defined.

Given these two facts, it surprises me that this compiles:

int? value = 10;
Assert.IsTrue(value == 10);

With the code value == 10, either value is being magically converted to an int (hence allowing int‘s == operator to be used, or the == operator is being magically defined for Nullable<int>. (Or, I presume less likely, Reflector is leaving out some of the code.)

I would expect to have to do one of the following:

Assert.IsTrue((value.Equals(10)); // works because Equals *is* defined
Assert.IsTrue(value.Value == 10); // works because == is defined for int
Assert.IsTrue((int?)value == 10); // works because of the explicit conversion

These of course work, but == also works, and that’s the part I don’t get.

The reason I noticed this and am asking this question is that I’m trying to write a struct that works somewhat similarly to Nullable<T>. I began with the Reflector code linked above, and just made some very minor modifications. Unfortunately, my CustomNullable<T> doesn’t work the same way. I am not able to do Assert.IsTrue(value == 10). I get “Operator == cannot be applied to operands of type CustomNullable<int> and int“.

Now, no matter how minor the modification, I would not expect to be able to do…

CustomNullable<T> value = null;

…because I understand that there is some compiler magic behind Nullable<T> that allows values to be set to null even though Nullable<T> is a struct, but I would expect I should be able to mimic all the other behaviors of Nullable<T> if my code is written (almost) identically.

Can anyone shed light on how the various operators of Nullable<T> work when they appear not to be defined?

  • 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-28T16:47:49+00:00Added an answer on May 28, 2026 at 4:47 pm

    Given these two facts, it surprises me that this compiles

    Given only those two facts, that is surprising.

    Here’s a third fact: in C#, most operators are ‘lifted to nullable’.

    By “lifted to nullable”, I mean that if you say:

    int? x = 1;
    int? y = 2;
    int? z = x + y;
    

    then you get the semantics of “if either x or y is null then z is null. If both are not null then add their values, convert to nullable, and assign the result to z.”

    The same goes for equality, though equality is a bit weird because in C#, equality is still only two-valued. To be properly lifted, equality ought to be three-valued: x == y should be null if either x or y is null, and true or false if x and y are both non-null. That’s how it works in VB, but not in C#.

    I would expect I should be able to mimic all the other behaviors of Nullable<T> if my code is written (almost) identically.

    You are going to have to learn to live with disappointment because your expectation is completely out of line with reality. Nullable<T> is a very special type and its magical properties are embedded deeply within the C# language and the runtime. For example:

    • C# automatically lifts operators to nullable. There’s no way to say “automatically lift operators to MyNullable”. You can get pretty close by writing your own user-defined operators though.

    • C# has special rules for null literals — you can assign them to nullable variables, and compare them to nullable values, and the compiler generates special code for them.

    • The boxing semantics of nullables are deeply weird and baked into the runtime. There is no way to emulate them.

    • Nullable semantics for the is, as and coalescing operators are baked in to the language.

    • Nullables do not satisfy the struct constraint. There is no way to emulate that.

    • And so on.

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

Sidebar

Related Questions

I was just looking at this code and i don't understand what RETURN TRUE
I just finished looking at this question: https://stackoverflow.com/questions/753122/which-cloud-computing-platform-should-i-choose But, I am not certain what
I was just looking at the jQueryUI button plug-in and noticed this $(button, input:submit,
Just looking at: (Source: https://xkcd.com/327/ ) What does this SQL do: Robert'); DROP TABLE
I'm just looking for suggestions on the best way to do this... I need
Does anyone have some numbers on this? I am just looking for a percentage,
How would you test this scenario? I've just started looking into NHibernate and having
Is it possible to retrieve just one photo from picasa. I am looking around
I'm very much just looking for feedback and guidance rather than a direct answer.
so I have been looking for this answer and the devise docs kind of

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.