C#索引器(Indexers)---逐步解析与理解(二)

继续前面一篇文章

在Employee类中为get和set存取器创建索引。完整的Employee类如下,这里,我们通过索引位置创建了一个索引,这样我们就能通过整型索引位置存取元素。在set存取器中,参数“value”是隐式保存赋值的。

 1 using System;
 2 namespace IndexersDemo
 3 {
 4     public class Employee
 5     {
 6         //Declare the properties
 7         public int ID { get; set; }
 8         public string Name { get; set; }
 9         public string Job { get; set; }
10         public double Salary { get; set; }
11         public string Location { get; set; }
12         public string Department { get; set; }
13         public string Gender { get; set; }
14 
15         //Initialize the properties through constructor
16         public Employee(int ID, string Name, string Job, int Salary, string Location, 
17                         string Department, string Gender)
18         {
19             this.ID = ID;
20             this.Name = Name;
21             this.Job = Job;
22             this.Salary = Salary;
23             this.Location = Location;
24             this.Department = Department;
25             this.Gender = Gender;
26         }
27 
28         public object this [int index]
29         {
30             //The get accessor is used for returning a value
31             get
32             {
33                 if (index == 0)
34                     return ID;
35                 else if (index == 1)
36                     return Name;
37                 else if (index == 2)
38                     return Job;
39                 else if (index == 3)
40                     return Salary;
41                 else if (index == 4)
42                     return Location;
43                 else if (index == 5)
44                     return Department;
45                 else if (index == 6)
46                     return Gender;
47                 else
48                     return null;
49             }
50             
51             // The set accessor is used to assigning a value
52             set
53             {
54                 if (index == 0)
55                     ID = Convert.ToInt32(value);
56                 else if (index == 1)
57                     Name = value.ToString();
58                 else if (index == 2)
59                     Job = value.ToString();
60                 else if (index == 3)
61                     Salary = Convert.ToDouble(value);
62                 else if (index == 4)
63                     Location = value.ToString();
64                 else if (index == 5)
65                     Department = value.ToString();
66                 else if (index == 6)
67                     Gender = value.ToString();
68             }
69         }
70     }
71 }

现在,像数组一样存取这些数值元素,同时,也可以像数组一样修改这些数值元素。

  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[int index]
 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     }
 72 
 73     class Program
 74     {
 75         static void Main(string[] args)
 76         {
 77             //Creating the Employee instance
 78             Employee emp = new Employee(101, "Pranaya", "SSE", 10000, "Mumbai", "IT", "Male");
 79 
 80             //Accessing Employee Properties using Indexers i.e. using Index positions
 81             Console.WriteLine("EID = " + emp[0]);
 82             Console.WriteLine("Name = " + emp[1]);
 83             Console.WriteLine("Job = " + emp[2]);
 84             Console.WriteLine("Salary = " + emp[3]);
 85             Console.WriteLine("Location = " + emp[4]);
 86             Console.WriteLine("Department = " + emp[5]);
 87             Console.WriteLine("Gender = " + emp[6]);
 88 
 89             emp[1] = "Kumar";
 90             emp[3] = 65000;
 91             emp[5] = "BBSR";
 92 
 93             Console.WriteLine("=======Afrer Modification=========");
 94             Console.WriteLine("EID = " + emp[0]);
 95             Console.WriteLine("Name = " + emp[1]);
 96             Console.WriteLine("Job = " + emp[2]);
 97             Console.WriteLine("Salary = " + emp[3]);
 98             Console.WriteLine("Location = " + emp[4]);
 99             Console.WriteLine("Department = " + emp[5]);
100             Console.WriteLine("Gender = " + emp[6]);
101             Console.ReadLine();
102         }
103     }
104 }

运行程序,输出结果如下:

 

posted @ 2022-02-14 22:39  chenlight  阅读(61)  评论(0编辑  收藏  举报