Unity3D - Why do we have to add some parameters outside of braces?

Black

I know many programming and scripting languages, but I never saw something like this before:

camera = GetComponentInParent<Camera>();

Is this normal in C#? Why don't we have to pass the parameter Camera like this:

camera = GetComponentInParent(Camera);

Where can I inform myself about why it is like this?

Paul Kertscher

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Why do we need locks for threads, if we have GIL?

From Java

Why do we have to add forward slash in the file directory to differentiate between directory and files?

From

Why do we have 0.0 and -0.0 in Ruby?

From Java

Why do we have two versions of Java?

From Dev

Why do some array parameters have a comma after opening parentheses?

From Dev

Why do we use double braces for ng-src in AugularJS?

From Dev

Why do we have to add 'View' as parameter in onClick method and what it does?

From Dev

Why do we have to use additional construction when iterating variadic template parameters?

From Dev

Why do we have to add -2 to a string when performing operation in assembly language?

From Dev

Why do we even need assembler when we have compiler?

From Dev

Do we have to explicitly add to db context?

From Dev

Why do we have to dereference stdout here?

From Dev

Why sometimes we have to add .values when we do elementwise operation in pandas?

From Dev

Why do we need double braces in this javascript function?

From Dev

Why do we have immutable empty map?

From Dev

Why do we not have software SIM cards?

From Dev

Why do we have to localize $/ in the code?

From Dev

Why do we have .desktop files?

From Dev

why can't we add elements to ArrayList outside of the main() method?

From Dev

Unity3D - Do we have a plugin or anyone have worked on Tag collection input view, see screenshot

From Dev

Why I need to add '/' to routerLink but we do not have '/' in router definination?

From Dev

Why do we have a namespace for Android?

From Dev

If we have some Binary Search Tree and perform the operations add(x) followed by remove(x) do we necessarily return to the original tree?

From Dev

C++ Why do we use << instead of braces at std::cout?

From Dev

Why do we have to add comma ',' in args in python multithreading?

From Dev

Why do we have .iml file in flutter

From Dev

Why do we have to convert entity to dto?

From Dev

Why do we have to add the "HEAD:" prefix when pushing to a contributor's fork of a GitHub repo?

From Dev

Why do some Linux files have a 'd' suffix?

Related Related

  1. 1

    Why do we need locks for threads, if we have GIL?

  2. 2

    Why do we have to add forward slash in the file directory to differentiate between directory and files?

  3. 3

    Why do we have 0.0 and -0.0 in Ruby?

  4. 4

    Why do we have two versions of Java?

  5. 5

    Why do some array parameters have a comma after opening parentheses?

  6. 6

    Why do we use double braces for ng-src in AugularJS?

  7. 7

    Why do we have to add 'View' as parameter in onClick method and what it does?

  8. 8

    Why do we have to use additional construction when iterating variadic template parameters?

  9. 9

    Why do we have to add -2 to a string when performing operation in assembly language?

  10. 10

    Why do we even need assembler when we have compiler?

  11. 11

    Do we have to explicitly add to db context?

  12. 12

    Why do we have to dereference stdout here?

  13. 13

    Why sometimes we have to add .values when we do elementwise operation in pandas?

  14. 14

    Why do we need double braces in this javascript function?

  15. 15

    Why do we have immutable empty map?

  16. 16

    Why do we not have software SIM cards?

  17. 17

    Why do we have to localize $/ in the code?

  18. 18

    Why do we have .desktop files?

  19. 19

    why can't we add elements to ArrayList outside of the main() method?

  20. 20

    Unity3D - Do we have a plugin or anyone have worked on Tag collection input view, see screenshot

  21. 21

    Why I need to add '/' to routerLink but we do not have '/' in router definination?

  22. 22

    Why do we have a namespace for Android?

  23. 23

    If we have some Binary Search Tree and perform the operations add(x) followed by remove(x) do we necessarily return to the original tree?

  24. 24

    C++ Why do we use << instead of braces at std::cout?

  25. 25

    Why do we have to add comma ',' in args in python multithreading?

  26. 26

    Why do we have .iml file in flutter

  27. 27

    Why do we have to convert entity to dto?

  28. 28

    Why do we have to add the "HEAD:" prefix when pushing to a contributor's fork of a GitHub repo?

  29. 29

    Why do some Linux files have a 'd' suffix?

HotTag

Archive