值类型和原始类型之间有什么区别?

时间:2022-03-24 16:27:33

Reading a book about C# i noticed that sometimes is mentioned value type and sometimes primitive type for some data type (e.g. int, double) . I thought they were the same thing, but they are really the same or not ?

读一本关于C#的书我注意到有时会提到一些数据类型的值类型和有时是原始类型(例如int,double)。我以为它们是同一个东西,但它们真的相同或不相同?

What is the difference between a value type and a primitive type ? Are they the same thing ?

值类型和基本类型有什么区别?它们是一样的吗 ?

EDIT

The question is not only related to C# programming Language, i was wondering how them are different even in any other Language ...

问题不仅与C#编程语言有关,我还想知道它们在任何其他语言中是如何不同的......

3 个解决方案

#1


5  

A value type is usually whatever type reside on the Stack .

值类型通常是堆栈上驻留的任何类型。

A primitive type is a type defined at the programming language level, often it is even a value type, directly supported by the compiler of the language.

基本类型是在编程语言级别定义的类型,通常它甚至是值类型,由语言的编译器直接支持。

However this is a summary general answer because each programming language have different set of differences between the two types ...

然而,这是一个总结性答案,因为每种编程语言在两种类型之间有不同的差异集......

#2


4  

A primitive type (e.g. int) can be mapped directly to a Base Class Library (BCL) type (e.g. System.Int32)

原始类型(例如int)可以直接映射到基类库(BCL)类型(例如System.Int32)

A value type inherits from System.ValueType and is passed by value (among other properties).

值类型继承自System.ValueType,并通过值(以及其他属性)传递。

They are not interchangeable as object (System.Object) is a primitive type but not a value type, and structs are value types but not primitive.

它们不可互换,因为对象(System.Object)是基本类型而不是值类型,结构是值类型但不是原始类型。

See more differences here

在这里查看更多差异

#3


0  

No they are not. A value type is copied and while a reference type is being referenced. See the image值类型和原始类型之间有什么区别?

不,他们不是。复制值类型,同时引用引用类型。看图像

Edit My mistake. Here's an tutorial for the differences: http://www.codeproject.com/Articles/11212/Primitive-Reference-and-Value-Types

编辑我的错误。以下是差异教程:http://www.codeproject.com/Articles/11212/Primitive-Reference-and-Value-Types

#1


5  

A value type is usually whatever type reside on the Stack .

值类型通常是堆栈上驻留的任何类型。

A primitive type is a type defined at the programming language level, often it is even a value type, directly supported by the compiler of the language.

基本类型是在编程语言级别定义的类型,通常它甚至是值类型,由语言的编译器直接支持。

However this is a summary general answer because each programming language have different set of differences between the two types ...

然而,这是一个总结性答案,因为每种编程语言在两种类型之间有不同的差异集......

#2


4  

A primitive type (e.g. int) can be mapped directly to a Base Class Library (BCL) type (e.g. System.Int32)

原始类型(例如int)可以直接映射到基类库(BCL)类型(例如System.Int32)

A value type inherits from System.ValueType and is passed by value (among other properties).

值类型继承自System.ValueType,并通过值(以及其他属性)传递。

They are not interchangeable as object (System.Object) is a primitive type but not a value type, and structs are value types but not primitive.

它们不可互换,因为对象(System.Object)是基本类型而不是值类型,结构是值类型但不是原始类型。

See more differences here

在这里查看更多差异

#3


0  

No they are not. A value type is copied and while a reference type is being referenced. See the image值类型和原始类型之间有什么区别?

不,他们不是。复制值类型,同时引用引用类型。看图像

Edit My mistake. Here's an tutorial for the differences: http://www.codeproject.com/Articles/11212/Primitive-Reference-and-Value-Types

编辑我的错误。以下是差异教程:http://www.codeproject.com/Articles/11212/Primitive-Reference-and-Value-Types