获取列表中每个项目的位置

时间:2015-01-28 17:31:17

标签: c# list

如果我循环浏览列表中的项目,我如何获得每个项目的位置(如果有5个项目,则为1,2,3,4和5?

    public void AllocatePosition(List<DbConnect.BidList> createBidList)
    {
        for (int i = 0; i < createBidList.Count; i++)
        {
            MessageBox.Show(createBidList[i].ToString()); // show position of item (i.e. is it first, second, third...on the list
        }
    }

2 个答案:

答案 0 :(得分:1)

public void AllocatePosition(List<DbConnect.BidList> createBidList)
{
    for (int i = 0; i < createBidList.Count; i++)
    {
        MessageBox.Show("Position: " + i + " Item: " + createBidList[i].ToString());
    }
}

i变量包含位置。通常i用于for循环,传统上它代表“index”。

答案 1 :(得分:0)

看看

for (int i = 0; i < createBidList.Count; i++)

i是列表中项目的位置,除非我遗漏了某些内容。所以你想要

for (int i = 0; i < createBidList.Count; i++)
{
    MessageBox.Show(i.ToString()); // show position of item (i.e. is it first, second, third...on the list
}