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 *;*

_ array = tmp;_
* }*
* }*
}

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 < T > 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. :slight_smile:

It allows you to pass in an array (in this instance) of any type.

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 :slight_smile: Let’s translate your example into plain English, by the way, I don’t believe that code compiles :slight_smile:

    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 :slight_smile:

Hope this helps :slight_smile:

-Pino