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

What#39;s the difference between struct and class in .NET?(.NET 中的结构和类有什么区别?)
本文介绍了.NET 中的结构和类有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

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

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

一般的区别是引用类型存在于堆上,而值类型存在于内联,也就是说,无论它是你定义的变量或字段.

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

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

这有一个好处,首先:

  • 值类型总是包含一个值
  • 引用类型可以包含一个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 中的结构和类有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
quot;Overflowquot; compiler error with -9223372036854775808L(编译器错误-9223372036854775808L(Q;溢出Q))
Visual Studio 2010 ReportViewer Assembly References(Visual Studio 2010 ReportViewer程序集引用)
Weird behaviour when I open a reportviewer in WPF(在WPF中打开报表查看器时出现奇怪的行为)
how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)