在C#语言中定义一个Collection类最简单的方法就是把System.Collection库中的CollectionBase类作为基础类。Collection类中包括了很多方法和属性,这里介绍一下Add方法、Remove方法、Count方法和Clear方法。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace myCollection
{
public class Collection :CollectionBase
{
public void Add(Object item)
{
InnerList.Add(item);
}
public void Remove(Object item)
{
InnerList.Remove(item);
}
public void Clear()
{
InnerList.Clear();
}
public int Count()
{
return InnerList.Count;
}
}
}
关于以上的实际应用,一般的资料上只介绍了简单的仅“姓名”队列的应用:
class Program
{
static void Main(string[] args)
{
Collection names = new Collection();
names.Add(“David”);
names.Add(“Bernic”);
names.Add(“Raymond”);
names.Add(“Clayton”);
foreach (Object name in names)
{
Console.WriteLine(name);
}
Console.WriteLine(“Number of names: {0}”, names.Count());
names.Remove(“Raymond”);
Console.WriteLine(“After remove Raymond,the number of names: {0}”, names.Count());
names.Clear();
Console.WriteLine(“Number of names: {0}”, names.Count());
Console.ReadKey();
}
然而,实际应用中,数据类型复杂。以下,是根据图灵计算机科学丛书《数据结构与算法(C#语言描述)》习题一所建立的复杂类型测试(不涉及题目的具体解答)。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using myCollection;
namespace tryCollectionClass
{
public class Students
{
private int studentID;
private string studentName;
public int ID
{
get
{
return studentID;
}
}
public string name
{
get
{
return studentName;
}
}
public Students(int id, string name)
{
this.studentID = id;
this.studentName = name;
}
}
class Program
{
static void Main(string[] args)
{
Collection studentCollection = new Collection();
Students stu1 = new Students(8503, “James”);
Students stu2 = new Students(8518, “Allbort”);
Students stu3 = new Students(8524, “YouGoFirst”);
Students stu4 = new Students(8537, “WeFinished”);
Students stu5 = new Students(8519, “ILikeIt”);
studentCollection.Add(stu1);
studentCollection.Add(stu2);
studentCollection.Add(stu3);
studentCollection.Add(stu4);
studentCollection.Add(stu5);
foreach (Students s in studentCollection)
{
Console.WriteLine(“{0} {1}”,s.name,s.ID);
}
Console.WriteLine();
Console.WriteLine(studentCollection.Count());
//Console.ReadKey();
studentCollection.Clear();
Console.WriteLine(“Clear”);
Console.WriteLine(studentCollection.Count());
Console.ReadKey();
}
}
}
[整理自网络]
分类目录
- arch/management (17)
- computer (38)
- java/j2ee (304)
- lnmpa (237)
- mac/iphone/ipad/android (11)
- mysql/oracle/postgresql (126)
- os/software (74)
- other (518)
- python (6)
- redis/memcached/mongo (31)
- sitebuild (143)
随便看看
标签云
程序员 创业 人生箴言 eclipse 快捷键 术语 索引 unix命令 vim wordpress java学习笔记 环境变量 oracle内置函数 index 人生 数据类型 nohup tuxedo mysql学习笔记 MS-DOS命令 servlet spring 职场进阶 职业进阶 服务器选购 服务器选型 apache JPA MongoDB 注解 tomcat 子女教育 jquery maven JVM aix命令 网络营销 java异常 seo 人生规划 关键字 css 网络推广 struts 系统优化 成长 frame iframe bluehost jdbc select 我的信仰 oracle函数 cookie HashMap 站长工具 乱码 ArrayList secureCRT jsp session tail find halt 事务 oracle单记录函数 算法 URL window table javascript操作表单元素 String 字符串处理 健康 http 域名 情感 more google A记录 域名解析 netstat 弹出对话框 弹出窗口 框架集 框架 excel 字符串 javascript函数 showModalDialog nginx number 数组 sql frameset 开源程序 java数组 软件 oracle服务友情链接
收藏链接