List 可通过索引访问的对象的强类型列表。能够用索引器访问吗?
解决方案 »
- 关于VS三个版本间的疑问
- C#的高手们,谁能给我个关于SOCKET不错的教程啊,我比较急啊...
- CommandArgument绑定的值可不可以在页面间传递?
- 请问如何编程控制本地连接或网卡的开启和关掉
- 100分紧急求助:寻求SQL语句:如何查询从当前日期开始,往后退12个小时的数据啊?
- 为什么C#中调用远程数据库中的DTS来导入数据会失败?急啊!!!可以再加分
- 泛型类型不能用于类的构造函数里吗?为什么?
- 急!急!急!
- 初学乍练c#,高分求教两个问题
- 大家用了这么久的C#,现在感觉怎么样了?
- winform 中的datagridview控件
- 日历,Microsoft JScript 运行时错误: 'outObject.value' 为空或不是对象
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]);
}