I have a CustomObject with n Children. These children are a List of CustomObjects. Something like this:
public class CustomObject
{
public List<CustomObject> Children = new List<CustomObject>();
}
What I am looking for is the most performant way to get ALL n Children and their children and subchildren etc, from a single instance of CustomObject. Is there a better way than looping through all veigns until I reach the end (null)?
(C#, .NET 3.5)
To make it more clear, I will make an example structure:
//root object
CustomObject.Children ->
CustomObject.Children ->
CustomObject
CustomObject
CustomObject.Children ->
CustomObject.Children ->
CustomObject
CustomObject
CustomObject
In this case, I need to get ALL custom objects beneath the root object.
No, you just have to loop through everything. You can use a recursive method:
public void GetItems(List<CustomObject> list) {
list.Add(this);
foreach (CustomObject child in Childs) {
child.GetItems(list);
}
}
Usage:
List<CustomObject> items = new List<CustomObject>();
someCustomObject.GetItems(items);
Note: The plural form of child is children.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With