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

前端 未结 4 1589
暖寄归人
暖寄归人 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:23

    This is just because the ControlCollection class came around before generics; so it implements IEnumerable but not IEnumerable.

    Fortunately, there does exist a LINQ extension method on the IEnumerable interface that allows you to generate an IEnumerable through casting: Cast. Which means you can always just do this:

    var c = Controls.Cast().Where(x => x.ID == "Some ID").SingleOrDefault();
    

提交回复
热议问题