且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

什么是C#中的整数引用类型?

更新时间:2022-11-24 13:17:01

有关信息, INT ? / 可空< T> 不会引用类型;它只是一个空类型,意思是:一个结构(主要是和 INT 布尔标记)具有特殊编译规则(重新null检查,运营商等)和CLI规则(装箱/拆箱)。有的.NET中没有整数引用类型,除非你计数拳击:

For info, int? / Nullable<T> is not a reference-type; it is simply a "nullable type", meaning: a struct (essentially and int and a bool flag) with special compiler rules (re null checks, operators, etc) and CLI rules (for boxing/unboxing). There is no "integer reference-type" in .NET, unless you count boxing:

int i = 123;
object o = i; // box



但是这创造了一个不必要的对象,有很多相关的其他问题。

but this creates an unnecessary object and has lots of associated other issues.

有关你想要什么, INT?应该是理想的。您的可能的使用长手语法(可空&LT; INT&GT; ),但IMO这是不必要的冗长,我已经看到了蛊惑人心。

For what you want, int? should be ideal. You could use the long-hand syntax (Nullable<int>) but IMO this is unnecessarily verbose, and I've seen it confuse people.