无法单步执行递归函数断点 VS2013
本文关键字:断点 VS2013 递归函数 执行 单步 | 更新日期: 2024-11-06 21:47:20
我试图将带有for循环的方法转换为递归,作为编码Kata实践的一部分(试图用递归方法解决问题)。逻辑中没有太多内容,但是
- 断点不会在递归方法中的任何位置命中。
- 我尝试放置一个记录器(控制台输出)来检查是否调用了该方法,但没有记录任何内容。
以下是方法定义:
// Original method with for loop
public IEnumerable<Tuple<int, int>> GetElementWithLargestDeltaOnTimeline(int[] a)
{
int runningLindex = 0;
int currLValue = a[0];
int runningHindex = 1;
int currHvalue = a[1];
int currDelta = 0;
for (int i = 1; i < a.Length - 1; i++)
{
if (a[i] < currLValue)
{
currLValue = a[i];
runningLindex = i;
}
for (int j = runningLindex + 1; j < a.Length; j++)
{
if ((a[j] - currLValue) > currDelta)
{
currDelta = a[j] - currLValue;
runningHindex = j;
currHvalue = a[j];
}
}
}
yield return new Tuple<int, int>(currLValue, runningLindex);
yield return new Tuple<int, int>(currHvalue, runningHindex);
}
递归的-
// Trying above method to convert to recursive,
// Note - It may not be correct *shy* but the problem is why it's not doing anything(not step through/logging)
public IEnumerable<Tuple<int, int>> GetElementWithLargestDeltaOnTimelineRec
(int[] a, int i, int j, int runningLindex, int currLValue, int runningHindex, int currHvalue, int currDelta)
{
Console.WriteLine("Iteration i-{0}: j-{1} runningLindex-{2} currLValue-{3} runningHindex-{4} currHvalue-{5} currDelta-{6}"
, i, j, runningLindex, currLValue, runningHindex, currHvalue, currDelta);
if (i < a.Length)
{
if (a[i] < currLValue)
{
currLValue = a[i];
runningLindex = i;
}
if (j < a.Length)
{
if ((a[j] - currLValue) > currDelta)
{
currDelta = a[j] - currLValue;
runningHindex = j;
currHvalue = a[j];
}
GetElementWithLargestDeltaOnTimelineRec(a, i, j++, runningLindex, currLValue, runningHindex, currHvalue, currDelta);
}
}
else
{
yield return new Tuple<int, int>(currLValue, runningLindex);
yield return new Tuple<int, int>(currHvalue, runningHindex);
}
GetElementWithLargestDeltaOnTimelineRec(a, i++, runningLindex + 1, runningLindex, currLValue, runningHindex, currHvalue, currDelta);
yield break;
}
主要-
public class Program
{
public static void Main()
{
var a = new[] { 10, 9, 3, 6, 7, 8, 15, 10, 6 };
var val = new StockManager();
var result = val.GetElementWithLargestDeltaOnTimelineRec(a, 0, 0, 0, a[0], 1, a[1], 0);
}
}
问题 -
- 递归方法调用是否是导致(不正确)的原因问题?
- 为什么该方法未被调用并返回空结果没有失败/错误/警告?
附加信息 -.Net 4.5, Visual Studio 2013
我还尝试在不同的机器上运行此代码(只是为了验证我的 VS 实例是否有问题)。
返回IEnumerable
的方法有一个小技巧。你应该列举它们!
与其在自身内部调用GetElementWithLargestDeltaOnTimelineRec
,不如枚举它并 yield 返回其元素或存储结果以供以后使用。 IEnumerable
在枚举之前什么都不做。
foreach (var e in GetElementWithLargestDeltaOnTimelineRec(...))
yield return e;
或
var innerResult = GetElementWithLargestDeltaOnTimelineRec(...);
并以某种方式使用innerResult
导致它被枚举。