开发者

In C# Generics are these two lines of code identical?

开发者 https://www.devze.com 2023-03-11 12:44 出处:网络
While reading about generics I came across those two chunks of code, and I was wondering if they were identical?

While reading about generics I came across those two chunks of code, and I was wondering if they were identical?

public abstract class Search<T, TCollection, TCriteria>
        where TCollection : Collection<Name>
        where T : Name

AND:

public abstract class Search<Name, Collection&l开发者_如何学Got;Name>, TCriteria>


No, because in the second you are specifying a concrete generic parameter but in the first you are specifying that the generic parameter can be any subclass of the type specified in the where clause.


As Lasse pointed out, your second version doesn't compile. If you changed it to

public abstract class Search<Name, Collection, TCriteria>

it would compile, but it wouldn't do what you wanted it to do: This just specifies a generic class with three type parameters called Name, Collection and TCriteria. But it doesn't limit them in any way, so you could create an instance like Search<int, long, ulong>.

Type parameters usually start with T, but the language doesn't enforce it in any way.

So the difference is that the second version doesn't work, use the first one.

0

精彩评论

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