Class VS Structs

本文详细对比了C#中的类(Class)与结构体(Struct)的主要区别:类支持继承,为引用类型,可能存在空引用,并且每次实例化都会有额外的内存开销;而结构体不支持继承,为值类型,不会出现空引用(除非使用Nullable),实例化时没有额外内存开销。两者均可包含方法与事件,并实现接口。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值