Do all C# casts result in boxing/unboxing

后端 未结 3 1722
南方客
南方客 2021-02-07 19:00

I am curious to know if all casts in C# result in boxing, and if not, are all casts a costly operation?

Example taken from Boxing and Unboxing (C# Programming Guide)

3条回答
  •  南旧
    南旧 (楼主)
    2021-02-07 19:52

    No.

    Boxing means putting a value into a new reference type instance.

    Standard casts between reference types do not result in any allocations.
    (User-defined casts can do anything)

提交回复
热议问题