我什么时候应该使用结构而不是类?

本文翻译自:When should I use a struct instead of a class?

MSDN says that you should use structs when you need lightweight objects. MSDN说你应该在需要轻量级对象时使用结构。 Are there any other scenarios when a struct is preferable over a class? 当结构比类更可取时,还有其他任何情况吗?

Some people might have forgotten that: 有些人可能忘记了:

  1. structs can have methods. 结构可以有方法。
  2. structs cannot be inherited. 结构不能被继承。

I understand the technical differences between structs and classes, I just don't have a good feel for when to use a struct. 我理解结构和类之间的技术差异,我只是对使用结构没有很好的感觉。


#1楼

参考:https://stackoom.com/question/MFt/我什么时候应该使用结构而不是类


#2楼

In addition the the excellent answers above: 另外上面的优秀答案:

Structures are value types. 结构是价值类型。

They can never be set to Nothing . 它们永远不会被设置为Nothing

Setting a structure = Nothing , will set all its values types to their default values. 设置结构=无,将其所有值类型设置为其默认值。


#3楼

结构在堆栈而不是堆上,因此它们是线程安全的,并且应该在实现传输对象模式时使用,您永远不想在堆上使用它们是易失性的,在这种情况下您希望使用调用堆栈,这是使用结构的一个基本案例我很惊讶这里的答案,


#4楼

Use a class if: 使用类如果:

  • Its identity is important. 它的身份很重要。 Structures get copied implicitly when being passed by value into a method. 在通过值传递给方法时,结构会隐式复制。
  • It will have a large memory footprint. 它将占用大量内存。
  • Its fields need initializers. 它的字段需要初始化器。
  • You need to inherit from a base class. 您需要从基类继承。
  • You need polymorphic behavior; 你需要多态行为;

Use a structure if: 使用结构如果:

  • It will act like a primitive type (int, long, byte, etc.). 它将像一个原始类型(int,long,byte等)。
  • It must have a small memory footprint. 它的内存占用量必须很小。
  • You are calling a P/Invoke method that requires a structure to be passed in by value. 您正在调用P / Invoke方法,该方法需要按值传递结构。
  • You need to reduce the impact of garbage collection on application performance. 您需要减少垃圾收集对应用程序性能的影响。
  • Its fields need to be initialized only to their default values. 其字段只需初始化为默认值。 This value would be zero for numeric types, false for Boolean types, and null for reference types. 对于数字类型,此值为零,对于布尔类型,该值为false,对于引用类型,该值为null。
    • Note that in C# 6.0 structs can have a default constructor that can be used to initialize the struct's fields to nondefault values. 请注意,在C#6.0中,结构可以有一个默认构造函数,可用于将struct的字段初始化为非默认值。
  • You do not need to inherit from a base class (other than ValueType, from which all structs inherit). 您不需要从基类继承(除了ValueType,所有结构都从该类继承)。
  • You do not need polymorphic behavior. 您不需要多态行为。

#5楼

我认为最好的答案就是使用struct,当你需要的是一组属性时,类是属性和行为的集合。


#6楼

I am surprised I have not read at any of the previous answer this, which I consider the most crucial aspect : 我很惊讶我没有读过任何前面的答案,我认为这是最关键的方面:

I use structs when I want a type with no identity. 当我想要一个没有身份的类型时,我使用结构。 For example a 3D point: 例如3D点:

public struct ThreeDimensionalPoint
{
    public readonly int X, Y, Z;
    public ThreeDimensionalPoint(int x, int y, int z)
    {
        this.X = x;
        this.Y = y;
        this.Z = z;
    }

    public override string ToString()
    {
        return "(X=" + this.X + ", Y=" + this.Y + ", Z=" + this.Z + ")";
    }

    public override int GetHashCode()
    {
        return (this.X + 2) ^ (this.Y + 2) ^ (this.Z + 2);
    }

    public override bool Equals(object obj)
    {
        if (!(obj is ThreeDimensionalPoint))
            return false;
        ThreeDimensionalPoint other = (ThreeDimensionalPoint)obj;
        return this == other;
    }

    public static bool operator ==(ThreeDimensionalPoint p1, ThreeDimensionalPoint p2)
    {
        return p1.X == p2.X && p1.Y == p2.Y && p1.Z == p2.Z;
    }

    public static bool operator !=(ThreeDimensionalPoint p1, ThreeDimensionalPoint p2)
    {
        return !(p1 == p2);
    }
}

If you have two instances of this struct you don't care if they are a single piece of data in memory or two. 如果你有这个结构的两个实例,你不关心它们是内存中的单个数据还是两个。 You just care about the value(s) they hold. 你只关心他们持有的价值。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值