简体   繁体   English

int是值类型还是引用类型?

[英]Is int a value type or reference type?

Why do I hear all the time that int is a reference type, but I see that int is a struct . 为什么我总是听到int是引用类型,但是却看到int是struct And struct is a value type. struct是一个值类型。 Thanks. 谢谢。

Integers are Value types . 整数是值类型

Main Features of Value Types 值类型的主要特征

Variables that are based on value types directly contain values. 基于值类型的变量直接包含值。 Assigning one value type variable to another copies the contained value. 将一个值类型变量分配给另一值将复制包含的值。 This differs from the assignment of reference type variables, which copies a reference to the object but not the object itself. 这与引用类型变量的分配不同,引用类型变量的分配将引用复制到对象,但不复制对象本身。

All value types are derived implicitly from the System.ValueType. 所有值类型都是从System.ValueType隐式派生的。

Unlike with reference types, you cannot derive a new type from a value type. 与引用类型不同,您不能从值类型派生新类型。 However, like reference types, structs can implement interfaces. 但是,像引用类型一样,结构可以实现接口。

Unlike reference types, a value type cannot contain the null value. 与引用类型不同,值类型不能包含空值。 However, the nullable types feature does allow for value types to be assigned to null. 但是,可为空的类型功能确实允许将值类型分配为null。

Each value type has an implicit default constructor that initializes the default value of that type. 每个值类型都有一个隐式默认构造函数,用于初始化该类型的默认值。 For information about default values of value types, see Default Values Table. 有关值类型的默认值的信息,请参见默认值表。

Int is certainly not a reference type in C#. 在C#中,int绝对不是引用类型。 It's a numerical struct, which is a value type. 这是一个数字结构,它是一个值类型。 When talking about C#, it is incorrect to say int is a reference type. 在谈论C#时,说int是引用类型是不正确的。

An int is most definitely a value type. int绝对是一种值类型。 However, in some cases it can be boxed (see this article for an explanation) into a reference type. 但是,在某些情况下,可以将其装箱成引用类型(请参阅本文以获取解释)。

int is a value type. int是一种值类型。 Alternatively, you can use Nullable<int> if you need. 或者,可以根据需要使用Nullable<int>

int是值类型,对象是引用类型,但默认类型是value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM