Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用list.Count()设置for循环错误的限制数量_C#_List_For Loop - Fatal编程技术网

C# 使用list.Count()设置for循环错误的限制数量

C# 使用list.Count()设置for循环错误的限制数量,c#,list,for-loop,C#,List,For Loop,我使用的是一个for循环,其声明的INT基于列表中的项目计数。我正在填充的列表是基于目录中的文件列表的动态列表。逻辑应该是,如果计数等于或超过1k条记录,则一次加载1k条记录,否则,如果计数较低,则对列表中的项目进行计数,并将其设置为for循环的最大值。但是for循环只运行了499次,并且出现错误(如图所示)。 有人知道如何解决发生的错误吗 下面是代码的核心,以便于理解 using System; using System.Collections.Generic; using System.D

我使用的是一个for循环,其声明的INT基于列表中的项目计数。我正在填充的列表是基于目录中的文件列表的动态列表。逻辑应该是,如果计数等于或超过1k条记录,则一次加载1k条记录,否则,如果计数较低,则对列表中的项目进行计数,并将其设置为for循环的最大值。但是for循环只运行了499次,并且出现错误(如图所示)。

有人知道如何解决发生的错误吗

下面是代码的核心,以便于理解

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.Threading;
using System.Linq;

namespace ViewProductImages
{
    public partial class Form1 : Form
    {
        

        public Form1()
        {
            InitializeComponent();
        }
             
                //new list
        List<string> filenames = new List<string>();          
       
        private void txtDirectory_TextChanged(object sender, EventArgs e)
        {
            
                //clear list - added peace of mind
            filenames.Clear();   

           
               //list is populated here via Directory.Getfiles


                //Sorted here kinda
                filenames.Sort();



        }
                 //Declare INT for for loop

        int maxImagesToShow;

        private void button1_Click(object sender, EventArgs e)
        {

                //Setting value for INT - IF over 1k records, set to 1k, IF under - set to actual list item count

            if (filenames.Count() >= 1000)
            {
                maxImagesToShow = 1000;
            }
            else
            {
                maxImagesToShow = filenames.Count();
            }

            try
            {

                //for loop - loops based on value in INT

                for (int i = 0; i < maxImagesToShow; i++)
                {                  
                    var filename = filenames[i];   

                   

                //remove current item being processed from the list(filenames)

                    filenames.Remove(filename);


                }

            }
            catch (Exception ex)
            {
                
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统图;
使用系统文本;
使用System.Windows.Forms;
使用System.Runtime.InteropServices;
使用System.IO;
使用系统线程;
使用System.Linq;
命名空间ViewProductImages
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
//新名单
列表文件名=新列表();
私有void txtDirectory_TextChanged(对象发送方,事件参数e)
{
//清晰列表-增加了内心的平静
Clear();
//列表在这里通过Directory.Getfiles填充
//在这里分类
Sort();
}
//为循环声明INT
int-maxImagesToShow;
私有无效按钮1\u单击(对象发送者,事件参数e)
{
//INT的设置值-如果记录超过1k,则设置为1k,如果记录不足,则设置为实际列表项计数
if(filenames.Count()>=1000)
{
最大stoshow=1000;
}
其他的
{
maxImagesToShow=filenames.Count();
}
尝试
{
//for循环-基于INT值的循环
对于(int i=0;i
您正在通过从列表中删除项目来减小列表的大小,但是
for
循环保持原始状态,最多可以看到1000个项目。如果删除循环末尾的列表中的项目,则列表中不会有1000项。您还将跳过项目,因为当您从列表的开头删除时,您会减少所有其他项目的索引。

这是否回答了您的问题?提示:假设列表中有5项,那么将从0到4进行迭代。然后调用
Remove
两次。。。列表中有多少项?删除前两个元素后,元素4是什么?(为什么要删除循环中的项目?我建议使用List.RemoveRange…)请记住在问题中包含错误消息及其堆栈跟踪作为文本。即使在屏幕截图中的对话框中,您也应该能够按CTRL+C并复制它。