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

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

问题描述

.NET 中结构和类的区别是什么?

解决方案

在.NET中,有两类类型,引用类型值类型.>

结构是值类型,类是引用类型.

一般区别在于引用类型存在于堆中,而值类型存在于内联中,也就是说,无论您定义了变量还是字段,都可以.

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

包含引用类型的变量包含一个指针,或引用到实际值所在的内存中的其他地方.

这有一个好处,首先是:

  • 值类型总是包含一个值
  • 引用类型可以包含一个 null 引用,这意味着它们目前根本不引用任何东西

在内部,引用类型被实现为指针,知道这一点,并且知道变量赋值的工作原理,还有其他行为模式:

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

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

  • 变量:值类型存在于堆栈中,引用类型存在于堆栈中,作为指向实际内存所在的堆内存中某处的指针(尽管注意Eric Lipperts 文章系列:堆栈是一个实现细节.)
  • class/struct-field:值类型完全位于类型内部,引用类型位于类型内部,作为指向实际内存所在的堆内存中某处的指针.

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

解决方案

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 和 class 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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