C
C#16mo ago
novial

❔ code simplification

any way to simplify this?
private Transform[] allChildrenHelper(List<Transform> children)
{
foreach (Transform child in Children)
{
children.Add(child);
children = (List<Transform>)children.Concat(allChildrenHelper(children));
}
return children.ToArray();
}
/// <summary>
/// Gets all children of this game object.
/// </summary>
public Transform[] AllChildren => allChildrenHelper(new List<Transform>());
private Transform[] allChildrenHelper(List<Transform> children)
{
foreach (Transform child in Children)
{
children.Add(child);
children = (List<Transform>)children.Concat(allChildrenHelper(children));
}
return children.ToArray();
}
/// <summary>
/// Gets all children of this game object.
/// </summary>
public Transform[] AllChildren => allChildrenHelper(new List<Transform>());
3 Replies
JakenVeina
JakenVeina16mo ago
depends what are you trying to do? is.... is that.... polymorphism abuse on a LINQ method?
(List<Transform>)children.Concat(allChildrenHelper(children))
(List<Transform>)children.Concat(allChildrenHelper(children))
Johnnnnnnn
Johnnnnnnn16mo ago
Yes, it can be simplified into one line:
public Transform[] AllChildren => new Transform[0];
public Transform[] AllChildren => new Transform[0];
Oops, I didn't notice that there's Children list in use that is not shown in the code... confusing for me that an an empty "children" list is passed in and itteration is done over a different "Children" list.
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server