C# 笔记——索引器
索引器允许类或者结构的实例按照与数组相同的方式进行索引取值,索引器与属性类似,不同的是索引器的访问是带参的。
索引器和数组比较:
(1)索引器的索引值(Index)类型不受限制
(2)索引器允许重载
(3)索引器不是一个变量
索引器和属性的不同点
(1)属性以名称来标识,索引器以函数形式标识
(2)索引器可以被重载,属性不可以
(3)索引器不能声明为static,属性可以
一个简单的索引器例子
using System;
using System.Collections;
public class IndexerClass
{
private string[] name = new string[2];
//索引器必须以this关键字定义,其实这个this就是类实例化之后的对象
public string this[int index]
{
//实现索引器的get方法
get
{
if (index < 2)
{
return name[index];
}
return null;
} //实现索引器的set方法
set
{
if (index < 2)
{
name[index] = value;
}
}
}
}
public class Test
{
static void Main()
{
//索引器的使用
IndexerClass Indexer = new IndexerClass();
//“=”号右边对索引器赋值,其实就是调用其set方法
Indexer[0] = "张三";
Indexer[1] = "李四";
//输出索引器的值,其实就是调用其get方法
Console.WriteLine(Indexer[0]);
Console.WriteLine(Indexer[1]);
}
}
以字符串作为下标,对索引器进行存取
public class IndexerClass
{
//用string作为索引器下标的时候,要用Hashtable
private Hashtable name = new Hashtable(); //索引器必须以this关键字定义,其实这个this就是类实例化之后的对象
public string this[string index]
{
get { return name[index].ToString();
set { name.Add(index, value); }
}
}
public class Test
{
static void Main()
{
IndexerClass Indexer = new IndexerClass();
Indexer["A0001"] = "张三";
Indexer["A0002"] = "李四";
Console.WriteLine(Indexer["A0001"]);
Console.WriteLine(Indexer["A0002"]);
}
}
索引器的重载
public class IndexerClass
{
private Hashtable name = new Hashtable(); //1:通过key存取Values
public string this[int index]
{
get { return name[index].ToString(); }
set { name.Add(index, value); }
} //2:通过Values存取key
public int this[string aName]
{
get
{
//Hashtable中实际存放的是DictionaryEntry(字典)类型,如果要遍历一个Hashtable,就需要使用到DictionaryEntry
foreach(DictionaryEntry d in name)
{
if (d.Value.ToString() == aName)
{
return Convert.ToInt32(d.Key);
}
}
return -1;
}
set
{
name.Add(value, aName);
}
}
}
public class Test
{
static void Main()
{
IndexerClass Indexer = new IndexerClass(); //第一种索引器的使用
Indexer[1] = "张三";//set访问器的使用
Indexer[2] = "李四";
Console.WriteLine("编号为1的名字:" + Indexer[1]);//get访问器的使用
Console.WriteLine("编号为2的名字:" + Indexer[2]); Console.WriteLine();
//第二种索引器的使用
Console.WriteLine("张三的编号是:" + Indexer["张三"]);//get访问器的使用
Console.WriteLine("李四的编号是:" + Indexer["李四"]);
Indexer["王五"] = 3;//set访问器的使用
Console.WriteLine("王五的编号是:" + Indexer["王五"]);
}
}
多参索引器
using System;
using System.Collections; //入职信息类
public class EntrantInfo
{
//姓名、编号、部门
private string name;
private int number;
private string department;
public EntrantInfo()
{ }
public EntrantInfo(string name, int num, string department)
{
this.name = name;
this.number = num;
this.department = department;
} public string Name
{
get { return name; }
set { name = value; }
} public int Num
{
get { return number; }
set { number = value; }
} public string Department
{
get { return department; }
set { department = value; }
}
} //声明一个类EntrantInfo的索引器
public class IndexerForEntrantInfo
{
private ArrayList ArrLst;//用于存放EntrantInfo类
public IndexerForEntrantInfo()
{
ArrLst = new ArrayList();
} //声明一个索引器:以名字和编号查找存取部门信息
public string this[string name, int num]
{
get
{
foreach (EntrantInfo en in ArrLst)
{
if (en.Name == name && en.Num == num)
{
return en.Department;
}
}
return null;
}
set
{
//new关键字:C#规定,实例化一个类或者调用类的构造函数时,必须使用new关键
ArrLst.Add(new EntrantInfo(name, num, value));
}
} //声明一个索引器:以编号查找名字和部门
public ArrayList this[int num]
{
get
{
ArrayList temp = new ArrayList();
foreach (EntrantInfo en in ArrLst)
{
if (en.Num == num)
{
temp.Add(en);
}
}
return temp;
}
} //还可以声明多个版本的索引器...
} public class Test
{
static void Main()
{
IndexerForEntrantInfo Info = new IndexerForEntrantInfo();
//this[string name, int num]的使用
Info["张三", ] = "人事部";
Info["李四", ] = "行政部";
Console.WriteLine(Info["张三", ]);
Console.WriteLine(Info["李四", ]); Console.WriteLine(); //this[int num]的使用
foreach (EntrantInfo en in Info[])
{
Console.WriteLine(en.Name);
Console.WriteLine(en.Department);
}
}
}
C# 笔记——索引器的更多相关文章
- C#索引器学习笔记
本笔记摘抄自:https://www.cnblogs.com/ArmyShen/archive/2012/08/27/2659405.html,记录一下学习过程以备后续查用. 索引器允许类或者结构的实 ...
- 《Inside C#》笔记(六) 属性、数组、索引器
一 属性 a) 属性可用于隐藏类的内部成员,对外提供可控的存取接口.属性相当于有些语言的getter.setter方法,只是使用起来更加方便一点,而且查看对应的IL码可以看到,属性的本质也确实是方法. ...
- C#学习笔记(十六):索引器和重载运算符
二维数组如何映射到一维数组 重载运算符 1.算术运算符 2.关系运算符, < 和 > 成对重载 using System; using System.Collections.Generic ...
- 接口、索引器、Foreach的本质(学习笔记)
接口 什么是接口? 接口代表一种能力,和抽象类类似但比抽象类的抽象程度更高! 接口的定义: public interface IEat//定义一个接口 { void Eat(string food); ...
- C# 索引器(C#学习笔记05)
索引器 索引器能够使对象像数组一样被索引,使用数组的访问方式 object[x] 索引器的声明在某种程度上类似于属性的声明,例如,使用 get 和 set 方法来定义一个索引器. 不同的是,属性值的定 ...
- 【c# 学习笔记】索引器
当一个类包含数组成员时,索引器 的使用将大大地简化对类中数组成员的访问.索引器的定义类似于属性,也具有GET访问器和set访问器,如下: [修饰符] 数据类型 this[索引类型 index] { g ...
- Elasticsearch7.X 入门学习第八课笔记-----索引模板和动态模板
原文:Elasticsearch7.X 入门学习第八课笔记-----索引模板和动态模板 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接: ...
- 【.net 深呼吸】细说CodeDom(7):索引器
在开始正题之前,先补充一点前面的内容. 在方法中,如果要引用方法参数,前面的示例中,老周使用的是 CodeVariableReferenceExpression 类,它用于引用变量,也适用于引用方法参 ...
- C# 索引器,实现IEnumerable接口的GetEnumerator()方法
当自定义类需要实现索引时,可以在类中实现索引器. 用Table作为例子,Table由多个Row组成,Row由多个Cell组成, 我们需要实现自定义的table[0],row[0] 索引器定义格式为 [ ...
随机推荐
- BZOJ1009:[HNOI2008]GT考试——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=1009 Description 阿申准备报名参加GT考试,准考证号为N位数X1X2....Xn(0&l ...
- JavaScript中的函数与栈
Javascript中会经常用到setTimeout来推迟一个函数的执行,如: setTimeout(function(){ alert("Hello World"); },100 ...
- PowerDesigner 技巧【2】
去掉Oracle生成的SQL创建语句中的双引号 用powerdesigner导出orale数据库的建表sql时,默认会给表名和字段名加上双引号,如下图: 这样给操作数据库带来很大的不便,解决的办法是设 ...
- 将shell返回的结果保存至数组
如下,我需要将u1和u2提取出保存至数组,方便后续的调用 root@ubuntu:~# lxc list+------+---------+------------------------------ ...
- ubuntu 服务器搭建汇总
开启ssh 1.首先:终端安装开启ssh-server服务: sudo apt-get install openssh-server 2.然后确认sshserver是否启动了: ps-e | grep ...
- HashMap详谈以及实现原理
(一).HashMap 基于哈希表的 Map 接口的实现 允许使用 null 值和 null 键 HashMap不是线程安全,想要线程安全,Collections类的静态方法synchronizedM ...
- ES6中函数的扩展
一.设置默认参数 ES6之前,给函数设置默认参数是这样做的: function fn(a) { if(typeof y === undefined){ a = a || 'hello'; } cons ...
- jsp 安装环境与基本语法
1.什么是web应用程序? web应用程序是一种可以通过web访问的应用程序.web应用程序的最大好处是用户很容易访问应用程序.用户只需要有浏览器即可,不需要安装其它软件. 2.搭建 java web ...
- Item 2---遇到构造器具有多个参数时,要考虑用构建器;Builder模式
问题,面对这种一个构造器具备多个参数的问题,现有的做法是使用重叠构造器的方式,该方式存在的问题: public class NutritionFacts { private final int ser ...
- UOJ#179. 线性规划[模板]
传送门 http://uoj.ac/problem/179 震惊,博主竟然还不会线性规划! 单纯形实在学不会啊……背个板子当黑盒用…… 学(chao)了NanoApe dalao的板子 #includ ...