基础知识补充

索引 器

没有名字 ,索引器的内部本质 (ILSpy的IL模式下看)类型 this[参数]{get;set;}

可以是只读或者只写(在get或者set前加上private)

字符串是只读索引,因此不能对字符串中的某个字符进行从新赋值,即只能char ch = s[5];不能s[5]=‘a’。

开发中自己写的机会很少,一道面试题:C#中索引器是否只能根据数字进行索引?是否允许多个索引器参数?答案:可以进行非数字索引,可以允许多个参数进行索引

using System;

using System.Collections;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace TestConsole

{

class Program

{

static void Main(string[] args)

{

MyIntIndex myIntIndex = new TestConsole.MyIntIndex();//整型索引

string name1 = myIntIndex[1];

Console.WriteLine(name1);

Hashtable ht = new Hashtable();

ht.Add("001", "chizi");

ht.Add("002", "dandan");

MyStringIndex myStringIndex = new TestConsole.MyStringIndex(ht);//字符串索引

string name2 = myStringIndex["001"];

Console.WriteLine(name2);

Console.ReadKey();

}

}

class MyStringIndex

{

private Hashtable ht;//字符串索引用到哈希表来存放键值对

public MyStringIndex(Hashtable ht)

{

this.ht = ht;

}

public string this[string key]

{

get

{

string name =(string)ht[key];

return name;

}

set

{

ht[key] = value;

}

}

}

class MyIntIndex

{

private static string[] name = { "dandan", "chizi", "jianguo" };

public string this[int index]

{

get

{

string n = name[index];

return n;

}

set

{

name[index] = value;

}

}

}

}

更多技术请关注

最新文章

  1. phpcms首页实现轮播图
  2. python获取当前时间的用法
  3. 《The Elder Scrolls V: Skyrim》百般冷门却强力职业
  4. 泛函编程(23)-泛函数据类型-Monad
  5. python 代码片段12
  6. Localizing Astah – Chinese version(simplified) is now available!
  7. BOM-字节序标记
  8. Debug with jdb
  9. Computational Geometry Template
  10. 最近客户的apache+php环境运行很慢解决
  11. Spring应用上下文中Bean的生命周期
  12. 一天搞定CSS: CSS选择器优先级--08
  13. Java实现二叉树的创建和遍历操作(有更新)
  14. linux工具-journalctl查询日志
  15. VMware Workstation Pro网络配置(WiFi配置等)
  16. flask中如何生成迁移文件
  17. 【知识碎片】getResource和getResourceAsStream
  18. 大话设计模式(C#)
  19. [转]iOS 中几种定时器 - 控制了时间,就控制了一切
  20. C++中的四种类型转换

热门文章

  1. Razor视图引擎布局 Razor视图引擎的基本概念与法语 SQL Server Mobile 和 .NET 数据访问接口之间的数据类型映射 binary 和 varbinary datetime 和 smalldatetime float 和 real
  2. HDU 2049 不容易系列之(4)——考新郎 (递推,含Cmn公式)
  3. 线程相关函数(POSIX线程):
  4. Win8下怎样安装Win7 or Win7下怎样安装win8?
  5. 项目Beta冲刺(团队3/7)
  6. const& 的东西
  7. update外联表,用另一个表数据更新本表数据
  8. Delphi服务端和PHP客户端通过Socket通信
  9. webpack与grunt/glub 的比较
  10. Oracle:datafile删除后,重启server报ORA-01110