.NET中的struct和类之间有什么区别? [英] What's the difference between struct and class in .NET?

查看:168
本文介绍了.NET中的struct和类之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.NET中的struct和类之间有什么区别?



我在寻找一个清晰,简洁和准确的答案。

解决方案

在.NET中,有两种类型的类型, 。



结构是值类型 em>引用类型。



一般的区别是,一个引用类型

包含值类型的变量包含以下内容:

整个值类型值。对于结构,这意味着变量包含整个结构及其所有字段。



包含引用类型的变量包含指针或 引用到实际值所在的内存中的其他位置。



这有一个好处,首先是:




  • 值类型总是包含值

  • p>在内部,引用类型被实现为指针,并且知道,并且知道变量赋值如何工作,还有其他行为模式:




    • 值类型变量的内容复制到另一个变量中,将整个内容复制到新变量中,使两者截然不同。换句话说,复制后,更改一个不会影响另一个

    • 引用类型变量的内容复制到另一个变量,复制引用,这意味着您现在有两个对实际数据的相同存储的 引用。换句话说,在复制之后,改变一个引用中的数据似乎也会影响另一个引用,但只是因为你实际上只是在两个地方查看相同的数据。



    当您声明变量或字段时,以下是两种类型的区别:




    • em>值类型居住在堆栈上, 作为指向堆内存中实际内存所在位置的指针(虽然请注意 Eric Lipperts文章系列:The Stack Is An实施详情。)

    • class / struct-field:值类型完全位于类型,在类型内部作为指向堆内存中实际内存所在位置的指针。


    What's the difference between struct and class in .NET?

    I'm looking for a clear, concise and accurate answer. Ideally as the actual answer, although links to good explanations are welcome.

    解决方案

    In .NET, there are two categories of types, reference types and value types.

    Structs are value types and classes are reference types.

    The general difference is that a reference type lives on the heap, and a value type lives inline, that is, wherever it is your variable or field is defined.

    A variable containing a value type contains the entire value type value. For a struct, that means that the variable contains the entire struct, with all its fields.

    A variable containing a reference type contains a pointer, or a reference to somewhere else in memory where the actual value resides.

    This has one benefit, to begin with:

    • value types always contains a value
    • reference types can contain a null-reference, meaning that they don't refer to anything at all at the moment

    Internally, reference types are implemented as pointers, and knowing that, and knowing how variable assignment works, there are other behavioral patterns:

    • copying the contents of a value type variable into another variable, copies the entire contents into the new variable, making the two distinct. In other words, after the copy, changes to one won't affect the other
    • copying the contents of a reference type variable into another variable, copies the reference, which means you now have two references to the same somewhere else storage of the actual data. In other words, after the copy, changing the data in one reference will appear to affect the other as well, but only because you're really just looking at the same data both places

    When you declare variables or fields, here's how the two types differ:

    • variable: value type lives on the stack, reference type lives on the stack as a pointer to somewhere in heap memory where the actual memory lives (though note Eric Lipperts article series: The Stack Is An Implementation Detail.)
    • class/struct-field: value type lives completely inside the type, reference type lives inside the type as a pointer to somewhere in heap memory where the actual memory lives.

    这篇关于.NET中的struct和类之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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