12

In C# 8.0 we can have nullable reference types. The docs state that there are 4 types of nullability. The first 3 are quite clear but I fail to understand the point of "unknown". The docs say it is used with generics but when I try to call a method on an unconstrained variable of T type in generics it just warns as if the type is nullable. I fail to see the difference between unknown and nullable. Why does unknown exist? How does it manifest itself?

0

1 Answer 1

12

Take the following generic method:

public static T Get<T>(T value)
{
    return value;
}

If we call it like Get<string>(s), the return is non-nullable, and if we do Get<string?>(s), it's nullable.

However if you are calling it with a generic argument like Get<T>(x) and T isn't resolved, for example it is a generic argument to your generic class like below...

class MyClass<T>
{
    void Method(T x)
    {
        var result = Get<T>(x);
        // is result nullable or non-nullable? It depends on T
    }
}

Here the compiler does not know if eventually it will be called with a nullable or non-nullable type.

There is a new type constraint we can use to signal that T cannot be null:

public static T Get<T>(T value) where T: notnull
{
    return value;
}

However, where T is unconstrained and still open, the nullability is unknown.

If these unknowns were treated as nullable then you could write the following code:

class MyClass<T>
{
    void Method(T x)
    {
        var result = Get<T>(x);
        // reassign result to null, cause we we could if unknown was treated as nullable
        result = null;
    }
}

In the case where T was not nullable, we should have got a warning. So with unknown nullability types, we want warnings when dereferencing, but also warnings for assigning potentially null.

5
  • When I do var result = Test.Get<T>(x); result.ToString(); the compiler complains about dereferencing a possibly null value. I don't see how unknown is different from simply nullable in this case.
    – Stilgar
    Nov 25, 2019 at 0:14
  • 1
    In terms of warnings they behave the same, but they are semantically different. You could say the difference is academic, and if that was your point then I agree.
    – Stuart
    Nov 25, 2019 at 0:42
  • 1
    I would still like to know why the difference was introduced. It seems strange to introduce such a distinction in the language for academic purposes.
    – Stilgar
    Nov 25, 2019 at 0:48
  • My bad, just re-read the spec, update the answer, the last part explains it.
    – Stuart
    Nov 25, 2019 at 0:51
  • 1
    Ah that's more like it
    – Stilgar
    Nov 25, 2019 at 1:19

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.