逻辑上低级错误

本文探讨了两个编程问题:1. List操作时索引越界错误,导致输出失败;2. 处理递归异常时引发的内存溢出,焦点在于异常捕获和集合操作的最佳实践。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.集合

public static void Show(List<string> names)
{
    if (names == null && names.Count < 1)
    {
        Console.WriteLine("集合中没有数据,输出失败!!!");
    }
    Console.WriteLine(names[0]);
}

public static void Test()
{
    List<string> names = new List<string>();
    Show(names);
}
索引超出范围。必须为非负值并小于集合大小。
参数名: index
   在 System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
   在 System.Collections.Generic.List`1.get_Item(Int32 index)
   在 xxx.xxxx.xxx.xxx(xxx xxx) 位置 xxx.xxx.xxx.cs:行号 30

2.异常

Exception tempEx = null;
while ((tempEx = ex.InnerException) != null)
{
    exceptions.Add(tempEx);
}
引发类型为“System.OutOfMemoryException”的异常。
   在 System.Collections.Generic.List`1.set_Capacity(Int32 value)
   在 System.Collections.Generic.List`1.EnsureCapacity(Int32 min)
   在 System.Collections.Generic.List`1.Add(T item)
   在 xxx.xxxx.xxx.xxx(xxx xxx) 位置 xxx.xxx.xxx.cs:行号 30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值