tags:

views:

208

answers:

4

What does a syntax like this mean in C#?

public abstract class HostFactory<TApp> : ServiceHostFactory
        where TApp : IFoo
+17  A: 

HostFactory is a public generic class (derived from ServiceHostFactory) with a single generic type argument called TApp, where any supplied TApp must implement the IFoo interface (caveat: the IWhatever pattern is merely a convention; IFoo could be a class, I suppose). The type HostFactory<TApp> is not a concrete type; it must be further subclassed to be instantiated - presumably by things like below (although a generic subclass would also suffice):

public class BarHostFactory : HostFactory<Bar> {}
public class Bar : IFoo {...}
Marc Gravell
Very nice explanation. Upvote!
Shadow
Thanks for explaining Marc!
Nevin Mathai
+3  A: 

This is an abstract generic class, that inherits from ServiceHostFactory with a constraint on the generic type.

Constraints on Type Parameters (C# Programming Guide)

Inheritance (C# Programming Guide)

Generic Classes (C# Programming Guide)

astander
Darn beat me to it :)
Shadow
Thank You Sir! Great Links :)
Nevin Mathai
+1  A: 

It says HostFactory has a generic type TApp and that it inherits from ServiceHostFactory and the where keyword signals that TApp is a constrant.

In C# 2.0 you use the where reserved keyword to define a constraint. Use the where keyword on the generic type parameter followed by a derivation colon to indicate to the compiler that the generic type parameter implements a particular interface.

http://msdn.microsoft.com/en-us/library/ms379564(VS.80).aspx

Shadow
(side note: `where` has other meanings in a generic constraint than just "implements a particular interface" - at least 4 other uses (`: struct`, `: class`, `: new()`, `: SomeBaseType`)
Marc Gravell
+2  A: 

Alot going on here and I wonder if this is homework because of the "IFoo", or maybe you made that replacement to simplify the example.

-: ServiceHostFactory, HostFactory inherits from ServiceHostFactory.

-HostFactory<TApp>, HostFactory is a generic type as it has a type parameter of TApp. Whenever someone uses the HostFactory class, they can specify a type for TApp which will cause that type to be used everywhere TApp appears int he code. So if the class has a function TApp GetApp() and they specify <int> for TApp, then GetApp is actually int GetApp()

-where TApp : IFoo, TApp must implement the IFoo interface(this could have been a class as well, indicating it must inherit from that class, not necesarily directly).

-abstract, HostFactory is an abstract class, meaning other classes can inherit from it, but no code can instantiate the HostFactory itself. This would be like me giving you the frame of a car, and saying, you're not legally allowed to drive this on the road, but you're welcome to make a car of your own out of it.

-public HostFactory is public, meaning it is visible to code outside of the assembly in which it was declared.

Edit: More on generics I will start with a pretty significant quote from MSDN: "Generics are the most powerful feature of C# 2.0."

You would use generics when you write something that could conceivably work with many types. For example, before we had generics in 2.0, we had to cast everything to an Object before we could place it in a collection, which was really dangerous because the copmpiler couldn't verify that you were casting to the correct type whenever you later got an item out of the collection. With generics we can do ArrayList<bool> to create an ArrayList of bool's and now there is no need to cast. The compiler can verify that we are putting bool's into the collection.

For example, in the above class we can write algorithms that work with things that implement IFoo, knowing only that the object will be able to do things that an IFoo interface has. So 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.

AaronLS
Not homework :)
Nevin Mathai
@hmm I'll take your word on it. Before I answered I looked at other questions you've asked in the past and it didn't seem like you were that type of person :)
AaronLS
Actually Generics is confusing to me, as to when would you use them etc and this syntax wasnt familiar to me either (although my guess on the implements was right, I wanted to hear more on how and when you would use this).
Nevin Mathai
What do you mean by algorithms here? is it an method implementation of the abstract HostFactory class?
Nevin Mathai
An algorithm such as a function. Perhaps there is a Sorter<T> class where T:IComparible, and it has a BinarySort(ArrayList<T>) function. The BinarySort is a generic algorithm, and because it knows that T implements IComparable, then it can compare the items in the ArrayList and rearrange them to sort them. So rather than write duplicate code for sorting int's, string's, etc. you just write the algorithm once. They are "Generic" in that they apply to many scenarios. You should start reading here: http://msdn.microsoft.com/en-us/library/ms379564%28VS.80%29.aspx
AaronLS