您好,欢迎访问一九零五行业门户网

c#属性和索引器

1、属性
所谓属性其实就是特殊的类成员,它实现了对私有类域的受控访问。在c#语言中有两种属性方法,其一是get,通过它可以返回私有域的值,其二是set,通过它就可以设置私有域的值。比如说,以下面的代码为例,创建学生姓名属性,控制对name字段的受控访问:
using system; public class student { private string name; /// <summary> /// 定义学生的姓名属性 /// </summary> public string name { get { return name; } set { name = value; } } } class program { static void main(string[] args) { student student = new student(); student.name = "jeff wong"; console.writeline(student.name); console.read(); } }
2、索引器
简单说来,所谓索引器就是一类特殊的属性,通过它们你就可以像引用数组一样引用自己的类。显然,这一功能在创建集合类的场合特别有用,而在其他某些情况下,比如处理大型文件或者抽象某些有限资源等,能让类具有类似数组的行为当然也是非常有用的。比如,上例中,我们假设一个班级有若干个学生,构建索引器就可以很方便地调用:
using system; using system.collections.generic; public class student { public list<student> liststudents = new list<student>(); /// <summary> /// 构建索引器 /// </summary> /// <param name="i"></param> /// <returns></returns> public student this[int i] { get { return liststudents[i]; } set { liststudents[i] = value; } } private string name; /// <summary> /// 属性 /// </summary> public string name { get { return name; } set { name = value; } } public student(string name) { this.name = name; } public student() { this.liststudents.add(new student("jeff wong")); this.liststudents.add(new student("jeffery zhao")); this.liststudents.add(new student("terry lee")); this.liststudents.add(new student("dudu")); } } class program { static void main(string[] args) { student student = new student(); int num = student.liststudents.count; console.writeline("all the students:"); for (int i = 0; i < num; i++) { console.writeline(student[i].name); //通过索引器,取所有学生名 } //设置索引器的值 student[0].name = "jeff"; console.writeline("after modified,all the students:"); for (int i = 0; i < num; i++) { console.writeline(student[i].name); } console.read(); } }
上面代码中,我们看到索引器的访问器带一个参数(参数为整数),其实可以构建多个参数的索引器。还以上述代码为例,我们要根据学生学号和姓名得到学生的考试总分,修改后代码如下:
using system; using system.collections.generic; public class student { public list<student> liststudents = new list<student>(); public student this[int i,string name] { get { foreach (student stu in liststudents.toarray()) { if (stu.sid == i && stu.name == name) //按照学号和姓名取出学生 { return stu; } } return null; } set { liststudents[i] = value; } } private int sid; //学号 public int sid { get { return sid; } set { sid = value; } } private string name;//姓名 public string name { get { return name; } set { name = value; } } private int score; //总分 public int score { get { return score; } set { score = value; } } public student(int sid, string name, int score) { this.sid = sid; this.name = name; this.score = score; } public student() { this.liststudents.add(new student(1, "jeff wong", 375)); this.liststudents.add(new student(2,"jeffery zhao",450)); this.liststudents.add(new student(3,"terry lee",400)); this.liststudents.add(new student(4,"dudu",500)); } } class program { static void main(string[] args) { student student = new student(); student stu = student[1, "jeff wong"]; console.writeline("student number:" + stu.sid + ",name:" + stu.name + ",score:" + stu.score); console.read(); } }
3、总结:
<1>、
属性的定义:
访问修饰符 返回类型 属性名

get{语句集合}
set{语句集合}

索引器的定义:
访问修饰符 返回类型 this[参数类型 参数...]

get{语句集合}
set{语句集合}

<2>、
索引器使得对象可按照与数组相似的方法进行索引。
this 关键字用于定义索引器。
get 访问器返回值。set 访问器分配值。
value 关键字用于定义由 set 索引器分配的值。
索引器不必根据整数值进行索引,由你决定如何定义特定的查找机制。
索引器可被重载。
<3>、属性和索引器的主要区别:
a、类的每一个属性都必须拥有唯一的名称,而类里定义的每一个索引器都必须拥有唯一的签名(signature)或者参数列表(这样就可以实现索引器重载)。
b、属性可以是static(静态的)而索引器则必须是实例成员。
<4>、索引器重载实例:
using system; using system.collections.generic; public class student { public list<student> liststudents = new list<student>(); public student this[int i,string name] { get { foreach (student stu in liststudents.toarray()) { if (stu.sid == i && stu.name == name) //按照学号和姓名取出学生 { return stu; } } return null; } set { liststudents[i] = value; } } /// <summary> /// 索引器重载 /// </summary> /// <param name="i"></param> /// <returns></returns> public student this[int i] //i从0开始 { get { return liststudents[i]; } set { liststudents[i] = value; } } private int sid; //学号 public int sid { get { return sid; } set { sid = value; } } private string name;//姓名 public string name { get { return name; } set { name = value; } } private int score; //总分 public int score { get { return score; } set { score = value; } } public student(int sid, string name, int score) { this.sid = sid; this.name = name; this.score = score; } public student() { this.liststudents.add(new student(1, "jeff wong", 375)); this.liststudents.add(new student(2,"jeffery zhao",450)); this.liststudents.add(new student(3,"terry lee",400)); this.liststudents.add(new student(4,"dudu",500)); } } class program { static void main(string[] args) { student student = new student(); student stu = student[1, "jeff wong"]; console.writeline("student number:" + stu.sid + ",name:" + stu.name + ",score:" + stu.score); console.writeline("all the students:"); for (int i = 0; i < student.liststudents.count; i++) { console.writeline("student number:" + student[i].sid + ",name:" + student[i].name + ",score:" + student[i].score); } console.read(); } }
更多c#属性和索引器。
其它类似信息

推荐信息