namespace test1
{
    class text5
    {
       
        static void Main(string[] args)
        {
            List<person> prs = new List<person>();
            for (int i = 0; i < 5; i++)
            {
                prs[i].num[i] = i;//在这个位置出现这个错误,嵌套使用索引超出范围。必须为非负值并小于集合大小。
            }
            for (int i = 0; i < prs.Count;i++ )
            {
                Console.WriteLine(prs[i].num[i]);
            }
            Console.Read();
        }
        
    }
    class person
    {
       public  string name;
       public List<int> num = new List<int>();
    }
}
请各位大侠给予帮助

解决方案 »

  1.   

    List<person> prs = new List<person>();
    这个直接就是空的,哪里来的i给你调用啊
      

  2.   

    你得先往集合里Add了数据才能用索引取值啊
      

  3.   


    namespace test1
    {
      class text5
      {
        
      static void Main(string[] args)
      {
      List<person> prs = new List<person>();
      
       
      for (int i = 0; i < 5; i++)
      {
       prs.Add(new person());
       prs[i].num[i] = i;
      }
      for (int i = 0; i < prs.Count;i++ )
      {
      Console.WriteLine(prs[i].num[i]);
      }
      Console.Read();
      }
        
      }
      class person
      {
       public person(0)
       {
          for(int i = 0; i < 5; i++) num.Add(0);
        }
      public string name;
      public List<int> num = new List<int>();
      }
    }
      

  4.   


    class person
      {
       public person()
       {
          for(int i = 0; i < 5; i++) num.Add(0);
        }
      public string name;
      public List<int> num = new List<int>();
      }