C#语言知识点整理 - 索引
2013-03-27 16:51
344 查看
一、 索引器定义:
索引器允许类或结构的实例就像数组一样进行索引。
二、 索引器使用
索引器经常是在主要用于封装内部集合或数组的类型中实现的。
C# 并不将索引类型限制为整数
三、 接口索引器与类索引器的区别:
接口访问器不使用修饰符。
接口访问器没有体。
四、 索引器与属性的区别:
索引器与属性类似。 除下表中显示的差别外,为属性访问器定义的所有规则同样适用于索引器访问器。
五、 索引器示例:
本文出自 “快乐分享” 博客,请务必保留此出处http://yongx.blog.51cto.com/1835267/1163789
索引器允许类或结构的实例就像数组一样进行索引。
二、 索引器使用
索引器经常是在主要用于封装内部集合或数组的类型中实现的。
C# 并不将索引类型限制为整数
三、 接口索引器与类索引器的区别:
接口访问器不使用修饰符。
接口访问器没有体。
四、 索引器与属性的区别:
索引器与属性类似。 除下表中显示的差别外,为属性访问器定义的所有规则同样适用于索引器访问器。
属性 | 索引器 |
允许像调用公共数据成员一样调用方法。 | 允许对一个对象本身使用数组表示法来访问该对象内部集合中的元素。 |
可通过简单的名称进行访问。 | 可通过索引器进行访问。 |
可以为静态成员或实例成员。 | 必须为实例成员。 |
属性的 get 访问器没有参数。 | 索引器的 get 访问器具有与索引器相同的形参表。 |
属性的 set 访问器包含隐式 value 参数。 | 除了值参数外,索引器的 set 访问器还具有与索引器相同的形参表。 |
支持对使用短语法。 | 不支持短语法。 |
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5: using System.Collections.Specialized;
6:
7: namespace CSharp.Indexer
8: {
9: public class Employee
10: {
11: private string _name = "";
12:
13: public string Name
14: {
15: get { return _name; }
16: set { _name = value; }
17: }
18:
19: public Employee(string name)
20: {
21: this._name = name;
22: }
23: }
24:
25: public interface IEmployeeInterface
26: {
27: //int Indexer declaration
28: Employee this[int index]
29: {
30: set;
31: }
32:
33: //string indexer declaration
34: Employee this[string name]
35: {
36: get;
37: set;
38: }
39: }
40:
41: public class EmployeeList : IEmployeeInterface
42: {
43: private ListDictionary empDictionary;
44:
45: public EmployeeList()
46: {
47: empDictionary = new ListDictionary();
48: }
49:
50: // The int indexer.
51: public Employee this[int item]
52: {
53: set
54: {
55: if (value != null && !string.IsNullOrEmpty(value.Name))
56: {
57: empDictionary.Add(value.Name, value);
58: }
59: }
60: }
61:
62: // The string indexer.
63: public Employee this[string name]
64: {
65: get { return (Employee)empDictionary[name]; }
66: set { empDictionary.Add(name, value); }
67: }
68: }
69:
70: class Program
71: {
72: static void Main(string[] args)
73: {
74: EmployeeList empList = new EmployeeList();
75:
76: empList[0] = new Employee("david");
77: empList[1] = new Employee("lisa");
78: empList[2] = new Employee("nana");
79:
80: empList["alice"] = new Employee("alice");
81: empList["sam"] = new Employee("sam");
82:
83: Employee alice = empList["alice"];
84: Console.WriteLine("Alice 's name is {0}", alice.Name);
85: Employee nana = empList["nana"];
86: Console.WriteLine("Nana 's name is {0}", nana.Name);
87:
88: Console.ReadLine();
89: }
90: }
91: }
本文出自 “快乐分享” 博客,请务必保留此出处http://yongx.blog.51cto.com/1835267/1163789
相关文章推荐
- C#语言知识点整理 - 结构
- C#语言知识点整理 - 事件
- C#语言知识点整理 - 委托
- C#知识点整理(2)-高级语言学习模型的建立之深入剖析委托
- C#语言知识点整理 - 常量
- C#语言知识点整理 - 枚举
- 改善 C# 的语言习惯(一) - 使用属性而不是可访问的数据成员(整理中)
- C#初级知识点整理及VS的简单使用
- C# 字符转换知识点整理
- 【学习ios之路:C语言】总结整理:学好Object-C---C语言必备知识点
- C#知识点整理
- C# 《编写高质量代码改善建议》整理&笔记 --(一)基本语言篇
- 对C#中的web访问mysql数据库的一些知识点进行了整理归纳总结
- C#知识点整理综合
- 输入汉字字符串索引拼音的首字母-.NET教程,C#语言
- C#知识点(为自己整理)
- C#知识点整理
- C#知识点整理(1)-委托
- 关于几种语言(c#,php,python,javascript)字符串的gzip压缩与解压的整理
- C#笔记整理(2)C#语言基础