What is a syntax like this in C #?

What does syntax like this in C # mean?

public abstract class HostFactory<TApp> : ServiceHostFactory where TApp : IFoo 
+6
syntax c #
source share
4 answers

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.

+2
source share

HostFactory is a publicly shared class (derived from ServiceHostFactory ) with one common argument of type TApp , where any supplied TApp must implement the IFoo interface (caveat: the IWhatever pattern is just a convention, IFoo can be a class, I suppose). The HostFactory<TApp> type is not a specific type; it must be further subclassed to create instances - presumably with things like the one shown below (although a general subclass will also suffice):

 public class BarHostFactory : HostFactory<Bar> {} public class Bar : IFoo {...} 
+17
source share

This is an abstract generic class that inherits from ServiceHostFactory with a generic type restriction.

Type Limitations (C # Programming Guide)

Inheritance (C # Programming Guide)

Common Classes (C # Programming Guide)

+3
source share

He says that HostFactory has a common type of TApp and that it inherits from the ServiceHostFactory and where keywords that TApp is a constant.

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

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

+2
source share

All Articles