什么是“int”?在 C# 类型中

pra*_*ddy 5 c# int types

我在考试中看到了这个代码示例问题,它完美地工作。

namespace Trials_1
{
    class Program
    {
        static void Main(string[] args)
        {
            int? a = 9;
            Console.Write("{0}", a);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但是下面的代码会抛出错误 CS0266。

namespace Trials_1
{
    class Program
    {
        static void Main(string[] args)
        {
            int? a = 9;
            int b = a;
            Console.Write("{0},{1}", a, b);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

有人可以详细解释我吗?

She*_*ock 3

这是 C#可空类型

可空类型表示可以分配 null 值的值类型变量。您无法基于引用类型创建可为 null 的类型。(引用类型已经支持 null 值。)

此行int b = a;会引发错误,因为您无法直接将int类型分配给nullable int类型。换句话说,int数据类型不能接受null值。