《C#本质论》读书笔记(16)构建自定义集合
16.1 更多集合接口
集合类(这里指IEnumerable层次结构)实现的接口层次结构

16.1.1 IList<T>与IDictionary<TKey,TValue>
list.Remove("Grumpy");
16.2.3 搜索List<T>
List<string> list = new List<string>();
int search;
list.Add("public");
list.Add("protected");
list.Add("private");
list.Sort();
search = list.BinarySearch("protected internal");
if (search < 0)
{
list.Insert(~search, "protected internal");
}
foreach (string accessModifier in list)
{
Console.WriteLine(accessModifier);
}
高级主题:使用 FindAll() 查找多个数据项
public static void Main()
{
List<int> list = new List<int>();
list.Add(1);
list.Add(2);
list.Add(3);
list.Add(2);
list.Add(4);
List<int> results = list.FindAll(Even);
foreach (int number in results)
{
Console.WriteLine(number);
}
//2,2,4
Console.Read();
}
public static bool Even(int value)
{
return (value % 2) == 0;
}
16.2.4 字典集合:Dictonary<TKey,TValue>

Dictionary<Guid, string> dictionary =
new Dictionary<Guid, string>();
Guid key = Guid.NewGuid();
dictionary.Add(key, "object");
还有个选择是索引操作符
Dictionary<Guid, string> dictionary =
new Dictionary<Guid, string>();
Guid key = Guid.NewGuid();
dictionary[key] = "object";
dictionary[key] = "byte";
由于键和值都要添加到字典中,所以用于枚举字典中的元素的 foreach 循环的循环变量必须是 KeyValuePair<TKey,TValue> 。
Dictionary<string, string> dictionary = new
Dictionary<string, string>();
int index = 0;
dictionary.Add(index++.ToString(), "object");
dictionary.Add(index++.ToString(), "byte");
dictionary.Add(index++.ToString(), "uint");
dictionary.Add(index++.ToString(), "ulong");
dictionary.Add(index++.ToString(), "float");
dictionary.Add(index++.ToString(), "char");
dictionary.Add(index++.ToString(), "bool");
dictionary.Add(index++.ToString(), "ushort");
dictionary.Add(index++.ToString(), "decimal");
dictionary.Add(index++.ToString(), "int");
dictionary.Add(index++.ToString(), "sbyte");
dictionary.Add(index++.ToString(), "short");
dictionary.Add(index++.ToString(), "long");
dictionary.Add(index++.ToString(), "void");
dictionary.Add(index++.ToString(), "double");
dictionary.Add(index++.ToString(), "string");
Console.WriteLine("Key Value Hashcode");
Console.WriteLine("--- ------- --------");
foreach (KeyValuePair<string, string> i in dictionary)
{
Console.WriteLine("{0,-5}{1,-9}{2}",
i.Key, i.Value, i.Key.GetHashCode());
}

16.2.5 已排序集合:SortedDictionary<TKey,TValue>和SortedList<T>

SortedDictionary<string, string> sortedDictionary =
new SortedDictionary<string, string>();
int index = 0;
sortedDictionary.Add(index++.ToString(), "object");
sortedDictionary.Add(index++.ToString(), "byte");
sortedDictionary.Add(index++.ToString(), "uint");
sortedDictionary.Add(index++.ToString(), "ulong");
sortedDictionary.Add(index++.ToString(), "float");
sortedDictionary.Add(index++.ToString(), "char");
sortedDictionary.Add(index++.ToString(), "bool");
sortedDictionary.Add(index++.ToString(), "ushort");
sortedDictionary.Add(index++.ToString(), "decimal");
sortedDictionary.Add(index++.ToString(), "int");
sortedDictionary.Add(index++.ToString(), "sbyte");
sortedDictionary.Add(index++.ToString(), "short");
sortedDictionary.Add(index++.ToString(), "long");
sortedDictionary.Add(index++.ToString(), "void");
sortedDictionary.Add(index++.ToString(), "double");
sortedDictionary.Add(index++.ToString(), "string");
Console.WriteLine("Key Value Hashcode");
Console.WriteLine("--- ------- ----------");
foreach (
KeyValuePair<string, string> i in sortedDictionary)
{
Console.WriteLine("{0,-5}{1,-9}{2}",
i.Key, i.Value, i.Key.GetHashCode());
}


16.2.6 栈集合:Stack<T>

16.2.7队列集合:Queue<T>

16.2.8 链表:LinkedList<T>

