Nullable type testing against null

I need to test if a Nullable<T> instance is null after it's been converted to an object. My current code fails using this:


using System;
using System.Collections.Generic;
using System.Text;

namespace TestNullable
{
class Program
{
staticvoid Main(string[] args )
{
System.Nullable<int> n =null;
object o = n;
if( o ==null )
{
Console.WriteLine( "IsNull" );
}
else
{
Console.WriteLine( "NotNull" );
}
}
}
}



What's best way to handle this considering I don't know what T is in advance of testing for null.

Regards
Lee

[1466 byte] By [LeeAlexander] at [2008-2-3]
# 1

I think this is a bug.
I already post this bug to microsoft feed back center

http://lab.msdn.microsoft.com/ProductFeedback/viewFeedback.aspx?FeedbackId=a50d1424-a6d3-469d-94b9-e1dcff94887c

EagleTsui at 2007-8-21 > top of Msdn Tech,Visual C#,Visual C# Language...
# 2
This is not a bug.

Even though you are assigning null to 'n', n still contains a Nullable<T> structure.

So when you assign 'n' to an object, you are actually boxing a Nullable structure and pointing 'o' to the boxed version. So when you compare this boxed version with null, you are saying 'is null equal to this object with something' and hence why this returns false.

DavidM.Kean at 2007-8-21 > top of Msdn Tech,Visual C#,Visual C# Language...
# 3

Oh just another thing, to convert a Nullable type to an object, so that comparision to null will work, use the following:



System.Nullable<int> n = null;

object o = Nullable.ToObject(n);

ToObject will return null if 'n' doesn't have a value, otherwise it returns its value.

Also, in C#, there is shorthand for using Nullable<T>:


int? n = null;

DavidM.Kean at 2007-8-21 > top of Msdn Tech,Visual C#,Visual C# Language...
# 4

Thanks for your replies but my problem is a little more complicated than the original post made out. I am actually using reflection to pull values out of an object. I am testing for null but PropertyInfo.GetValue and FieldInfo.GetObject return a boxed version of Nullable<T> which causes me problems. I want to know if there is an *easy* way of finding out this. The following is an updated example:


using System;
using System.Reflection;
using System.Collections.Generic;
using System.Text;

namespace TestNullable
{
class Program
{
public class TestNull
{
public Nullable<int> SomeInt = null;
}

static void Main( string[] args )
{
TestNull testNull = new TestNull();
FieldInfo fi = testNull.GetType().GetField( "SomeInt" );
object o = fi.GetValue( testNull );
if( o == null )
{
Console.WriteLine( "IsNull" );
}
else
{
Console.WriteLine( "NotNull" );
}
}
}
}

I figure I could drill into the type and find out if it's Nullable<T> and then call Nullable.ToObject on it...

Regards Lee

LeeAlexander at 2007-8-21 > top of Msdn Tech,Visual C#,Visual C# Language...
# 5
Yea, that's probably what you will have to do. I can't think of another way around this at the moment.
DavidM.Kean at 2007-8-21 > top of Msdn Tech,Visual C#,Visual C# Language...
# 6
I had to do something similar to this. First determine if the type is nullable or not. If it is then cast to a nullable type. However, I do not convert to object with ToObject. Why should I? There is a property on the nullable type HasValue() which tells you what you want to know. ToObject scares me, as it most likely is just the boxed value type.
JayC202 at 2007-8-21 > top of Msdn Tech,Visual C#,Visual C# Language...
# 7
Nullable Types has got 2 Usefull method
HasValue() and Value
HasValue returns true if it has a value...
DogaOztuzun at 2007-8-21 > top of Msdn Tech,Visual C#,Visual C# Language...
# 8
The non-generic Nullable class can help a lot here. You can try to Unwrap a boxed Nullable<T>, or determine if it is a Nullable<T> by calling GetUnderlyingType.
anfortas at 2007-8-21 > top of Msdn Tech,Visual C#,Visual C# Language...
# 9
Just an update on this issue:

This has been fixed and Boxed Nullable types are now null. You should see this in the August CTP and above.

DavidM.Kean at 2007-8-21 > top of Msdn Tech,Visual C#,Visual C# Language...
# 10
Good news! Thanks!
PeterNRoth at 2007-8-21 > top of Msdn Tech,Visual C#,Visual C# Language...