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 MFI_Michael · Sep 01, 2016 at 03:10 PM · c#foreachnestedsimplify

How can I simplify a nested foreach

I think I took longer to try to think of a title that summarized this than I did on explaining the actual problem. x_x

I have a function with nested foreach loops that I would like to simplify or at least make re-usable. In this sample, each friends property is of type List<Person>.

 void InviteFriendsToParty(Person p) {
     foreach (Person e in p.friends) 
         foreach (Person r in e.friends) 
             foreach (Person s in r.friends) 
                 foreach (Person o in s.friends) 
                     SendInvitation(o); }

Is it possible to convert this to such a function that I can pass in a source Person p and an int depth and let the nested foreach part be dynamically sized? I've tried a couple of variations, but I think I've been trying to overcomplicate it for so long that I'm missing something really simple.

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

1 Reply

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

Answer by NoseKills · Sep 01, 2016 at 06:05 PM

Something like this could work

 void InviteFriendsToParty(Person p, int depth) {
      foreach (Person e in p.friends) {
           if (depth == 0) {
                 SendInvitation(e);
           } else {
                 InviteFriendsToParty(e, --depth);
           }
      }
 }

You could make it more reusableby parameterizing the function that should be called at the end.

 void DoToFriendsRecursive(Person p, Action<Person> action, int depth) {
          foreach (Person e in p.friends) {
               if (depth == 0) {
                     action(e);
               } else {
                     DoToFriendsRecursive(e, action, --depth);
               }
          }
     }

I'm just guessing here but you probably need either a depth limit like in my example or some way to detect and terminate endless loops. If A is friend of B who's friend of C who's friend of A, they will send invites in a circle unless you specifically stop it

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 MFI_Michael · Sep 01, 2016 at 08:45 PM 1
Share

Awesome. This worked perfectly, with 1 exception. If I pass depth-- into the recursive function, I get StackOverflowException. Changing this to depth-1 however seems to fix it entirely. Fast and concise.

Thank you very much. :)

avatar image NoseKills MFI_Michael · Sep 01, 2016 at 10:02 PM 0
Share

Lol. Of course X-)... depth-- passes forward the non-decremented value. It should work with --depth too. Answer edited.

Glad to be of help.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I check the color of GameObjects in an Array and print out their color? 0 Answers

Retrieving values from list of Scriptable Objects 1 Answer

Using foreach to remove and delete bullet in List - C# 3 Answers

For loop continues to instantiate even after it should be finished 0 Answers

How to create a sequences of pressure plates 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