Sometimes I find myself writing tail recursive functions. I have been searching high and low, and I have found there is tail recursion in the .NET framework, but I'm not sure in what cases I can, and in what cases I can't use tail recursion efficiently. For example, I have a simple tree, lets call it
public class Tree
{
public Tree parent = null;
public Tree(Tree parent)
{
this.parent = parent;
this.children = new List<Tree>();
}
public List<Tree> children {get; private set;}
public Tree root
{
get
{
return this.parent == null ? this : parent.root;
}
}
}
for the root property, will the compiler emit a loop? will it emit .tail? Will the jitter respect the .tail? will nothing fancy happen, and will the algorithm just run recursively? Most importantly, should I rewrite this to be iterative?