The parameter in the first version is a type parameter. Type parameters do not refer to values, but to types. This is used in generic methods and classes, that allow (a certain) flexibility of the types used in the class, while staying statically typed.
Compare the generic list class List<T>
. You have to pass a type parameter to enforce that you can't add instances of other types.
List<int> myList = new List<int>();
myList.Add(1);
myList.Add(2);
foreach(var item in myList)
{
// ...
}
Since myList
has the type parameter int
you and the compiler (and intellisense) know, that everything in myList
is an int
. Hence item
will have the type int
. On the other hand the following is not possible
myList.Add("FooBar");
since List<T>.Add
has the signature void Add(T item)
and creating a List<int>
fixes the typeparameter T
to int
.
The second syntax
If Camera
is a type this syntax is no valid C#. The compiler will throw the error
Camera
is a type, which is not valid in the current context.
If GetComponentInParent
would take a Type
parameter (a parameter of the type Type
, not a generic type parameter) you could call it like
GetComponentInParent(typeof(Camera))
but you will lose the merits of generics, i.e. the return type of GetComponentInParent
won't be a Camera
, but a less specialized type. If the components that can be returned by GetComponentInParent
have no common ancestor, it might even return an object
, whereas the generic version might have the signature
T GetComponentInParent<T>()
ich which case it would return the right type out of the box without the need to cast the result.