C#里的Index是什么意思?

来源:学生作业帮助网 编辑:作业帮 时间:2024/04/29 18:27:21
C#里的Index是什么意思?

C#里的Index是什么意思?
C#里的Index是什么意思?

C#里的Index是什么意思?
Index是类的一个索引符,C#对类引进了索引符的操作,是为了有时候可以将类当作数组来用,如下所示:
class Person{private string name;
private string sex;
private string title;
public string this[int i]{get{switch(i){case 0:return name;case 1:return sex;case 2:return title;defualt:throw new IndexOutRangeException();}set{switch(i){case 0:name=value;break;case 1:sex=value;break;case 2:title=value;break;default:throw new IndexOutOfException();}}}}
定义了索引符之后,我们就可以像这样使用类了: