C#索引器(Indexers)---逐步解析与理解(三)
但是,在现实中,我们可能有更多的大量类型属性,很难通过使用整型索引位置来存取数值元素。所以在很多情况下,我们需要通过使用属性名称来存取数值元素。为达到这个目的,我们需要使用string名称取代int索引。用string名称做为索引,并修改Employee类:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace IndexersDemo 7 { 8 public class Employee 9 { 10 //Declare the properties 11 public int ID { get; set; } 12 public string Name { get; set; } 13 public string Job { get; set; } 14 public double Salary { get; set; } 15 public string Location { get; set; } 16 public string Department { get; set; } 17 public string Gender { get; set; } 18 19 //Initialize the properties through constructor 20 public Employee(int ID, string Name, string Job, int Salary, string Location, 21 string Department, string Gender) 22 { 23 this.ID = ID; 24 this.Name = Name; 25 this.Job = Job; 26 this.Salary = Salary; 27 this.Location = Location; 28 this.Department = Department; 29 this.Gender = Gender; 30 } 31 32 public object this[string Name] 33 { 34 //get 35 //{ 36 // if (index == 0) 37 // return ID; 38 // else if (index == 1) 39 // return Name; 40 // else if (index == 2) 41 // return Job; 42 // else if (index == 3) 43 // return Salary; 44 // else if (index == 4) 45 // return Location; 46 // else if (index == 5) 47 // return Department; 48 // else if (index == 6) 49 // return Gender; 50 // else 51 // return null; 52 //} 53 //set 54 //{ 55 // if (index == 0) 56 // ID = Convert.ToInt32(value); 57 // else if (index == 1) 58 // Name = value.ToString(); 59 // else if (index == 2) 60 // Job = value.ToString(); 61 // else if (index == 3) 62 // Salary = Convert.ToDouble(value); 63 // else if (index == 4) 64 // Location = value.ToString(); 65 // else if (index == 5) 66 // Department = value.ToString(); 67 // else if (index == 6) 68 // Gender = value.ToString(); 69 //} 70 71 get 72 { 73 if (Name == "ID") 74 return ID; 75 else if (Name == "Name") 76 return Name; 77 else if (Name == "Job") 78 return Job; 79 else if (Name == "Salary") 80 return Salary; 81 else if (Name == "Location") 82 return Location; 83 else if (Name == "Department") 84 return Department; 85 else if (Name == "Gender") 86 return Gender; 87 else 88 return null; 89 } 90 set 91 { 92 if (Name == "ID") 93 ID = Convert.ToInt32(value); 94 else if (Name == "Name") 95 Name = value.ToString(); 96 else if (Name == "Job") 97 Job = value.ToString(); 98 else if (Name == "Salary") 99 Salary = Convert.ToDouble(value); 100 else if (Name == "Location") 101 Location = value.ToString(); 102 else if (Name == "Department") 103 Department = value.ToString(); 104 else if (Name == "Gender") 105 Gender = value.ToString(); 106 } 107 } 108 } 109 110 class Program 111 { 112 static void Main(string[] args) 113 { 114 //Creating the Employee instance 115 Employee emp = new Employee(101, "Pranaya", "SSE", 10000, "Mumbai", "IT", "Male"); 116 117 //Accessing Employee Properties using Indexers i.e. using Index positions 118 //Console.WriteLine("EID = " + emp[0]); 119 //Console.WriteLine("Name = " + emp[1]); 120 //Console.WriteLine("Job = " + emp[2]); 121 //Console.WriteLine("Salary = " + emp[3]); 122 //Console.WriteLine("Location = " + emp[4]); 123 //Console.WriteLine("Department = " + emp[5]); 124 //Console.WriteLine("Gender = " + emp[6]); 125 126 Console.WriteLine("EID = " + emp["ID"]); 127 Console.WriteLine("Name = " + emp["Name"]); 128 Console.WriteLine("Job = " + emp["Job"]); 129 Console.WriteLine("Salary = " + emp["Salary"]); 130 Console.WriteLine("Location = " + emp["Location"]); 131 Console.WriteLine("Department = " + emp["Department"]); 132 Console.WriteLine("Gender = " + emp["Gender"]); 133 134 //emp[1] = "Kumar"; 135 //emp[3] = 65000; 136 //emp[5] = "BBSR"; 137 138 emp["Name"] = "Kumar"; 139 emp["Salary"] = 65000; 140 emp["Location"] = "BBSR"; 141 142 Console.WriteLine("=======Afrer Modification========="); 143 //Console.WriteLine("EID = " + emp[0]); 144 //Console.WriteLine("Name = " + emp[1]); 145 //Console.WriteLine("Job = " + emp[2]); 146 //Console.WriteLine("Salary = " + emp[3]); 147 //Console.WriteLine("Location = " + emp[4]); 148 //Console.WriteLine("Department = " + emp[5]); 149 //Console.WriteLine("Gender = " + emp[6]); 150 151 Console.WriteLine("EID = " + emp["ID"]); 152 Console.WriteLine("Name = " + emp["Name"]); 153 Console.WriteLine("Job = " + emp["Job"]); 154 Console.WriteLine("Salary = " + emp["Salary"]); 155 Console.WriteLine("Location = " + emp["Location"]); 156 Console.WriteLine("Department = " + emp["Department"]); 157 Console.WriteLine("Gender = " + emp["Gender"]); 158 Console.ReadLine(); 159 160 } 161 } 162 }
运行结果如上述所示 ,同时,我们应该注意到,索引器是区分大小写的(case-sensitive);如果没有对应一致 ,结果就不会显示出来。