Generics < T >

Generics introduce the concept of type parameters to .NET, which make it possible to design classes and methods that defer the specification of one or more types until the class or method is declared and instantiated by client code. Generic classes and methods combine reusability, type safety, and efficiency in a way that their non-generic counterparts cannot. Generics are most frequently used with collections and the methods that operate on them. The System.Collections.Generic namespace contains several generic-based collection classes. MSDN

Generics allow the data-Type of a class or method to be specified when used within a a code statement, rather than being defined as part of a class definition. Generics are defined with < T > as a placeholder for the dataType that will be specified when used in code. In Unity, the fact that many GameObject and Object methods are specified as Generics means that the method will work for any type of component. This provides standard Unity methods that can be used with custom created Components.

Examples of Generics used in Unity Animator animator = GetComponent< Animator >( );

MyComponent myComponent = GetComponent< MyComponent >( );

Last updated