C# Indexer 索引
来源:互联网 发布:vmware的ubuntu扩容 编辑:程序博客网 时间:2024/05/30 23:23
Indexer,索引,或译作索引器,可以使对象利用类似于数组的下标来访问类中的数据成员。
其语法为
修饰符 类型 this (参数列表)
{
get
{
//获取数据成员
}
set
{
//设置数据成员
}
}
其中,this指当前对象。get和set的语法和属性(property)很相像。只不过多了索引参数。
和方法一样,索引也可以重载。其重载是通过参数列表来区分的。
public class IndexerRecord
...{
private string[] keys = ...{
"Author","Publisher","Title","Subject","ISBN","Content"
};
private string[] data = new string[6];
//索引
public string this[int index]
...{
set
...{
this.data[index] = value;
}
get
...{
return data[index];
}
}
//索引重载
public string this[string key]
...{
set
...{
int index = FindKey(key);
this.data[index] = value;
}
get
...{
int index = FindKey(key);
return data[index];
}
}
private int FindKey(string key)
...{
int len = data.Length;
for(int i = 0; i < len ; i++)
...{
if(keys[i]==key)
return i;
}
return -1;
}
public static void Main()
...{
IndexerRecord r = new IndexerRecord();
//索引set操作
r[0] = "zhaohongliang";
r[2] = "C# Indexer Test";
//索引get操作
System.Console.WriteLine(r["Author"]);
System.Console.WriteLine(r["Title"]);
}
}
...{
private string[] keys = ...{
"Author","Publisher","Title","Subject","ISBN","Content"
};
private string[] data = new string[6];
//索引
public string this[int index]
...{
set
...{
this.data[index] = value;
}
get
...{
return data[index];
}
}
//索引重载
public string this[string key]
...{
set
...{
int index = FindKey(key);
this.data[index] = value;
}
get
...{
int index = FindKey(key);
return data[index];
}
}
private int FindKey(string key)
...{
int len = data.Length;
for(int i = 0; i < len ; i++)
...{
if(keys[i]==key)
return i;
}
return -1;
}
public static void Main()
...{
IndexerRecord r = new IndexerRecord();
//索引set操作
r[0] = "zhaohongliang";
r[2] = "C# Indexer Test";
//索引get操作
System.Console.WriteLine(r["Author"]);
System.Console.WriteLine(r["Title"]);
}
}
zhaohongliang
C# Indexer Test
- C# Indexer 索引
- C# 索引器(Indexer)
- C# 索引器(Indexer)
- C# 索引器(Indexer)
- C#索引器(Indexer)
- C#梳理【索引器Indexer】
- the Indexer(索引器) in C#
- C# 类索引器Class Indexer
- 索引器-Indexer(MSDN)
- INDEXER索引器例子
- 索引器indexer学习
- 索引器indexer
- 索引器indexer
- 索引器(Indexer)
- Multiparameter Indexer in C#
- Lucene实战-Indexer索引创建
- 【C#】索引器(Indexer)
- C#基础之字段(Field)、属性(Properties)和索引器(Indexer)
- 什么是Python
- Myeclipse 的菜茑问题 新手求助
- 请各位专家教授给我看看问题出在那里
- linux 下 飞鸽传书 最新原版的简单安装过程
- asp.net的Ajax学习进阶
- C# Indexer 索引
- 对日面试基本礼仪和用语
- 比较两个字符串是否相等?
- Harry Potter最终版的驾临
- 用 VC.NET 2003 (VC7.1) 编译 Xerces-c 2.7 和 ICU 3.6.
- typeid
- 今天租了房子
- SQL中使用关系代数合并数据
- 要集中看体育方面的视频了