Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic typed function: return a non-nullable value

I wrote this trivial utility function:

public static T isNull<T>(T? v, T d)
{
    return v == null ? d : v.Value;
}

the purpose is to avoid annoying task like check for a member to be null, very common in reading a linq recordset. The problem is that it's throwing this error:

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

The error seems legit, anyway I wish I can do something like this:

int? myField = record.myField;
int  myValue = isNull(myField, 0);

instead of:

int? myField = record.myField;
int  myValue = myField == null ? 0 : myField.Value;

I feel like I'm missing some c# basic knowledge. Does exist a way to accomplish my task?

like image 214
balanza Avatar asked May 08 '26 21:05

balanza


2 Answers

Generic constraints can be used on generic functions to limit the types that are permitted to be used to certain subsets, and this opens up possibilities for how you might use those types inside your method or class.

In this case, you can apply a constraint to T that limits it to a struct to resolve your specific compiler error.

public static T IsNull<T>(T? v, T d) where T : struct
{
    return v == null ? d : v.Value;
}

However, another answer does correctly point out that you could opt to use the null coalescing operator ?? in this particular situation.

like image 128
Anthony Pegram Avatar answered May 11 '26 10:05

Anthony Pegram


This is called null coalescing, and there is a built in operator to do it:

int myValue = record.myField ?? 0
like image 45
Chris Pitman Avatar answered May 11 '26 12:05

Chris Pitman