.NET中的结构和类有什么区别?
当前回答
从微软在类和结构之间的选择。。。
根据经验,框架中的大多数类型应该是类。然而,在某些情况下值类型的特性使其更适合使用结构。✓ 考虑结构而不是类:如果该类型的实例很小并且通常很短,或者通常嵌入在其他对象中。X避免结构,除非该类型具有以下所有属性特点:它在逻辑上表示单个值,类似于原始类型(int、double等)。它的实例大小小于16字节。它是不可变的。(无法更改)它不必经常装箱。
其他回答
Struct | Class | |
---|---|---|
Type | Value-type | Reference-type |
Where | On stack / Inline in containing type | On Heap |
Deallocation | Stack unwinds / containing type gets deallocated | Garbage Collected |
Arrays | Inline, elements are the actual instances of the value type | Out of line, elements are just references to instances of the reference type residing on the heap |
Al-Del Cost | Cheap allocation-deallocation | Expensive allocation-deallocation |
Memory usage | Boxed when cast to a reference type or one of the interfaces they implement, Unboxed when cast back to value type (Negative impact because boxes are objects that are allocated on the heap and are garbage-collected) |
No boxing-unboxing |
Assignments | Copy entire data | Copy the reference |
Change to an instance | Does not affect any of its copies | Affect all references pointing to the instance |
Mutability | Should be immutable | Mutable |
Population | In some situations | Majority of types in a framework should be classes |
Lifetime | Short-lived | Long-lived |
Destructor | Cannot have | Can have |
Inheritance | Only from an interface | Full support |
Polymorphism | No | Yes |
Sealed | Yes | When have sealed keyword (C#), or Sealed attribute (F#) |
Constructor | Can not have explicit parameterless constructors | Any constructor |
Null-assignments | When marked with nullable question mark | Yes (When marked with nullable question mark in C# 8+ and F# 5+ 1) |
Abstract | No | When have abstract keyword (C#), or AbstractClass attribute (F#) |
Member Access Modifiers | public , private , internal |
public , protected , internal , protected internal , private protected |
1不鼓励在F#中使用null,请改用Option类型。
从微软在类和结构之间的选择。。。
根据经验,框架中的大多数类型应该是类。然而,在某些情况下值类型的特性使其更适合使用结构。✓ 考虑结构而不是类:如果该类型的实例很小并且通常很短,或者通常嵌入在其他对象中。X避免结构,除非该类型具有以下所有属性特点:它在逻辑上表示单个值,类似于原始类型(int、double等)。它的实例大小小于16字节。它是不可变的。(无法更改)它不必经常装箱。
在.NET中,有两类类型,引用类型和值类型。
结构是值类型,类是引用类型。
一般的区别是,引用类型存在于堆中,值类型存在于内联中,也就是说,在定义变量或字段的任何位置。
包含值类型的变量包含整个值类型值。对于结构,这意味着变量包含整个结构及其所有字段。
包含引用类型的变量包含指针,或对内存中实际值所在位置的引用。
首先,这有一个好处:
值类型始终包含值引用类型可以包含空引用,这意味着它们此刻根本不引用任何内容
在内部,引用类型被实现为指针,并且知道变量赋值是如何工作的,还有其他行为模式:
将值类型变量的内容复制到另一个变量中,将整个内容复制到新变量中,使这两个变量不同。换句话说,复制后,对其中一个的更改不会影响另一个将一个引用类型变量的内容复制到另一个变量中,就会复制引用,这意味着现在有两个引用指向实际数据的同一个存储位置。换句话说,在复制之后,更改一个引用中的数据似乎也会影响另一个引用,但这只是因为您实际上只是在两个地方查看相同的数据
当您声明变量或字段时,以下是两种类型的区别:
变量:值类型位于堆栈上,引用类型位于堆栈中,作为指向堆内存中实际内存所在位置的指针(请注意EricLipperts系列文章:堆栈是一个实现细节)类/结构字段:值类型完全位于类型内部,引用类型位于类型内部作为指向堆内存中实际内存所在位置的指针。
结构是实际值-它们可以为空,但不能为空
这是正确的,但是也要注意,从.NET2开始,结构支持Nullable版本,C#提供了一些语法糖,使其更易于使用。
int? value = null;
value = 1;
我♥ 可视化,在这里我创建了一个来展示结构和类之间的基本区别。
以及文本表示,以防万一;)
+--------------------------------------------------+------+----------------------------------------------+
| Struct | | Class |
+--------------------------------------------------+------+----------------------------------------------+
| - 1 per Thread. | | - 1 per application. |
| | | |
| - Holds value types. | | - Holds reference types. |
| | | |
| - Types in the stack are positioned | | - No type ordering (data is fragmented). |
| using the LIFO principle. | | |
| | | |
| - Can't have a default constructor and/or | | - Can have a default constructor |
| finalizer(destructor). | | and/or finalizer. |
| | | |
| - Can be created with or without a new operator. | | - Can be created only with a new operator. |
| | | |
| - Can't derive from the class or struct | VS | - Can have only one base class and/or |
| but can derive from the multiple interfaces. | | derive from multiple interfaces. |
| | | |
| - The data members can't be protected. | | - Data members can be protected. |
| | | |
| - Function members can't be | | - Function members can be |
| virtual or abstract. | | virtual or abstract. |
| | | |
| - Can't have a null value. | | - Can have a null value. |
| | | |
| - During an assignment, the contents are | | - Assignment is happening |
| copied from one variable to another. | | by reference. |
+--------------------------------------------------+------+----------------------------------------------+
有关更多信息,请查看以下内容:
类和结构(官方文档)。在类和结构之间选择(官方文档)。
推荐文章
- HTTP POST返回错误:417“期望失败。”
- 为什么Path。以Path.DirectorySeparatorChar开头的文件名合并不正确?
- c# .NET中的App.config是什么?如何使用它?
- String类中的什么方法只返回前N个字符?
- 我如何提高ASP。NET MVC应用程序性能?
- 无法解析类型为“Microsoft.AspNetCore.Http.IHttpContextAccessor”的服务
- 如何在没有任何错误或警告的情况下找到构建失败的原因
- Visual Studio弹出提示:“操作无法完成”
- 否ConcurrentList<T>在。net 4.0?
- 在c#中解析字符串为日期时间
- 由Jon Skeet撰写的《Singleton》澄清
- 自定义数字格式字符串始终显示符号
- Post参数始终为空
- string.ToLower()和string.ToLowerInvariant()
- 检查instance是否属于某个类型