using System;
namespace IndexerApplication{
//1.类成员变量是数组的情况
// class IndexedNames{
// private string[] namelist = new string[size];
// static public int size = 10;
// public IndexedNames(){
// for(int i = 0; i < size; i++){
// namelist[i] = "N.A.";
// }
// }
// 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;
// }
// }
// }
// static void Main(string[] args){
// IndexedNames names = new IndexedNames();
// names[0] = "Zara";
// names[1] = "Riz";
// names[2] = "Nuha";
// names[3] = "Asif";
// names[4] = "Davinder";
// names[5] = "Sunil";
// names[6] = "Rubic";
// for(int i = 0; i < IndexedNames.size; i++){
// Console.WriteLine(names[i]);
// }
// }
// }
//2.类成员变量是多个基本类型
class Employee{
public string firstName;
public string middleName;
public string lastName;
public string this[string index]{
set{
switch(index){
case "a":firstName = value;
break;
case "b":middleName = value;
break;
case "c":lastName = value;
break;
default: throw new ArgumentOutOfRangeException("index");
}
}
get{
switch(index){
case "a":return firstName;
case "b":return middleName;
case "c":return lastName;
default: throw new ArgumentOutOfRangeException("index");
}
}
}
static void Main(string[] args){
Employee ee = new Employee();
ee.firstName = "胡";
ee.middleName = "大";
ee.lastName = "阳";
Console.WriteLine("我的名字叫: {0}{1}{2}",ee["a"],ee["b"],ee["c"]);
}
}
}二狗子 二狗子
137***6461@qq.com
7年前 (2018-12-06)