验证中...
Languages: C#
Categories: 编程语言基础
Latest update 2018-12-09 10:33
基本用法
Raw Copy
//定义元素存储数组和容量
private string[] namelist = new string[size];
static public int size = 10;
public string this[int index]
{
get
{
string tmp;
if (index >= 0 && index <= size - 1)
{
tmp = namelist[index];
}
else
{
tmp = "";
}
return (tmp);
}
set
{
if (index >= 0 && index <= size - 1)
{
namelist[index] = value;
}
}
}
IndexedNames names = new IndexedNames();
names[0] = "Zara";
names[1] = "Riz";
names[2] = "Nuha";
names[3] = "Asif";
索引器重载
Raw Copy
//索引器通过[名字&课程编号]查找和保存成绩
public int this[string stuName, int courseId]
{
get
{
}
set
{
}
}
//索引器重载,根据[名字]查找所有成绩
public List<Scores> this[string stuName]
{
get
{
List<Scores> tempList = listScores.FindAll(x => x.StuName == stuName);
return tempList;
}
}
//多参数索引器和索引器重载
FindScore fScore = new FindScore();
fScore["张三", 1] = 98;
fScore["张三", 2] = 100;
fScore["张三", 3] = 95;
fScore["李四", 1] = 96;
//查找所有张三的成绩
List<Scores> listScores = fScore["张三"];

Comment list( 0 )

Sign in for post a comment

Help Search