none
Why is UIElementCollection IEnumerable, but not not IEnumerable<T>? RRS feed

  • Question

  • In WPF 4.0, panels such as StackPanel expose a Children of type UIElementCollection.  Does anyone know why UIElementCollection is IList, ICollection, and IEnumerable instead of IList<UIElement>, ICollection<UIElement>, and IEnumerable<UIElement>?

    There is an indexer that returns a UIElement, so it is definitely strongly-typed internally.  It just makes it a bit annoying to do LINQ queries against the children with the Generic implementations.

    Tuesday, June 22, 2010 6:36 PM

Answers

  • The answer to your question is historical: WPF development was started before generics existed in the framework. You will therefore find generics used in WPF itself far less than they really could be. I suppose someone should really do the work to adapt these core classes so that they implement generic interfaces, but nobody has yet done so.
    Controls for WPF, Windows Forms and Silverlight at http://www.divelements.co.uk
    • Marked as answer by Moby Disk Wednesday, June 23, 2010 3:16 PM
    Tuesday, June 22, 2010 7:13 PM

All replies

  • For Linq, you can use Cast<T>() on the UIElementCollection.
    http://blog.voidnish.com
    Tuesday, June 22, 2010 7:06 PM
    Moderator
  • The answer to your question is historical: WPF development was started before generics existed in the framework. You will therefore find generics used in WPF itself far less than they really could be. I suppose someone should really do the work to adapt these core classes so that they implement generic interfaces, but nobody has yet done so.
    Controls for WPF, Windows Forms and Silverlight at http://www.divelements.co.uk
    • Marked as answer by Moby Disk Wednesday, June 23, 2010 3:16 PM
    Tuesday, June 22, 2010 7:13 PM