English 中文(简体)
C# Index Out Of range Statelessness [duplicate]
原标题:C# Index Out Of Range Exception [duplicate]

我似乎对2008年C#有问题。 我正在制定一项简单的方案,在具体文件夹中列出所有档案的清单。 我选择试验Windows 夹中的系统文档。 它列出了档案清单,然后出现例外。 该守则是:

if (EnterNumber == "1")
{
    Console.WriteLine("Files");
    DirectoryInfo folderInfo = new DirectoryInfo("F:\WINDOWS");
    FileInfo[] Files = folderInfo.GetFiles();

    String UserChoice = Console.ReadLine();


    for (int index = 0; index < Files.Length; index++)
    {


        Console.WriteLine("{0}, {1} ({2})", index++, Files[index].Name, Files[index].Length);
    }
    Console.Write("Return To Main Menu?: ");
    if (UserChoice == "y")
    { 

因此,用户将第1号报到,以显示档案和档案。 该网站在Windows 夹中展示文档。 但是,你能否看到奥塞罗语与一些信息相符合? 一条线似乎带有错误的信息。 出现例外的情况是,该指数不属于阵列的范围。 我知道有哪一个阵列,但我有一个适用这一信息的问题。 如果你能够告诉我如何消除这一错误,我将不胜感激。 因此,档案通常显示,无论清单是多长时间的。 此外,是否允许用户清除屏幕并返回主菜? 我已尝试了明确的职能,但我是否应当补充允许用户再次投入其选择的发言?

问题回答
        for (int index = 0; index < Files.Length; index++)
        {
            Console.WriteLine("{0}, {1} ({2})", index++, Files[index].Name, Files      [index].Length);
        }

问题出现在你的文字上,你正在再次增加指数。 我只是将它改为索引而不是索引++。

变化:

Console.WriteLine("{0}, {1} ({2})", index++, Files[index].Name, Files[index].Length);

:

Console.WriteLine("{0}, {1} ({2})", index, Files[index].Name, Files[index].Length);

,在您的休息室里被两度加插,当时你可能只想去一次。





相关问题
handling exceptions IN Action Filters

Is there a better way to handle exceptions that occur inside an Action Filter itself in ASP .NET MVC? There re 2 ways I can think of at the moment. Using a try catch and setting the HTTP Status ...

既可捕获,又可举出例外。

我有一种办法,可以进入亚洲开发银行,因此,我国的亚行在多瑙河航道中的所有 st子都位于一个试捕区。 它正在追捕Kexception

Cross compiler exception handling - Can it be done safely?

I am doing some maintenance on a C++ windows dll library that is required to work with different VC++ compilers (as I don’t want to address different mangling schemes). I have already eliminated any ...

File Handling Issue

I am developing a tool in c#, at one instance I start writing into a xml file continuously using my tool,when i suddenly restart my machine the particular xml file gets corrupted, what is the reason ...

Watch a memory location/install data breakpoint from code?

We have a memory overwrite problem. At some point, during the course of our program, a memory location is being overwritten and causing our program to crash. the problem happens only in release mode. ...

Unit Test for Exceptions Message

Is there a simple (Attribute-driven) way to have the following test fail on the message of the exception. [TestMethod()] [ExpectedException(typeof(ArgumentException))] public void ExceptionTestTest() ...

热门标签