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?

Roman Starkov
  • 4,469
  • 3
  • 31
  • 38

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

Piotr Golacki
  • 275
  • 2
  • 10
Michael Brown
  • 21,684
  • 3
  • 46
  • 83
  • 1
    I [get the impression that XAML was never meant to be hand-written](http://programmers.stackexchange.com/questions/139382/in-hindsight-is-basing-xaml-on-xml-a-mistake-or-a-good-approach), which makes the ugliness argument moot. XAML is already a PITA to hand-write. As for the history thing – that’s very interesting to know, do you have a reference? – Roman Starkov Apr 13 '12 at 14:06
  • I still write most of my XAML by hand. And even when I use Blend for coarse layout and animations, I still go in and touch it up by hand after the fact. What I should have pointed out is that XAML didn't support generics in V1 (except for as part of the root element) – Michael Brown Apr 13 '12 at 14:08
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).

Konrad Morawski
  • 9,721
  • 4
  • 37
  • 58
  • Is there a good reason why `Binding` itself can’t be generic in source and destination, implementing `IBindingSource` and `IBindingDestination`? – Roman Starkov Apr 13 '12 at 12:34
  • @romkyns maybe because it would force you to update TSrc and TDest twice any time it is necessary (in both the class - model - and the XAML view)? I'm guessing / thinking loud – Konrad Morawski Apr 14 '12 at 09:56
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,685
  • 50
  • 69
  • 1
    `ListBox` solves that perfectly fine, so I’m not sure this answer explains anything. And besides, 99% of the time I only put specific object types into my UIs. – Roman Starkov Apr 13 '12 at 14:17