Jonathan Wood
Jonathan Wood

Reputation: 67315

Why is the type Nullable<T> not detected?

Can anyone explain the following?

Code:

int i = 1;
int? j = 2;
Nullable<int> k = 2;
string s = "3";

Console.WriteLine(i.GetType().Name);
Console.WriteLine(j.GetType().Name);
Console.WriteLine(k.GetType().Name);
Console.WriteLine(s.GetType().Name);
Console.WriteLine();

Console.WriteLine(Nullable.GetUnderlyingType(i.GetType()));
Console.WriteLine(Nullable.GetUnderlyingType(j.GetType()));
Console.WriteLine(Nullable.GetUnderlyingType(k.GetType()));
Console.WriteLine(Nullable.GetUnderlyingType(s.GetType()));

Output:

Int32
Int32
Int32
String

null
null
null
null

Why does the type for j and k show as Int32 instead of Nullable<Int32>? And why does Nullable.GetUnderlyingType() return null for both variables?

Upvotes: -1

Views: 41

Answers (0)

Related Questions