Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.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# Can';我的数组循环不正确_C#_.net - Fatal编程技术网

C# Can';我的数组循环不正确

C# Can';我的数组循环不正确,c#,.net,C#,.net,我得到一个索引超出了数组的界限。 string[] paths = { "\\\\server\\c$\\folder\\subfolder\\user1\\300\\1\\abc.docx", "\\\\server\\c$\\folder\\subfolder\\user2\\400\\1\\xyz.docx", }; FileInfo[] f = ne

我得到一个
索引超出了数组的界限。

string[] paths = { 
                        "\\\\server\\c$\\folder\\subfolder\\user1\\300\\1\\abc.docx",
                        "\\\\server\\c$\\folder\\subfolder\\user2\\400\\1\\xyz.docx",
                    };

FileInfo[] f = new FileInfo[paths.Length];

for (int i = 0; i <= paths.Length; i++)
{
    f[i] = new FileInfo(paths[i]);
    Console.WriteLine(f[i].Length);
}
string[]路径={
“\\\\server\\c$\\folder\\subfolder\\user1\\300\\1\\abc.docx”,
“\\\\server\\c$\\folder\\subfolder\\user2\\400\\1\\xyz.docx”,
};
FileInfo[]f=newfileinfo[path.Length];

对于(int i=0;i使用
使用
您的数组中有两个项

paths[0], paths[1]
你在三个项目上的循环

paths[0], paths[1], paths[2]
要更正此问题,请更改

for (int i = 0; i <= paths.Length; i++)

for(int i=0;i数组中有两个项

paths[0], paths[1]
你在三个项目上的循环

paths[0], paths[1], paths[2]
要更正此问题,请更改

for (int i = 0; i <= paths.Length; i++)

for(int i=0;i数组从0开始计算iten。因此,如果数组的长度为2,则对象将位于位置[0]和[1]。如果尝试访问位置[2],则会得到索引超出数组异常的边界,因为此数组中不存在索引2


在for循环中,您正在使用数组从0开始计算iten。因此,如果您有一个长度为2的数组,则对象将位于位置[0]和[1]。如果尝试访问位置[2],您将得到索引超出数组异常的边界,因为此数组中不存在索引2


在for for循环中,您使用的是使用<代码>,如果您不需要索引<代码> i <代码>,您可以使用<代码>前缀而不是<代码> < <代码>以避免这样的错误。还考虑使用<代码>列表< /> >而不是<代码>数组<代码> >您的<代码>文件信息> /代码>使用<代码>,如果您不需要索引<代码> i>代码>实际上可以使用<代码>前缀而不是<代码> > <代码>以避免这样的错误。也考虑使用<代码>列表<代码>代替<代码>数组<代码> > <代码>文件信息< /代码>
for (int i = 0; i <= paths.Length; i++)
for (int i = 0; i < paths.Length; i++)