Class VS Structs

来源:互联网 发布:哪儿有卖淘宝买家数据 编辑:程序博客网 时间:2024/05/09 02:29

A short summary of each:

Classes Only:

  • Can support inheritance
  • Are reference (pointer) types
  • The reference can be null
  • Have memory overhead per new instance

Structs Only:

  • Cannot support inheritance
  • Are value types
  • Are passed by value (like integers)
  • Cannot have a null reference (unless Nullable is used)
  • Do not have a memory overhead per new instance - unless 'boxed'

Both Classes and Structs:

  • Are compound data types typically used to contain a few variables that have some logical relationship
  • Can contain methods and events
  • Can support interfaces
http://stackoverflow.com/questions/13049/whats-the-difference-between-struct-and-class-in-net
0 0