16.4 返回null或者空集合
16.5 迭代器
16.5.1 迭代器定义
16.5.2 迭代器语法
public IEnumerator<T> GetEnumerator()
{
//...
return new List<T>.Enumerator();//This will be implimented in 16.16
}
16.1.3 从迭代器生成值
《C#本质论》读书笔记(16)构建自定义集合的更多相关文章
- 十六、C# 常用集合类及构建自定义集合(使用迭代器)
常用集合类及构建自定义集合 1.更多集合接口:IList<T>.IDictionary<TKey,TValue>.IComparable<T>.ICollectio ...
- 16位模式/32位模式下PUSH指令探究——《x86汇编语言:从实模式到保护模式》读书笔记16
一.Intel 32 位处理器的工作模式 如上图所示,Intel 32 位处理器有3种工作模式. (1)实模式:工作方式相当于一个8086 (2)保护模式:提供支持多任务环境的工作方式,建立保护机制 ...
- Node.js高级编程读书笔记 - 4 构建Web应用程序
Outline 5 构建Web应用程序 5.1 构建和使用HTTP中间件 5.2 用Express.js创建Web应用程序 5.3 使用Socket.IO创建通用的实时Web应用程序 5 构建Web应 ...
- 《java并发编程实战》读书笔记11--构建自定义的同步工具,条件队列,Condition,AQS
第14章 构建自定义的同步工具 本章将介绍实现状态依赖性的各种选择,以及在使用平台提供的状态依赖机制时需要遵守的各项规则. 14.1 状态依赖性的管理 对于并发对象上依赖状态的方法,虽然有时候在前提条 ...
- ArcGIS API for JavaScript 4.2学习笔记[16] 弹窗自定义功能按钮及为要素自定义按钮(第五章完结)
这节对Popups这一章的最后两个例子进行介绍和解析. 第一个[Popup Actions]介绍了弹窗中如何自定义工具按钮(名为actions),以PopupTemplate+FeatureLayer ...
- OCA读书笔记(16) - 执行数据库恢复
16. Performing Database Recovery 确定执行恢复的必要性访问不同接口(EM以及命令行)描述和使用可用选项,如RMAN和Data Recovery Advisor执行恢复- ...
- 流处理与消息队列------《Designing Data-Intensive Applications》读书笔记16
上一篇聊了聊批处理的缺点,对于无界数据来说,流处理会是更好的选择,"流"指的是随着时间的推移逐步增加的数据.消息队列可以将这些流组织起来,快速的在应用程序中给予反馈.但是消息队列与 ...
- 《Java Concurrency》读书笔记,构建线程安全应用程序
1. 什么是线程安全性 调用一个函数(假设该函数是正确的)操作某对象常常会使该对象暂时陷入不可用的状态(通常称为不稳定状态),等到操作完全结束,该对象才会重新回到完全可用的状态.如果其他线程企图访问一 ...
- 【读书笔记】构建之法(CH4~CH6)
从chapter4至chapter6,围绕着构建过程的合作讨论构建之法,而合作与个人工作的区别却以一个微妙的问题为开端:阅读别人的代码有多难? 两人合作:(驾驶员与领航员) 合作要注意代码风格规范与设 ...
随机推荐
- 微信JSSDK javascript 开发 代码片段,仅供参考
最全面最专业的微信公众平台开发教程:http://www.cnblogs.com/txw1958/p/weixin-js-sdk-demo.html 比较完整的分享教程:http://www.cnbl ...
- mysql-批量修改表字段中的某一部分内容
MySQL批量替换指定字段字符串语句(1)updat 表名 set 字段名=replac(字段名,'原来的内容','替换后的内容') 举一个例子,就是我实际操作的时候的命令: update cpg14 ...
- 【转】Java中try catch finally语句中含有return语句的执行情况(总结版)
Java中try catch finally语句中含有return语句的执行情况(总结版) 有一点可以肯定,finally块中的内容会先于try中的return语句执行,如果finall语句块中也有r ...
- HTML5利用link标签的rel=import引入html页面
如果是以前,我们可以使用iframe去引入,现在可以是这样的形式:<link rel="import" href="a.html" id="tm ...
- 了解了下 Google 最新的 Fuchsia OS
就是看到篇报道,有点好奇,就去FQ挖了点东西回来. 我似乎已开始就抓到了重点,没错,就是 LK . LK 是 Travis Geiselbrecht 写的一个针对 ARM 的嵌入式操作系统,开源的.点 ...
- Web项目学习
首先配好jdk,tomcat,下载eclipse,下载bootstrap模板,进行JDBC连接 创建项目 打开Eclipse,选择左上角的File->NEW->最后一个other,选择如下 ...
- Alpha阶段发布说明
Alpha版本功能介绍 机器法官功能已实现 这是我们统计了当下所有存在的狼人APP的共同缺点.也是用户最主要的痛点.现在所有已知存在的类似APP都不能提供法官功能,我们的APP将该功能革命性的自动实现 ...
- 分析移动端APP的网络请求抓包
为了方便,本文以 iOS 系统来进行演示. 使用代理 移动操作系统中都有可以设定系统代理的设置,比如在 iOS 中可以通过 Settings->WLAN 看到很多 Networks,通过点击它们 ...
- python描述符(descriptor)、属性(property)、函数(类)装饰器(decorator )原理实例详解
1.前言 Python的描述符是接触到Python核心编程中一个比较难以理解的内容,自己在学习的过程中也遇到过很多的疑惑,通过google和阅读源码,现将自己的理解和心得记录下来,也为正在为了该问题 ...
- SDL绑定播放窗口 及 视频窗口缩放
绑定播放窗口 必须在Sdl.SDL_Init之前执行 Sdl.SDL_putenv 同时SDL_SetVideoMode里播放窗口长宽不能大于绑定窗口的长宽 int i = Sdl.SDL_puten ...