Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#, struct vs class, faster? [duplicate]

Tags:

Possible Duplicate:
Which is best for data store Struct/Classes?

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?

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

like image 671
AMH Avatar asked Sep 20 '11 11:09

AMH


1 Answers

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

Do not define a structure unless the type has all of the following characteristics:

  • 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.

like image 102
Mark Byers Avatar answered Oct 15 '22 06:10

Mark Byers