You need to enable JavaScript to run this app. C#打印List<StorageItems>的amount值及StackOverflowException异常解决
优惠活动
大模型
产品
解决方案
定价
更多

C#打印List<StorageItems>的amount值及StackOverflowException异常解决

问题原因

存在两个核心问题:

  • 循环逻辑越界:List<T>的索引从0起始,有效范围为0 ~ 元素总数 - 1。你当前的循环条件是counter <= listrow,其中listrow是元素总个数(8个元素时值为8),会尝试访问不存在的第9个元素,触发索引越界异常。
  • 栈溢出(System.StackOverflowException)的本质是无限递归调用,结合代码场景大概率是以下两种情况之一:
    1. StorageItems类的amount属性get访问器、构造函数中意外调用了StorageList方法,每次执行StorageList、实例化StorageItems时重复触发自身调用,无限递归撑爆栈空间
    2. 当前类的类名与StorageList方法名重名,无返回值的同名方法会被识别为构造函数,若构造函数内存在重复实例化当前类的逻辑,也会触发栈溢出
修复方案
  1. 修正循环判断逻辑,避免手动维护计数器带来的越界风险
  2. 排查递归调用点,切断无限递归的链路
  3. 优先使用C#内置的遍历语法,减少手动写循环的出错概率
修正后的代码

兼容原while循环写法的修正版

public void StorageList()
{ 
    int counter = 0;
    List<StorageItems> storageitems = new List<StorageItems>();
    storageitems.Add(new StorageItems() { from_value = 40.0, to_value = 80.0, amount = 49.0 });
    storageitems.Add(new StorageItems() { from_value = 81.0, to_value = 200.0, amount = 63.0 });
    storageitems.Add(new StorageItems() { from_value = 201.0, to_value = 2000.0, amount = 126.0 });
    storageitems.Add(new StorageItems() { from_value = 2001.0, to_value = 4000.0, amount = 189.0 });
    storageitems.Add(new StorageItems() { from_value = 4001.0, to_value = 20000.0, amount = 252.0 });
    storageitems.Add(new StorageItems() { from_value = 20001.0, to_value = 40000.0, amount = 315.0 });
    storageitems.Add(new StorageItems() { from_value = 40001.0, to_value = 200000.0, amount = 472.0 });
    storageitems.Add(new StorageItems() { from_value = 200001.0, to_value = 200001.0, amount = 630.0 });

    // List<T>自带Count属性,比LINQ的Count()扩展方法执行效率更高
    int listrow = storageitems.Count;
    // 修正循环条件:仅当counter小于元素总数时执行,避免越界
    while (counter < listrow)
    {
        Console.WriteLine(storageitems[counter].amount);
        counter++;
    }
}

更安全无越界风险的foreach遍历版

public void StorageList()
{ 
    List<StorageItems> storageitems = new List<StorageItems>();
    storageitems.Add(new StorageItems() { from_value = 40.0, to_value = 80.0, amount = 49.0 });
    storageitems.Add(new StorageItems() { from_value = 81.0, to_value = 200.0, amount = 63.0 });
    storageitems.Add(new StorageItems() { from_value = 201.0, to_value = 2000.0, amount = 126.0 });
    storageitems.Add(new StorageItems() { from_value = 2001.0, to_value = 4000.0, amount = 189.0 });
    storageitems.Add(new StorageItems() { from_value = 4001.0, to_value = 20000.0, amount = 252.0 });
    storageitems.Add(new StorageItems() { from_value = 20001.0, to_value = 40000.0, amount = 315.0 });
    storageitems.Add(new StorageItems() { from_value = 40001.0, to_value = 200000.0, amount = 472.0 });
    storageitems.Add(new StorageItems() { from_value = 200001.0, to_value = 200001.0, amount = 630.0 });

    // 直接遍历列表元素,无需手动维护计数器,从根源避免越界
    foreach (var item in storageitems)
    {
        Console.WriteLine(item.amount);
    }
}

栈溢出排查提示:

  • 检查StorageItems类的amount属性定义,确认get访问器没有回调StorageList的逻辑
  • 若当前类名与StorageList方法重名,将方法改为其他语义化名称(如PrintStorageAmount),避免被识别为构造函数
  • 全局搜索StorageList()的调用点,确认没有循环调用、或者方法内部意外递归调用自身的代码

内容的提问来源于stack exchange,提问作者Tangub City Fire Station

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.30 00:45:08