Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Nullable<int> can't compile but int? does without 'using System;'

In .Net 2, the code:

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

does not compile and gives the error:

The type or namespace name 'Nullable' could not be found (are you missing a using directive or an assembly reference?)

It is missing using System; but this code:

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

compiles.

Can somebody explain why?

like image 234
manji Avatar asked Dec 01 '22 11:12

manji


1 Answers

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;
like image 131
rpetrich Avatar answered Dec 06 '22 20:12

rpetrich