一、集合

** System.Collections名称空间中的几个接口提供了基本的集合功能

Ps:这里看成一个动态的链表,但是已经完美的封装好了。

(一)使用集合

1、代码示例

(1)Animal.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Exercise
{
public abstract class Animal
{
protected string name; public string Name
{
get
{
return name;
}
set
{
name = value;
}
} public Animal()
{
name = "The animal with no name";
}
public Animal(string newName)
{
name = newName;
} public void Feed()
{
Console.WriteLine("{0} has been fed", name);
}
}
}

(2)Cow.cs

namespace Exercise
{
public class Cow:Animal
{
public void Milk()
{
Console.WriteLine("{0} has been milked.", name);
} public Cow(string newName):base(newName)
{ }
}
}

(3)Chicken.cs

namespace Exercise
{
public class Chicken:Animal
{
public void LayEgg()
{
Console.WriteLine("{0} has laid an egg.", name);
} public Chicken(string newName):base(newName)
{ }
}
}

(4)Program.cs

namespace Exercise
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Create an Array type collection of Animal " + "objects and use it:"); Animal[] animalAraay = new Animal[2];
Cow myCowl = new Cow("Deirdre");
animalAraay[0] = myCowl;
animalAraay[1] = new Chicken("Ken"); foreach(Animal myAnimal in animalAraay)
{
Console.WriteLine("New {0} object added to Array collection, " + "Name = {1}", myAnimal.ToString(), myAnimal.Name);
} Console.WriteLine("Array collection contains {0} objects.", animalAraay.Length);
animalAraay[0].Feed();
((Chicken)animalAraay[1]).LayEgg();
Console.WriteLine();
Console.ReadKey(); Console.WriteLine("Create an ArrayList type collection of Animal " + "object and use it ");
ArrayList animalArrayList = new ArrayList();
Cow mycow2 = new Cow("Heylay");
animalArrayList.Add(mycow2);
animalArrayList.Add(new Chicken("Roy")); foreach(Animal myAnimal in animalArrayList)
{
Console.WriteLine("New {0} object added to ArrayList collection," + "Name = {1}", myAnimal.ToString(), myAnimal.Name);
}
Console.WriteLine("ArrayList collection contains {0} objects.", animalArrayList.Count);
((Animal)animalArrayList[0]).Feed();
((Chicken)animalArrayList[1]).LayEgg();
Console.WriteLine(); Console.WriteLine("Additional manipulation of ArrayList:");
animalArrayList.RemoveAt(0);
((Animal)animalArrayList[0]).Feed();
animalArrayList.AddRange(animalAraay);
((Chicken)animalArrayList[2]).LayEgg();
Console.WriteLine("The animal called {0} is at index {1}.", myCowl.Name, animalArrayList.IndexOf(myCowl));
myCowl.Name = "Janice";
Console.WriteLine("The animal is now called {0}.", ((Animal)animalArrayList[1]).Name);
Console.ReadKey(); }
}
}

2、运行结果

3、注意点:

(1)ArrayList创建时不需要指定初始长度值。但是Array是需要的。

(2)对于ArrayList是不强调类型的一个集合,所以再采用所属对象的方法之类的时候,必须进行强制类型转换,而对于Array来说,他是强调对象类型的集合,所以可以直接调用其方法,但是对于其派生类来说,还是需要进行强制类型转换。

(3)还有些删除(at)和扩展方法(AddRange)之类的扩展方法

(4)ArrayList需要加上using System.Collections;名字空间的引用,切记切记。

(二)定义集合

一般从已有的基类来派生自己的集合,例如System.Collections.CollectionsBase还有System.IDictonaryBase。

1、System.Collections.CollectionsBase

(1)为了便于完成任务,CollectionBase提供了两个受保护的属性List和InnerList,List可以通过IList接口访问项,InnerList则是用于储存项的ArrayList对象。

(2)一个demo

注意Add和Remove是强类型化的方法,该方法只能用于处理Animal类或者派生于Animal的类,而前面介绍的ArrayList实现代码可以处理任何的对象。

2、索引符

(1)

这种类似数组的用法必须是在提供了索引符之后才能够使用。

(2)  一个demo:添加一个数字索引。

public class Animals:CollectionBase
{ public Animal this[int animalIndex]
{
get
{
return (Animal)List[animalIndex]; //因为IList.List属性返回的是一个System.Object对象。所以要进行强制类型转换。
}
set
{
List[animalIndex]=value;
}
}
}

(3)下面我们使用Animal来构建一个Animals集合

a、创建一个Animals类

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Exercise
{
public class Animals:CollectionBase
{
public void Add(Animal newAnimal)
{
List.Add(newAnimal);
}
public void Remove(Animal newAnimal)
{
List.Remove(newAnimal);
} public Animals()
{ }
public Animal this[int animalIndex]
{
get
{
return (Animal)List[animalIndex];
}
set
{
List[animalIndex] = value;
}
}
}
}

b、修改Program.cs的代码

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Exercise
{
class Program
{
static void Main(string[] args)
{
Animals animalCollection = new Animals(); animalCollection.Add(new Cow("Jack"));
animalCollection.Add(new Chicken("SB")); foreach(Animal myAnimal in animalCollection)
{
myAnimal.Feed();
}
Console.ReadKey(); }
}
}

c、简单来说就是创建了一个Animal的集合,List是用于访问的接口,所以有Add,Remove的方法,我们还添加了索引符,最后再主函数里面实例化一个Animal集合,然后添加了两个Animal的派生类,最后使用foreach循环调用这两个对象继承于基类Animal的Feed()的方法。

