码迷,mamicode.com
首页 > 编程语言 > 详细

C#语言进阶——3.C# 的索引器

时间:2016-11-27 06:37:27      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:iso   索引   stat   code   没有   console   i++   字符串   get   

简述:索引器为c#程序语言中类的一种成员,它使得对象可以像数组一样被索引,使程序看起来更直观,更容易编写。

1.C# 索引器的语法:介绍 C# 语言中的索引器的语法,如何建立一个索引器,以及索引器在语义上的正确使用。

 1 class Program
 2     {
 3 
 4         private const string FILE_NAME = "Test.txt";  //文件名
 5         static void Main(string[] args)
 6         {
 7             var names = new IndexedNames();
 8             names[0] = "hello 1";
 9             names[1] = "hello 2";
10             names[2] = "hello 3";
11             names[3] = "hello 4";
12             names[4] = "hello 5";
13             names[5] = "hello 6";
14             names[6] = "hello 7";
15             names[7] = "hello 8";
16             names[8] = "hello 9";
17             names[9] = "hello 10";
18 
19             for (int i = 0; i < 9; i++)
20             {
21                 Console.WriteLine(names[i]);
22             }
23             Console.ReadLine();
24         }
25 
26         class IndexedNames
27         {
28             private string[] nameList = new string[10];
29             public IndexedNames()
30             {
31                 for (int i = 0; i < nameList.Length; i++)
32                 {
33                     nameList[i] = "N/A";
34                 }
35             }
36 
37             /// <summary>
38             /// 建立索引
39             /// </summary>
40             /// <param name="index">索引</param>
41             /// <returns></returns>
42             public string this[int index]
43             {
44                 get
45                 {
46                     string tmp; //临时变量
47                     if (index >= 0 && index <= nameList.Length - 1)//有没有超过他的长度或者是负数的
48                     {
49                         tmp = nameList[index];
50                     }
51                     else
52                     {
53                         tmp = "";// 不合法的话就返回为空
54                     }
55                     return tmp;
56                 }
57                 set
58                 {
59                     if (index >= 0 && index <= nameList.Length - 1)
60                     {
61                         nameList[index] = value;
62                     }
63                 }
64             }
65         }
66 
67     }

 

2.C# 索引器的重载:介绍 C# 语言中如何基于字符串建立索引器,同时实现了索引器的重载。

 1     class Program
 2     {
 3 
 4         private const string FILE_NAME = "Test.txt";  //文件名
 5         static void Main(string[] args)
 6         {
 7             var names = new IndexedNames();
 8             names[0] = "hello 1";
 9             names[1] = "hello 2";
10             names[2] = "hello 3";
11             names[3] = "hello 4";
12             names[4] = "hello 5";
13             names[5] = "hello 6";
14             names[6] = "hello 7";
15             names[7] = "hello 8";
16             names[8] = "hello 9";
17             names[9] = "hello 10";
18 
19             for (int i = 0; i < 9; i++)
20             {
21                 Console.WriteLine(names[i]);
22             }
23             //可以根据整行进行索引页可以通过字符串进行索引,这样就叫做重载,根据类型或者名称区别
24             //可以进行多维的数组进行索引,但并不多见;
25             Console.WriteLine(names["hello 1"]);
26             Console.WriteLine(names["55555"]);
27 
28             Console.ReadLine();
29         }
30 
31         class IndexedNames
32         {
33             private string[] nameList = new string[10];
34             public IndexedNames()
35             {
36                 for (int i = 0; i < nameList.Length; i++)
37                 {
38                     nameList[i] = "N/A";
39                 }
40             }
41 
42             /// <summary>
43             /// 建立索引(整数)
44             /// </summary>
45             /// <param name="index">索引</param>
46             /// <returns></returns>
47             public string this[int index]
48             {
49                 get
50                 {
51                     string tmp; //临时变量
52                     if (index >= 0 && index <= nameList.Length - 1)//有没有超过他的长度或者是负数的
53                     {
54                         tmp = nameList[index];
55                     }
56                     else
57                     {
58                         tmp = "";// 不合法的话就返回为空
59                     }
60                     return tmp;
61                 }
62                 set
63                 {
64                     if (index >= 0 && index <= nameList.Length - 1)
65                     {
66                         nameList[index] = value;
67                     }
68                 }
69             }
70             /// <summary>
71             /// 建立索引(字符串)
72             /// </summary>
73             /// <param name="name"></param>
74             /// <returns></returns>
75             public int this[string name]
76             {
77                 get
78                 {
79                     int index = 0;
80                     while (index < nameList.Length)
81                     {
82                         if (nameList[index] == name)
83                         {
84                             return index;
85                         }
86                         index++;
87                     }
88                     //如果数组里面返回-1
89                     return -1;
90                 }
91 
92             }
93 
94         }
95 
96     }

3.C# 基于接口的索引器和代码强壮性:介绍 C# 语言中如何基于接口 interface 建立索引器,以及关于如何编写强壮的代码进行讨论。

 1  //接口
 2     public interface ISomeInterface
 3     {
 4         int this[int index]
 5         {
 6             get;
 7             set;
 8         }
 9     }
10     class IndexerClass : ISomeInterface
11     {
12         private int[] arr = new int[100];
13         /// <summary>
14         /// 索引器的具体实现
15         /// </summary>
16         /// <param name="index"></param>
17         /// <returns></returns>
18         public int this[int index]
19         {
20             //没有合理的防错误机制
21             get
22             {
23                 return arr[index];
24             }
25              set 
26             {
27                 arr[index] = value;
28             }
29         }
30     }

 

C#语言进阶——3.C# 的索引器

标签:iso   索引   stat   code   没有   console   i++   字符串   get   

原文地址:http://www.cnblogs.com/lilb/p/6105324.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!