8

One way of looking at type safety is that it adds automatic tests all over your code that stop some things breaking in some ways. One of the tools that helps this in .NET is generics.

However, both WinForms and WPF are generics-free. There is no ListBox<T> control, for example, which could only show items of the specified type. Such controls invariably operate on object instead.

Why are generics and not popular with UI framework developers?

3 Answers3

6

WinForms came with .NET 1.0 before there was support for generics. In the case of WPF, development started on that before the Framework supported generics as well. Although generics support came to .NET before WPF was released, it would have taken substantial effort to go back and add it in.

Also, the XAML to represent generics is not pretty

Michael Brown
  • 21,822
2

In case of WinForms it's backward compatibility with early .NET versions that didn't support generics (hence all those object senders in event handlers), while WPF implements databinding differently ({Binding Path=Name} etc.; it works by reflection).

1

For a UI framework it makes much more sense to take objects and bind reflexively than to strongly-type the interfaces somehow. Lots of cases where you need to throw completely arbitrary objects into arbitrary UI containers in cases where you can't divine the types at runtime.

Wyatt Barnett
  • 20,787