tags:

views:

154

answers:

3
namespace ns
{
    class Class1
    {
        Nullable<int> a;
    }
}

will not compile: The type or namespace name 'Nullable' could not be found (are you missing a using directive or an assembly reference?) <-- missing 'using System;'

but,

namespace ns
{
    class Class1
    {
        int? a;
    }
}

will compile! (.Net 2)

do you know why?

+6  A: 

I believe that int? is an alias for

System.Nullable<Int32>
ChaosPandion
+8  A: 

The T? syntax is translated by the compiler into System.Nullable<T> by referencing the type directly, rather than by examining the usings that are in scope. You could similarly write this and the compiler would succeed:

System.Nullable<int> a;
rpetrich
+2  A: 

? is a language construct, while System.Nullable is a class - since it lives in the System namespace, you have to import it in the file (or more often, explicitly import it for the whole project as part of the project properties/configuration).

gregmac