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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:08:26+00:00 2026-05-31T15:08:26+00:00

When C# compares a nullable type to a null, does the C# compiler insert

  • 0

When C# compares a nullable type to a “null”, does the C# compiler insert some code to check if the variable has a value first?

int? fk2=null;
OtherNullable<int> fk3=null;

Console.WriteLine(fk2 == null);

Console.WriteLine(fk3 == null);

Context, I’m trying to work out what the equality + implicit, and explicit cast should look like to enable the comparison of fk3 and null.

Say that you implemented Nullable as per the code posted here: How does a Nullable<T> type work behind the scenes? and called it OtherNullable<T>

This works fine with little modification, except that val == null will not work at runtime.

Implementation:

[Serializable, StructLayout(LayoutKind.Sequential)]
public struct OtherNullable<T> : IEquatable<OtherNullable<T>> where T : struct {

    private bool hasValue;
    internal T value;

    public OtherNullable(T value)
        : this() {
        this.value = value;
        this.hasValue = true;
    }

    public OtherNullable(T? value) {
        this.value = default(T);
        this.hasValue = false;
    }

    public bool HasValue {
        get { return this.hasValue; }
    }

    public T Value {
        get {
            if (!this.HasValue) {
                throw new InvalidOperationException("No Value");
            }
            return this.value;
        }
    }

    public OtherNullable(bool b) {
        this.value = default(T);
        this.hasValue = false;
    }

    public T GetValueOrDefault() {
        return this.value;
    }

    public T GetValueOrDefault(T defaultValue) {
        if (!this.HasValue) {
            return defaultValue;
        }
        return this.value;
    }

    public bool Equals(OtherNullable<T> other)
    {
        if (!other.HasValue & !this.HasValue)
            return true;
        else
            return other.hasValue.Equals(hasValue) && other.value.Equals(value);
    }

    public static bool operator ==(OtherNullable<T> left, OtherNullable<T> right) {
        return left.Equals(right);
    }

    public static bool operator !=(OtherNullable<T> left, OtherNullable<T> right) {
        return !left.Equals(right);
    }

    public override bool Equals(object other) {
        if (ReferenceEquals(null, other)) return false;
        if (other.GetType() != typeof(OtherNullable<T>)) return false;
        return Equals((OtherNullable<T>)other);
    }

    public override int GetHashCode() {
        unchecked {
            return (hasValue.GetHashCode() * 397) ^ value.GetHashCode();
        }
    }

    public override string ToString() {
        if (!this.HasValue) {
            return "";
        }
        return this.value.ToString();
    }

    public static implicit operator OtherNullable<T>(T value) {
        return new OtherNullable<T>(value);
    }

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

    public static implicit operator OtherNullable<T>(Nullable<T> value) {
        if (value.HasValue) {
            return new OtherNullable<T>(value.Value);
        } else {
            return new OtherNullable<T>(null);
        }
    }

}

Test Code (will not compile — it was previously):

[TestFixture]
public class TestOtherNullable {
    [Test]
    public void Test()
    {
        var a = new OtherNullable<int>();

        a = 1;

        Assert.IsTrue(a.HasValue);
        Assert.IsFalse(a == null);

        var b = new OtherNullable<int>();
        b = null;
        Assert.IsTrue(b == null);
    }
}
  • 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-31T15:08:27+00:00Added an answer on May 31, 2026 at 3:08 pm

    The equality check of any Nullable<T> to null is equivalent to !nullable.HasValue. There are no other semantics for it that would make sense. I ‘m not sure what the deal with OtherNullable<T> is, since that’s not a standard type.

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

Sidebar

Related Questions

I understand that using the === compares type, so running the following code results
I have the following function which compares the first column value in two 2D
Possible Duplicate: C# okay with comparing value types to null Why value type as
I am writing a code which compares file1 (single column of entries) with file
This sample code compares serial method with threaded method, on Quad core processor. The
I have a question on how to determine an object's Nullable property type. ObjectA
The following code compares two XML texts and returns a collection of data changes
I have a SQL query that compares a value in the database to a
Is there a built-in IEqualityComparer that compares objects by the value returned by their
I'm trying to compare two variables of type nullable(of boolean) in VB.NET 2010. One

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.