C#中,结构类VS更快? [英] C#, struct vs class, faster?

查看:126
本文介绍了C#中,结构类VS更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
哪一个最适合的数据存储结构/类?

考虑在那里我有一样的年龄,姓名,性别,职务,薪水属性Employee对象的例子。我现在有一个名单,我想带着一帮员工(每个Employee实例是唯一的)来填充。

Consider the example where I have an Employee object with attributes like age, name, gender, title, salary. I now have a list i want to populate with a bunch of Employees (each Employee instance is unique).

在刚刚的速度和内存占用方面,是更可取创建员工为结构或类?

In terms of just speed and memory footprint, is it more preferable to create the employee as a Struct or a Class?

欢迎关于结构体VS类在上述情况下的任何额外的警告

Any additional caveats regarding Struct vs Class in the above scenario are welcome

推荐答案

结构是仅被用于应该具有的价值类似的行为相对小的结构。

Structs are to be used only for relatively small structures that should have value-like behaviour.

  • Class and struct differences
  • Choosing Between Classes and Structures

除非类型有以下所有
特性
不要定义一个结构:


  • 这在逻辑上代表一个值,类似于原始类型
    (整数,双,等)。

  • 它有一个实例大小超过
    较小的16个字节。

  • 这是不可改变的。

  • 它不会有要经常装箱。

  • It logically represents a single value, similar to primitive types (integer, double, and so on).
  • It has an instance size smaller than 16 bytes.
  • It is immutable.
  • It will not have to be boxed frequently.

您类型打破了前两个指导方针,以及可能还有第三个。所以,你一定要在这里使用的类。

Your type breaks the first two guidelines, and probably also the third. So you should definitely use a class here.

这篇关于C#中,结构类VS更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