- Home /
Why do I have to use a generic parameter when creating generic functions?
I looked up a tutorial and Im an intermediate user. I know a lot of basics and intermediate. However, What does the T in angle brackets do?
using UnityEngine;
using System.Collections;
public static class Utility : MonoBehaviour {
public static T [] RandomizeArray <T> (T [] array) {
for (int i = 0; i < array.Length; i ++)
{
int randomIndex = Random.Range (i, array.Length);
GameObject tmp = array [randomIndex];
array [randomIndex] = array [i];
array [i] = tmp;
}
}
}
Answer by Dave-Carlile · Oct 30, 2015 at 07:43 PM
You have to tell the compiler that you're declaring a generic function, otherwise it will assume that T is a type you've defined somewhere else. By using the syntax you're telling the compiler that T is a stand-in for an actual type that will have to be figured out whenever the function is used.
It doesn't even have to be T, it can be any identifier. You can also do multiple types, e.g..
void SomeGenericFunction<X, Y>(X value1, Y value2)...
Without the angle brackets, the compiler will see...
void SomeGenericFunction(X value1, Y value2)...
And will give you an error unless you've defined the data types X and Y.
Back to your original function, when you call it you can specify the generic type...
int[] myIntArray...
RandomizeArray<int>(myIntArray)...
Or if the compiler can figure out the data type you can just pass it in...
RandomizeArray(myIntArray)...
And one final thing, in your function you're using GameObject during the swap. You should be using T instead, so instead of
GameObject tmp = array[randomIndex];
You should use
T tmp = array[randomIndex];
if that makes sense. And one last thing, you need to return the sorted array at the end. :)
Well explained. Just the "last thing" is not really necessary since he's directly changing the array so the array is simply shuffled. Arrays are reference types. He doesn't create a new array or something like that.
Of course he either need to return the array again or remove the return type and use void ins$$anonymous$$d.
Yes, good points - I just noticed the compiler error for not having a return value :)
Answer by Munchy2007 · Oct 30, 2015 at 06:43 PM
It allows you to pass in an array (in this instance) of any type.
Hi @$$anonymous$$unchy2007, thanks for the quick reply!
I think I somewhat understand, but isnt that what the regular parameter is for?
Whats different?
"Any type" is key here. When not using generics you would have to use a specific type, e.g...
public static int[] RandomizeArray(int[] array)...
By using generics that same code and be used for any array type, in a type-safe manner.
Hi @Dave Carlile!
unfortunately i still dont follow. The type is already stated in the regular parameters, an array of t.
Answer by DFT-Games · Oct 30, 2015 at 08:09 PM
Hi @awplays49,
To understand geneics you have to overlook the ugly syntax because otherwise you'll be misled. Here's a simple explanation.
A generic definition is nothing else than a type replace mechanism: the compiler will replace the ugly T (or whatever one wants to use) with the type in the usage declaration. I know... it's a mouthful :) Let's translate your example into plain English, by the way, I don't believe that code compiles :)
public static T[] RandomizeArray<T>(T[] array)
It reads: Declare a Static method called RandomizeArray that takes an array of type T as a parameter ad gives back an array of the same type T.
The notation is the key thing, it's the part that will allow you to tell what is the type that you want to use when you call the method, therefore all the following are valid:
int[] randomizedArrayOfINT = RandomizeArray<int>(someOriginalArrayOfINT);
string[] randomizedArrayOfSTRING = RandomizeArray<string>(someOriginalArrayOfSTRING);
string[] randomizedArrayOfGAMEOBJECT = RandomizeArray<GameObject>(someOriginalArrayOfGAMEOBJECT);
The magic happens in those <int>, <string>
and <GameObject>
: that is what tells the compiler how to replace all the T
in that code :)
Hope this helps :)
-Pino
When you call the function you don't have to specify the . The compiler can generally infer this based on the array that's passed.
Yes, but the explanation would have been cryptic to a person trying to understand the meaning of that <T>
:)
I'm all for keeping things simple, but your wording implies that it's required. :)
Your answer

Follow this Question
Related Questions
MonoBehaviour pseudo-singleton with C# generics 1 Answer
2 problems with serialization of a generic list of a custom class 3 Answers
(analytics beta) How to initialize Generic Dictionary from JS? 1 Answer
Can I right a generic method for components having same member variable? 1 Answer
Is the class derived/inherited from generic list serializable? 1 Answer