Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nullable type as a generic parameter possible?

Tags:

c#

generics

I want to do something like this :

myYear = record.GetValueOrNull<int?>("myYear"), 

Notice the nullable type as the generic parameter.

Since the GetValueOrNull function could return null my first attempt was this:

public static T GetValueOrNull<T>(this DbDataRecord reader, string columnName)   where T : class {     object columnValue = reader[columnName];      if (!(columnValue is DBNull))     {         return (T)columnValue;     }     return null; } 

But the error I'm getting now is:

The type 'int?' must be a reference type in order to use it as parameter 'T' in the generic type or method

Right! Nullable<int> is a struct! So I tried changing the class constraint to a struct constraint (and as a side effect can't return null any more):

public static T GetValueOrNull<T>(this DbDataRecord reader, string columnName)   where T : struct 

Now the assignment:

myYear = record.GetValueOrNull<int?>("myYear"); 

Gives the following error:

The type 'int?' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method

Is specifying a nullable type as a generic parameter at all possible?

like image 346
Tom Pester Avatar asked Oct 16 '08 15:10

Tom Pester


People also ask

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 the purpose of a nullable type?

You typically use a nullable value type when you need to represent the undefined value of an underlying value type. For example, a Boolean, or bool , variable can only be either true or false .

Is nullable type reference type?

Nullable reference types aren't new class types, but rather annotations on existing reference types. The compiler uses those annotations to help you find potential null reference errors in your code. There's no runtime difference between a non-nullable reference type and a nullable reference type.

How do you make a variable Nullable in C#?

C# 2.0 introduced nullable types that allow you to assign null to value type variables. You can declare nullable types using Nullable<t> where T is a type. Nullable<int> i = null; A nullable type can represent the correct range of values for its underlying value type, plus an additional null value.


2 Answers

Change the return type to Nullable<T>, and call the method with the non nullable parameter

static void Main(string[] args) {     int? i = GetValueOrNull<int>(null, string.Empty); }   public static Nullable<T> GetValueOrNull<T>(DbDataRecord reader, string columnName) where T : struct {     object columnValue = reader[columnName];      if (!(columnValue is DBNull))         return (T)columnValue;      return null; } 
like image 173
Greg Dean Avatar answered Sep 25 '22 05:09

Greg Dean


public static T GetValueOrDefault<T>(this IDataRecord rdr, int index) {     object val = rdr[index];      if (!(val is DBNull))         return (T)val;      return default(T); } 

Just use it like this:

decimal? Quantity = rdr.GetValueOrDefault<decimal?>(1); string Unit = rdr.GetValueOrDefault<string>(2); 
like image 21
James Jones Avatar answered Sep 22 '22 05:09

James Jones