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
. And struct
is a value type. 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.
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.
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#. It's a numerical struct, which is a value type. When talking about C#, it is incorrect to say int is a reference type.
An int
is most definitely a value type. However, in some cases it can be boxed (see this article for an explanation) into a reference type.
int
is a value type. Alternatively, you can use Nullable<int>
if you need.
int is a value type, objects are reference types but the default types are value
精彩评论