Is there a built in way to determine the recursion level?

  • Thread starter Thread starter james
  • Start date Start date
J

james

In SQL Server there is @@NESTLEVEL which returns the current number of
levels down in a recursive function call. Is there a C# equivilant to get
the current level of a recursive method call? I would like to be able to
tell how many levels down I am at any given point.

thanks

JIM
 
james said:
In SQL Server there is @@NESTLEVEL which returns the current number of
levels down in a recursive function call. Is there a C# equivilant to get
the current level of a recursive method call? I would like to be able to
tell how many levels down I am at any given point.

Not really, but you can get access to the call stack:

StackTrace trace = new StackTrace(true);
int stackFrames = trace.FrameCount;

This isn't the same as @@NESTLEVEL, but it does increment for each method
call.
 
Hi,

NO AFAIK, you could try to use StackTrace and go over the StackFrames
counting.

Or you could implement it using a counter and passing it as a parameter.

cheers,
 
I usually just code it by adding a class variable counter. E.g.

private int m_iMRGLevel;
private void FnCallingMRF()
{
m_iMRGLevel = 0;
MyRecursiveFunction();
}
private void MyRecursiveFunction()
{
m_iMRFLevel++;
// More code
if( someCondition )
MyRecursiveFunction();
}

Of course, you can also use a ref parameter like this:
private void FnCallingMFR()
{
int iLevel = 0;
MyRecursiveFunction( ref iLevel );
}
private void MyRecursiveFunction( ref int level )
{
level++;
if( someCondition )
MyRecursiveFunction( ref level );
}
 
This wont work as it will always increment. It wont decrement as levels are
popped back, will it?

JIM
 
This doesn't tell you the exact level, it includes all levels prior to the
recursive call all the way from Application.Run

Thanks,

JIM
 
No you don't. In a tree traversal of an n-ary tree this will not work. You
need a static variable internal to the method so that its value is retained
from frame to frame. With the incrementing decrementing global variable, it
will continue to increment for each leaf node it goes into even though those
leaf nodes are on the same level.

JIM
 
Hi,
This doesn't tell you the exact level, it includes all levels prior to the
recursive call all the way from Application.Run

No really, you know that the recursion's stackframes are a subset , in fact
they are the one at the top of the stack, all you have to do is know where
it ends and find the parent method ( the one that called the recursive
method) , not a very efficient way though.


Cheers,
 
That's what I was thinking. Something like
void MyRecursiveMethod(...) {
MyRecursiveMethod(..., 1);
}
void MyRecursiveMethod(..., int recursionDepth) {
...
MyRecursiveMethod(...,recursionDepth+1);
}
 
The best way to do this that I have found is to pass the level into the
method when you recurse.

Something like this:

void DoSomething(ICollection objects, int level)
{
// do something with the collection
...
DoSomething(children of one of the objects, level + 1)
...
// do something after the recursion (level will be unchanged)
}
 
This will deffinately work, although I was hoping for a way to avoid passing
parameters.

thanks,

JIM
 
Back
Top