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

While I was reading about generics, I came across these two pieces of code, and I was wondering if they are identical?

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

and

public abstract class Search<Name, Collection<Name>, TCriteria>
+3
source share
2 answers

As Lasse noted, your second version does not compile. If you changed it to

public abstract class Search<Name, Collection, TCriteria>

it will compile, but it won’t do what you want it to do: it just indicates a common class with three type parameters Name, Collectionand TCriteria. But this in no way limits them, so you can instantiate the type Search<int, long, ulong>.

Type parameters usually begin with T, but the language does not apply it in any way.

So the difference is that the second version does not work, use the first.

+1
source

, , , , where.

+5

All Articles