Co-variant array conversion from x to y may cause run-time exception

前端 未结 7 1406
感动是毒
感动是毒 2020-12-23 15:37

I have a private readonly list of LinkLabels (IList). I later add LinkLabels to this list and add those

7条回答
  •  清酒与你
    2020-12-23 16:06

    The warning is due to the fact that you could theoretically add a Control other than a LinkLabel to the LinkLabel[] through the Control[] reference to it. This would cause a runtime exception.

    The conversion is happening here because AddRange takes a Control[].

    More generally, converting a container of a derived type to a container of a base type is only safe if you can't subsequently modify the container in the way just outlined. Arrays do not satisfy that requirement.

提交回复
热议问题