是否应该在Struct中使用太多数据?

use*_*048 2 c#

public struct Cache {
    public int babyGangters { get; set; }
    public int punks { get; set; }
    public int ogs { get; set; }
    public int mercs { get; set; }
    public int hsPushers { get; set; }
    public int collegeDealers { get; set; }
    public int drugLords { get; set; }
    public int streetHoes { get; set; }
    public int webcamGrls { get; set; }
    public int escort { get; set; }
    public int turns { get; set; }
    public int cash { get; set; }
    public int bank { get; set; }
    public int drugs { get; set; }
    public int totalValue { get; set; }
    public int attackIns { get; set; }
    public int attackOuts { get; set; }
    public int status { get; set; }
    public int location { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

Jon*_*eet 12

这不仅仅是大多数指导方针都太大了,但它也是可变的.在我看来,这是一个更大的红旗.

在许多情况下,可变结构可能会导致意外行为.拒绝吧".

你为什么首先想要这个结构?它真的需要变得可变吗?

  • 结构的好坏取决于应用程序。如果典型的使用场景是:获取结构,那么可变结构就可以了。对其进行更改。把它推回去。此外,可变结构数组通常比对象数组更实用。我不喜欢本质上不是“普通旧数据”的可变结构,但 POD 可变结构对我来说似乎完全合理。 (2认同)

Ste*_*ven 8

经验法则是结构不应大于16个字节(根据框架设计指南).你的结构是76个字节(= 19*4),所以它非常大.但是,您必须衡量性能.大结构对某些应用可能是有益的.

框架设计指南声明:

避免定义结构,除非类型[...]实例大小小于16个字节.

Jeffrey Richterto的注释之一是指导状态:

如果您不打算将它们传递给其他方法或将它们复制到集合类(如数组),则值类型可以超过16个字节.