c#索引器如何定义不同的访问方式
3个回答
展开全部
关键代码:
//基本索引器 根据数组下标查找
public Student this[int index]
{
get { return students[index]; }
}
//重载的索引器 根据姓名查找
public Student this[string name]
{
get{
bool found = false;
for (int i = 0; i < students.Length; i++)
{
if (students[i].Name == name)
{
found = true;
break;
}
}
if (found)
{
return students[i];
}
else
{
return null;
}
}
}
//基本索引器 根据数组下标查找
public Student this[int index]
{
get { return students[index]; }
}
//重载的索引器 根据姓名查找
public Student this[string name]
{
get{
bool found = false;
for (int i = 0; i < students.Length; i++)
{
if (students[i].Name == name)
{
found = true;
break;
}
}
if (found)
{
return students[i];
}
else
{
return null;
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询