List 可通过索引访问的对象的强类型列表。能够用索引器访问吗?

解决方案 »

  1.   

    这个是可以的。如List list=new List();list.Add(……)……;
    list[1]^
      

  2.   


        public Book()
            { }
            public Book(String name,String author)
            {
                this.author = author;
                this.name = name;
            }
            private String name;        public String Name
            {
                get { return name; }
                set { name = value; }
            }
            private String author;        public String Author
            {
                get { return author; }
                set { author = value; }
            }        public string this[int index]
            {
                get
                {
                    if (index == 0) return name;
                    else if (index == 1) return author;
                    else return null;
                }
                set
                {
                    if (index == 0) name = value;
                    else if (index == 1) author = value;
                }
            }
          public Help()
            {
                this.Book = new List<Book>();
                this.book.Add(new Book("三国","曹雪"));
                this.book.Add(new Book("西游","五成"));
                this.book.Add(new Book("红楼","曹"));
            }
            private List<Book> book;        internal List<Book> Book
            {
                get { return book; }
                set { book = value; }
            }
     
           static void Main(string[] args)
            {
                Help h = new Help();
                Console.WriteLine(h.Book[1].Name);
                Console.WriteLine(h.Book[0][0]);
                Console.ReadLine();
            }
    可以通过List这个集合去访问。如果直接访问是和list这个集合里面的值不同的,或者说没有值
      

  3.   

    简单点说就是假如
    在一个箱子里面放入了许多 编号不重复的小球
    那么小球的编号就是索引 
    简而言之就是 箱子是list 小球编号是索引  小球是结果
      

  4.   


                List<string> list = new List<string>();
                //添加5个string元素
                for (int i = 0; i < 5; i++)
                {
                    list.Add(i.ToString());
                }
                //Count是list集合的个数,用[index]来操作,记住,索引是从0开始的
                for (int i = 0; i < list.Count; i++)
                {
                    Console.WriteLine(list[i]);
                }