循环遍历列表会抛出超出界限的异常C#

Nau*_*nja 0 c# loops list

有人会帮我理解这个for循环有什么问题吗?为什么我会超出界限异常呢?

此特定列表的容量设置为8.

public static List<Beds> BedsList = new List<Beds>(8); 

private int GetFirstAvailableBed()
    {
        var result = 0;

        for (int i = 0; i < Beds.BedsList.Capacity; i++)
        {
            if (Beds.BedsList[i] == null) // Here is trhowing the exception
            {
                result = i;
                break;
            }
        }
        return result;
    }
Run Code Online (Sandbox Code Playgroud)

Jak*_*rtz 7

List.Count而不是List.Capacity.

Capacity物业

获取或设置内部数据结构在不调整大小的情况下可以保留的元素总数.

这不是列表中的项目数.