Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by awplays49 · Oct 30, 2015 at 06:11 PM · generics

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;
         }
     }
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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. :)

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Oct 31, 2015 at 04:43 PM 1
Share

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.

avatar image Dave-Carlile Bunny83 · Nov 01, 2015 at 01:53 AM 0
Share

Yes, good points - I just noticed the compiler error for not having a return value :)

avatar image
1

Answer by Munchy2007 · Oct 30, 2015 at 06:43 PM

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

http://www.tutorialspoint.com/csharp/csharp_generics.htm

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image awplays49 · Oct 30, 2015 at 07:02 PM 0
Share

Hi @$$anonymous$$unchy2007, thanks for the quick reply!

I think I somewhat understand, but isnt that what the regular parameter is for?

Whats different?

avatar image Dave-Carlile awplays49 · Oct 30, 2015 at 07:11 PM 0
Share

"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.

avatar image awplays49 Dave-Carlile · Oct 30, 2015 at 07:26 PM 0
Share

Hi @Dave Carlile!

unfortunately i still dont follow. The type is already stated in the regular parameters, an array of t.

Show more comments
avatar image
0

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dave-Carlile · Oct 30, 2015 at 08:17 PM 0
Share

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.

avatar image DFT-Games Dave-Carlile · Oct 30, 2015 at 08:21 PM 0
Share

Yes, but the explanation would have been cryptic to a person trying to understand the meaning of that <T> :)

avatar image Dave-Carlile DFT-Games · Oct 30, 2015 at 10:14 PM 0
Share

I'm all for keeping things simple, but your wording implies that it's required. :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

34 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges