什么是C#中的整数引用类型? [英] What is the integer reference type in C#?

查看:141
本文介绍了什么是C#中的整数引用类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有它可以设置为null,不希望有使用 INT整数变量? MYVARIABLE 语法。我试着用 INT 的Int16 无济于事。我一定要使用诠释? MYVARIABLE

I'd like to have an integer variable which can be set to null and don't want to have to use the int? myVariable syntax. I tried using int and Int16 to no avail. Do I have to use int? myVariable?

我因为在Java提到这同时存在一个'廉政'类型(基本)和整数(引用类型)。我想,以确保没有,我可以使用内置的整数引用类型。我会用'诠释?对于我在做什么。

I mentioned this because in Java there is both an 'int' type (a primitive) and 'Integer' (a reference type). I wanted to be sure that there isn't a built-in integer reference type that I could be using. I'll use 'int?' for what I'm doing.

推荐答案

有关信息, 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?应该是理想的。您的可能的使用长手语法(可空< INT> ),但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.

这篇关于什么是C#中的整数引用类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