There's the recursive approach with . . .
A source object:
class MyObj
{
public int AnInteger;
}
A generic recursive sort function:
private static void GetValues<T>(List<T> sourceList, int iter, List<T> resultList, Func<T, bool> compareFunc)
{
if (iter >= sourceList.Count)
{
return;
}
if (compareFunc(sourceList[iter]))
{
resultList.Add(sourceList[iter]);
}
GetValues(sourceList, iter + 1, resultList, compareFunc);
}
and a usage case:
List<MyObj> myList = new List<MyObj>();
for (int i = 0; i < 10; i++)
{
myList.Add(new MyObj() { AnInteger = i });
}
List<MyObj> results = new List<MyObj>();
GetValues(myList, 0, results, obj => obj.AnInteger < 5);
foreach (MyObj obj in results)
{
Trace.WriteLine("Obj: " + obj.AnInteger);
}
Hopefully this is a homework assignment or some kind of exercise because limiting yourself to no looping and no linq in this case is kind of silly. This recursive example is by far not the best solution performance wise. Also,
List
s are preferred over
ArrayList
s now.