Home  /  Questions  /  Question

50   50
Jul 09, 2011

Question about the Presenter class in the Design Pattern Framework

This may be more of a C# question and not a Design Pattern question, but could someone please explain the following line from the WinForms Presenter class?

public class Presenter<T> where T : IView

Is this the same as an abstract class where the Presenter takes on the type of IView that is passed to it?  If so, what is the difference between using the above statement and an abstract class?

Thank you,
G. Deward

1,128   99.9
Jul 12, 2011
This is all about Generics (http://msdn.microsoft.com/en-us/library/ms379564(v=vs.80).aspx), And generics allows you to define type-safe data structures, without committing to actual data types.

public class Presenter<T>

In the above example, T could be any type. But in your example, generic constraints are used. This means that where T is used, it must be derived from an IView interface. This means that you are enforcing anyone what uses the presenter to supply a view that derives from an IView interface.

I do think that when you specify a generic constraint like this that Visual Studio will provide you with intellisense as well.

This is not the same as abstract class, instead it is a means of adding type-safety with generics and also to add some constraints as to what base type the developer can use for the generic type T.

Hope that made sense.
Good Luck!