Why doesn't the Controls collection provide all of the IEnumerable methods?

前端 未结 4 1575
暖寄归人
暖寄归人 2020-12-29 18:33

I\'m not for sure how the ControlCollection of ASP.Net works, so maybe someone can shed some light on this for me.

I recently discovered the magic that is extension

4条回答
  •  無奈伤痛
    2020-12-29 19:05

    No, IEnumerable doesn't have many extension methods on it: IEnumerable does. They are two separate interfaces, although IEnumerable extends IEnumerable.

    The normal LINQ ways of converting are to use the Cast() and OfType() extension methods which do extend the nongeneric interface:

    IEnumerable textBoxes = Controls.OfType();
    IEnumerable controls = Controls.Cast();
    

    The difference between the two is that OfType will just skip any items which aren't of the required type; Cast will throw an exception instead.

    Once you've got references to the generic IEnumerable type, all the rest of the LINQ methods are available.

提交回复
热议问题