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


当前回答

除了其他答案之外,还有一个基本的区别值得注意,那就是数据如何存储在数组中,因为这会对性能产生重大影响。

对于结构,数组包含结构的实例对于类,数组包含指向内存中其他位置的类实例的指针

因此,内存中的结构数组如下所示

[结构][结构]〔结构〕〔结构〕

而类数组看起来像这样

指针

对于一个类数组,您感兴趣的值不会存储在数组中,而是存储在内存的其他位置。

对于绝大多数应用程序来说,这种差异并不重要,但是,在高性能代码中,这将影响内存中数据的位置,并对CPU缓存的性能产生很大影响。在可以/应该使用结构的情况下使用类将大大增加CPU上的缓存未命中数。

现代CPU做的最慢的事情不是处理数字,而是从内存中提取数据,一级缓存命中速度比从RAM读取数据快很多倍。

下面是一些可以测试的代码。在我的机器上,遍历类数组所需的时间大约是结构数组的3倍。

    private struct PerformanceStruct
    {
        public int i1;
        public int i2;
    }

    private class PerformanceClass
    {
        public int i1;
        public int i2;
    }

    private static void DoTest()
    {
        var structArray = new PerformanceStruct[100000000];
        var classArray = new PerformanceClass[structArray.Length];

        for (var i = 0; i < structArray.Length; i++)
        {
            structArray[i] = new PerformanceStruct();
            classArray[i] = new PerformanceClass();
        }

        long total = 0;
        var sw = new Stopwatch();
        sw.Start();
        for (var loops = 0; loops < 100; loops++)
        for (var i = 0; i < structArray.Length; i++)
        {
            total += structArray[i].i1 + structArray[i].i2;
        }

        sw.Stop();
        Console.WriteLine($"Struct Time: {sw.ElapsedMilliseconds}");
        sw = new Stopwatch();
        sw.Start();
        for (var loops = 0; loops < 100; loops++)
        for (var i = 0; i < classArray.Length; i++)
        {
            total += classArray[i].i1 + classArray[i].i2;
        }

        Console.WriteLine($"Class Time: {sw.ElapsedMilliseconds}");
    }

其他回答

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类型。

结构是实际值-它们可以为空,但不能为空

这是正确的,但是也要注意,从.NET2开始,结构支持Nullable版本,C#提供了一些语法糖,使其更易于使用。

int? value = null;
value  = 1;

结构和类别之间的差异:

结构是值类型,而类是引用类型。结构存储在堆栈上,而类存储在堆值类型将其值保存在声明它们的内存中,但引用类型保存对内存中对象的引用。值类型在作用域丢失后立即销毁,而引用类型仅在作用域丢失后销毁变量。该对象随后被垃圾收集器销毁。将结构复制到另一个结构时,该结构的新副本创建。修改的结构不会影响其他结构。将一个类复制到另一个类时,它只复制参考变量。两个引用变量都指向堆上的同一对象。对一个变量所做的更改将影响另一个参考变量。结构不能有析构函数,但类可以有析构器。结构不能具有显式无参数构造函数,而类可以。结构不支持继承,但类支持支持从接口继承。结构为密封型。

为了使其完整,使用Equals方法时还有另一个不同之处,它由所有类和结构继承。

假设我们有一个类和一个结构:

class A{
  public int a, b;
}
struct B{
  public int a, b;
}

在Main方法中,我们有4个对象。

static void Main{
  A c1 = new A(), c2 = new A();
  c1.a = c1.b = c2.a = c2.b = 1;
  B s1 = new B(), s2 = new B();
  s1.a = s1.b = s2.a = s2.b = 1;
}

然后:

s1.Equals(s2) // true
s1.Equals(c1) // false
c1.Equals(c2) // false
c1 == c2 // false

因此,结构适用于类似数字的对象,例如点(保存x和y坐标)。课程适合其他人。即使两个人的名字、身高、体重。。。,他们还是两个人。

如前所述:类是引用类型,而结构是具有所有后果的值类型。

根据经验,框架设计指南建议在以下情况下使用结构而不是类:

它的实例大小小于16字节它逻辑上表示单个值,类似于原始类型(int、double等)它是不可变的它不必经常装箱