Why Enum's HasFlag method need boxing?

前端 未结 6 812
时光取名叫无心
时光取名叫无心 2020-12-10 03:45

I am reading \"C# via CLR\" and on page 380, there\'s a note saying the following:

Note The Enum class defines a HasFlag method defined as follows

6条回答
  •  粉色の甜心
    2020-12-10 04:25

    When ever you pass a value type of a method that takes object as a parameter, as in the case of console.writeline, there will be an inherent boxing operation. Jeffery Richter discusses this in detail in the same book you mention.

    In this case you are using the string.format method of console.writeline, and that takes a params array of object[]. So your bool, will be cast to object, so hence you get a boxing operation. You can avoid this by calling .ToString() on the bool.

提交回复
热议问题