Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

T?, nullable type parameter in C# 9

This program has two errors:

using System;

T? f<T>(T? t)
{    
    t = null; //  Error CS0403  Cannot convert null to type parameter 'T' because it could be a non-nullable value type
    return default(T?);
}

if(f(10) is null) // Error  CS0037  Cannot convert null to 'int' because it is a non-nullable value type    
    Console.WriteLine("null");

T? has to be a nullable type. But it seems T? is the same as T in the above program.

Is ? ignored in T??


Edit: Both errors disappear with a struct constraint:

using System;

T? f<T>(T? t)  where T : struct
{    
    t = null; // error
    return default(T?);
}

if(f<int>(10) is null) // error
    Console.WriteLine("null");

I don't understand why the constraint changes the results.

like image 739
Minimus Heximus Avatar asked Jan 17 '21 20:01

Minimus Heximus


People also ask

What is nullable parameter?

Use two commas (,,) to give a parameter variable a null value when it is followed by other non-null parameters. After the last non-null parameter, all remaining parameter variables up to &31 are automatically given null values. Null parameters are useful when a value is not required.

What are Nullable type variables?

The Nullable type allows you to assign a null value to a variable. Nullable types introduced in C#2.0 can only work with Value Type, not with Reference Type. The nullable types for Reference Type is introduced later in C# 8.0 in 2019 so that we can explicitly define if a reference type can or can not hold a null value.

Can a generic type be null?

This means that you can put any object in a collection because all classes in the C# programming language extend from the object base class. Also, we cannot simply return null from a generic method like in normal method.

What is T type in C#?

T is called type parameter, which can be used as a type of fields, properties, method parameters, return types, and delegates in the DataStore class. For example, Data is generic property because we have used a type parameter T as its type instead of the specific data type. Note.


Video Answer


2 Answers

When you say T? in T? and in (T? t), they both refer to nullable reference types, not to the special Nullable<T> struct. There's no way that you can specify a generic parameter such that you can treat it as a class and a nullable value type.

The second error is just because f(10) (so f<int>(10)) is implicitly taken as an int (as there's no such thing as a nullable reference int value), so null isn't valid, just as if you did if (10 is null).


If T stops being open and instead you add a constraint such as where T : struct, T? becomes System.Nullable<T> rather than a nullable reference parameter, and so the code becomes the exact same as before nullable reference types were introduced.

like image 93
Camilo Terevinto Avatar answered Oct 06 '22 10:10

Camilo Terevinto


I don't have an idea for the first error, but the second.

f(10) is null is inferred as int in lieu of int? since 10 is of int type. Either f((int?)10) is null, or f<int?>(10) is null should be used.

like image 43
snr Avatar answered Oct 06 '22 10:10

snr