I was under the impression that the only difference between Func and Action is that the former has to have a return value.So I thought you can call a recursive linq from either a Func or Action. I am new to C# and I am just experimenting and curious.
So I tried the following to recursively print the nested types within a Type.
Type t = typeof(Lev1);
Action<Type> p1 = null, p2 = null;
p1 = tn =>
{
Console.WriteLine(tn.Name);
tn.GetNestedTypes().Select(x => { p1(x); return x; });
};
p2 = tn =>
{
Console.WriteLine(tn.Name);
tn.GetNestedTypes().ToList().ForEach(x => { p2(x);});
};
p1(t);
Console.WriteLine("=".PadRight(50, '='));
p2(t);
So the result I got was that p1 (which uses recursion from a Func-ie Select) only prints the top level whereas p2 which uses Action-ie Foreach prints all levels.
I thought Func is just a function def so recursion is valid. Sure my understanding is wrong can somebody explain
The reason you see only the top-level in the first implementation is because the
Selectis lazily evaluated. It only starts returning values when it needs to, for example when you iterate it (or when you callSumor a number of other functions). If you add aToList()call after theSelect, it will work.