Continued here, and I am wondering if this is homework due to “IFoo”, or maybe you made this replacement to simplify the example.
- : ServiceHostFactory , HostFactory is inherited from ServiceHostFactory.
- HostFactory<TApp> , HostFactory is a typical type, since it has a parameter of type TApp. Whenever someone uses the HostFactory class, he can specify a type for TApp, which will cause this type to be used everywhere. TApp appears in the code. Therefore, if the class has a TApp GetApp() function TApp GetApp() and they indicate <int> for TApp, then GetApp is actually int GetApp()
- where TApp : IFoo , TApp should implement the IFoo interface (it can be a class, indicating that it should inherit from this class, and not necessarily directly).
- abstract , HostFactory is an abstract class, that is, other classes can inherit it, but no code can create an instance of HostFactory. It would be like I give you the frame of the car and say: you are not allowed to drive along this path legally, but you can make your own car.
- public HostFactory is public, which means that it is displayed by code outside the assembly in which it was declared.
Edit: more about generics I'll start with a pretty significant quote from MSDN : "Generics is C # 2.0's most powerful feature."
You would use generics when writing something that could possibly work with many types. For example, before we got generics in version 2.0, we had to discard all objects before we could put them in a collection, which was really dangerous because copmpiler could not verify that you selected the correct type whenever You received an item from the collection. Using generics, we can make an ArrayList<bool> to create an ArrayList from bool, and now there is no need to throw. The compiler can verify that we put the bool in the collection.
For example, in the above class, we can write algorithms that work with things that implement IFoo, knowing only that the object can do what the IFoo interface has. Therefore, I can write an algorithm that calls methods on the IFoo interface, and then my algorithm can be reused in the future by anyone who implements the IFoo interface.