1

Possible Duplicate:
Nullable type is not a nullable type?
How to check if an object is nullable?

See the codes below. Is there a way to get something like System.Nullable`1[System.Int32] by passing n as the argument?

static void Test()
{
  List<int> l = new List<int>();
  Nullable<int> n = new Nullable<int>( 1 );
  Console.WriteLine( l.GetType() ); // will output System.Collections.Generic.List`1[System.Int32]
  Console.WriteLine( n.GetType() ); // will output System.Int32. Why won't output something like System.Nullable`1[System.Int32] ?
}
Community
  • 1
  • 1
Setyo N
  • 1,953
  • 2
  • 26
  • 28
  • Nullables are boxed, so you can not really have a variable of type `Nullable` well you can but it'll never be that type if you ask it. – M.Babcock Apr 13 '12 at 01:43
  • 2
    See this post http://stackoverflow.com/questions/374651/how-to-check-if-an-object-is-nullable – Mike Parkhill Apr 13 '12 at 01:51
  • 2
    @M.Babcock the key points are that the nullable is boxed *when you call `GetType()` on it*, and that boxing a `Nullable` results in a *boxed `T`* (unless the `Nullable` has no value, of course, in which case, boxing it results in a null reference). – phoog Apr 13 '12 at 01:58
  • @phoog - Thanks for the clarification. Boxing in C# still confuses me a bit. If I bought something that _looks like_ a dog yet when I ask it what it is and it says it's a cat... which is it??? – M.Babcock Apr 13 '12 at 02:04
  • @M.Babcock [supercat's answer](http://stackoverflow.com/a/9983291/385844) to one of the duplicate questions discusses this fairly well. – phoog Apr 13 '12 at 02:12
  • @phoog - Ohhh, so it's a cat in a dog costume? – M.Babcock Apr 13 '12 at 02:13
  • @M.Babcock why yes, I never thought of it that way, but now that you mention it... – phoog Apr 13 '12 at 02:16
  • @phoog - It makes sense why a cat would dress up like a dog. Only witches like cats, and they only shop while the pet stores are closed. – M.Babcock Apr 13 '12 at 02:18

0 Answers0