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 /
  • Help Room /
avatar image
0
Question by ensomniac · May 06, 2016 at 05:26 PM · objectfunctionsfunction callbuiltinbuiltin array

How do you create a generic function for custom types that are inherited from a generic class? C#

I am looking to write a generic function to resize builtin arrays. When I use builtin arrays, they are often of a custom class, like this:

 public RangedSetWeapon[] rangedSetWeapons;

And I want to create a function that works for all builtin arrays, something like this:

 public static void append (Array[] originalArray, GameObject newItem) {
     Array[] updatedArray = new Array[originalArray.Length] + 1;
 
     int i = 0;
     foreach (Array existingItem in originalArray) {
         updatedArray[i] = existingItem;
         i += 1;
     }
 
     updatedArray[i] = newItem;
 
     return updatedArray;
 
 }

But this obviously wont work because the originalArray type is unknown, as is the type of the newItem object.

Is there a way to generisize this code so it would work for ANY builtin array using ANY object type (as long as it matches the originalArray type)?

Any thoughts would be appreciated

Comment
Add comment · Show 1
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 ensomniac · May 06, 2016 at 07:30 PM 0
Share

Thanks to @FortisVenaliter - this function works perfectly:

 public static T[] append <T> (T[] originalArray, T newItem) {
 
     T[] updatedArray = new T[originalArray.Length + 1];
 
     for (int i = 0; i < updatedArray.Length-1; i++) {
         updatedArray[i] = originalArray[i];
     }
 
     updatedArray[updatedArray.Length-1] = newItem;
 
     return updatedArray;
 
 }

1 Reply

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

Answer by FortisVenaliter · May 06, 2016 at 05:40 PM

Yep, and it's called (drum roll please) C# Generics. Basically, you can include one or more type parameters in your function calls that will allow you to determine the type at runtime and execute. Of course, not knowing the type ahead of time limits your functionality. There are a bunch of resources online if you search, but the basic function header would look something like this:

 public static void append<T>(T[] originalArray, T newItem) {...

In this case, the function is using the stand-in type variable 'T' to represent they type.

On a separate note though, if you are looping with an index, you should really use the old-school 'for' instead of the memory hog 'foreach'.

Comment
Add comment · Show 1 · 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 ensomniac · May 06, 2016 at 07:28 PM 0
Share

Epic! Thanks so much for the clear response. Works perfectly.

Thank you also for the memory tip!

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

43 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Any reason why global vars are null within certain functions? 0 Answers

particle system going wild 1 Answer

Invoke function it is called only once 1 Answer

Passing an object to a function in OnDrop 0 Answers

Getting which function affects a GameObject 0 Answers


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