Index (zero based) must be greater than or equal to zero

后端 未结 8 854
灰色年华
灰色年华 2020-12-01 08:32

Hey I keep getting an error:

Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

相关标签:
8条回答
  • 2020-12-01 09:11
    using System;
    
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main()
            {
                Console.WriteLine("Enter Your FirstName ");
                String FirstName = Console.ReadLine();
    
                Console.WriteLine("Enter Your LastName ");
                String LastName = Console.ReadLine();
                Console.ReadLine();
    
                Console.WriteLine("Hello {0}, {1} ", FirstName, LastName);
                Console.ReadLine();
    
            }
        }
    }
    

    0 讨论(0)
  • 2020-12-01 09:11

    In my case I could not see the mistake "+name". The compiler would not report an error in this case. So take care.

    //Wrong Code:
    
    string name="my name";
    string age=25;
    String.Format(@"Select * from table where name='{1}' and age={1}" +name, age);
    
    
    //Right Code:
    
    string name="my name";
    string age=25;
    String.Format(@"Select * from table where name='{1}' and age={1}" , name, age);
    
    0 讨论(0)
  • 2020-12-01 09:12

    In this line:

    Aboutme.Text = String.Format("{2}", reader.GetString(0));
    

    The token {2} is invalid because you only have one item in the parms. Use this instead:

    Aboutme.Text = String.Format("{0}", reader.GetString(0));
    
    0 讨论(0)
  • 2020-12-01 09:16

    String.Format must start with zero index "{0}" like this:

    Aboutme.Text = String.Format("{0}", reader.GetString(0));
    
    0 讨论(0)
  • 2020-12-01 09:18

    Your second String.Format uses {2} as a placeholder but you're only passing in one argument, so you should use {0} instead.

    Change this:

    String.Format("{2}", reader.GetString(0));
    

    To this:

    String.Format("{0}", reader.GetString(2));
    
    0 讨论(0)
  • 2020-12-01 09:23

    This can also happen when trying to throw an ArgumentException where you inadvertently call the ArgumentException constructor overload

    public static void Dostuff(Foo bar)
    {
    
       // this works
       throw new ArgumentException(String.Format("Could not find {0}", bar.SomeStringProperty));
    
       //this gives the error
       throw new ArgumentException(String.Format("Could not find {0}"), bar.SomeStringProperty);
    
    }
    
    0 讨论(0)
提交回复
热议问题