当前位置:编程学习 > C#/ASP.NET >>

list的索引访问是什么意思?

List 可通过索引访问的对象的强类型列表。能够用索引器访问吗? --------------------编程问答-------------------- 这个是可以的。如List list=new List();list.Add(……)……;
list[1]^ --------------------编程问答--------------------

    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这个集合里面的值不同的,或者说没有值 --------------------编程问答-------------------- 索引访问意思是像数组一样可以用下标来访问, --------------------编程问答-------------------- 简单点说就是假如
在一个箱子里面放入了许多 编号不重复的小球
那么小球的编号就是索引 
简而言之就是 箱子是list 小球编号是索引  小球是结果 --------------------编程问答--------------------

            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]);
            }
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,