C#学习笔记(九)——集合、比较和转换的更多相关文章

  1. 多线程学习笔记九之ThreadLocal

    目录 多线程学习笔记九之ThreadLocal 简介 类结构 源码分析 ThreadLocalMap set(T value) get() remove() 为什么ThreadLocalMap的键是W ...

  2. MDX导航结构层次:《Microsoft SQL Server 2008 MDX Step by Step》学习笔记九

    <Microsoft SQL Server 2008 MDX Step by Step>学习笔记九:导航结构层次   SQL Server 2008中SQL应用系列及BI笔记系列--目录索 ...

  3. 软件测试之loadrunner学习笔记-02集合点

    loadrunner学习笔记-02集合点 集合点函数可以帮助我们生成有效可控的并发操作.虽然在Controller中多用户负载的Vuser是一起开始运行脚本的,但是由于计算机的串行处理机制,脚本的运行 ...

  4. python3.4学习笔记(九) Python GUI桌面应用开发工具选择

    python3.4学习笔记(九) Python GUI桌面应用开发工具选择 Python GUI开发工具选择 - WEB开发者http://www.admin10000.com/document/96 ...

  5. Go语言学习笔记九: 指针

    Go语言学习笔记九: 指针 指针的概念是当时学C语言时了解的.Go语言的指针感觉与C语言的没啥不同. 指针定义与使用 指针变量是保存内存地址的变量.其他变量保存的是数值,而指针变量保存的是内存地址.这 ...

  6. Java学习笔记之---集合

    Java学习笔记之---集合 (一)集合框架的体系结构 (二)List(列表) (1)特性 1.List中的元素是有序并且可以重复的,成为序列 2.List可以精确的控制每个元素的插入位置,并且可以删 ...

  7. go微服务框架kratos学习笔记九(kratos 全链路追踪 zipkin)

    目录 go微服务框架kratos学习笔记九(kratos 全链路追踪 zipkin) zipkin使用demo 数据持久化 go微服务框架kratos学习笔记九(kratos 全链路追踪 zipkin ...

  8. Python学习笔记九

    Python学习笔记之九 为什么要有操作系统 管理硬件,提供接口. 管理调度进程,并且将多个进程对硬件的竞争变得有序. 操作系统发展史 第一代计算机:真空管和穿孔卡片 没有操作系统,所有的程序设计直接 ...

  9. vue学习笔记(九)vue-cli中的组件通信

    前言 在上一篇博客vue学习笔记(八)组件校验&通信中,我们学会了vue中组件的校验和父组件向子组件传递信息以及子组件通知父组件(父子组件通信),上一篇博客也提到那是对组件内容的刚刚开始,而本 ...

  10. C#线程学习笔记九:async & await入门二

    一.异步方法返回类型 只能返回3种类型(void.Task和Task<T>). 1.1.void返回类型:调用方法执行异步方法,但又不需要做进一步的交互. class Program { ...

随机推荐

  1. QQ 腾讯QQ(简称“QQ”)是腾讯公司开发的一款基于Internet的即时通信(IM)软件

    QQ 编辑 腾讯QQ(简称“QQ”)是腾讯公司开发的一款基于Internet的即时通信(IM)软件.腾讯QQ支持在线聊天.视频通话.点对点断点续传文件.共享文件.网络硬盘.自定义面板.QQ邮箱等多种功 ...

  2. C++中的异常处理(二)

    C++中的异常处理(二) 标签: c++C++异常处理 2012-11-24 20:56 1713人阅读 评论(2) 收藏 举报  分类: C++编程语言(24)  版权声明:本文为博主原创文章,未经 ...

  3. Windows下文件的所有和权限

    跟linux不同, 在linux下 ,文件的所有者,就拥有对文件的所有读写执行的权限, 而windows, 文件的所有者不一定对文件拥有所有的权限, 场景: 要对系统文件(windows\system ...

  4. 获取window窗口大小

    窗口大小 跨浏览器确定一个窗口的大小不是一件简单的事.IE9+.Firefox.Safari.Opera和Chrome均为此提供了4个属性:innerWidth.innerHeight.outerWi ...

  5. dedecms首页调用的简介一直修改不了是自动文章摘要在作怪

    一位美女问:dedecms首页调用的简介一直修改不了,ytkah让她到具体的文章修改,然后再重新生成一下首页.她说还是不行.那就奇了怪了,点击到具体的文章页面是显示已经修改好了,为什么首页还是原来的呢 ...

  6. 昂贵的聘礼(dijkstra)

    昂贵的聘礼 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 38549   Accepted: 11158 Descripti ...

  7. Python 常用函数大体分类

    ==================系统库函数================ 字符串函数 举例数学函数 import math val=math.sin(3.14/6) val=math.sin(m ...

  8. 作为一名职高生学习Linux的心酸经历

    当你点进这篇文章的时候,一定会好奇我为什么要用“心酸”这个词,这个词已经太久没被人提起,也许心酸这种感情只能存在于一个人在追中梦想过程中内心角落吧.从小我们总是会被问这样一个问题“你的梦想是什么?”每 ...

  9. Mysql跨平台(Windows,Linux,Mac)使用与安装

    MySQL其实是一个跨平台的轻量级数据库,平时开发会用到很多.有写程序可能要跨平台开发,接下来我就介绍一下如何跨平台使用Mysql. 这里所谓的跨平台就是Windows,Linux,Mac共同用一套M ...

  10. Iphone和iPad适配, 横竖屏

    竖屏情况下: [UIScreen mainScreen].bounds.size.width = 320 [UIScreen mainScreen].bounds.size.width = 568 横 ...