开发者

What does a syntax like this mean in C#?

开发者 https://www.devze.com 2022-12-27 03:18 出处:网络
What does a syntax like this mean in C#? public abstr开发者_运维技巧act class HostFactory<TApp> : ServiceHostFactory

What does a syntax like this mean in C#?

public abstr开发者_运维技巧act class HostFactory<TApp> : ServiceHostFactory
        where TApp : IFoo


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 {...}


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)


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消