主要想讲一下关于collection在asp.net2.0的应用.
1.Arrays
Array是一种最简单的集合对象(collection of object) .那么先从一个person的类开始吧.
关于类的概念不多说了,打开一个项目,新建一个App_Code的文件夹(这里面就是放置类文件),强调一下名字最好必须.
点住App_Code文件夹,添加一个person.cs的类文件.
person.cs代码如下
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// Summary description for person
/// </summary>
public class person
{
string FirstName;
string LastName;
public person(string first,string last)
{
FirstName = first;
LastName = last;
//
// TODO: Add constructor logic her //
}
public string FullName
{
get
{
return FirstName + " " + LastName;
}
}
}
然后新建一个showperson.aspx文件,后置代码showperson.aspx.cs
showperson.aspx.cs 内Page_load写如下
protected void Page_Load(object sender, EventArgs e)
{
person jimmy = new person("jimmy", "zeng");
person stone = new person("stone", "wang");
person helen = new person("Helen", "liu");
person[] people = { jimmy, stone, helen };
Response.Write("我们现在开始输出姓名了<br/>");
foreach(person p in people)
{
Response.Write(p.FullName+"<br/>");
}
}
意思就是person 生成三个姓名对象,分别是jimmy/stone/helen.然后组建了一个对象的array people.
最后使用foreach取得每个对象的FullName; 运行一下就可以看到结果了.
调整Arrays大小
在C#中无法调整Arrays的大小的.唯一的办法就是,copy以前的array到一个容量大的array
person [ ] arypeople2 = new person [ 5]
Array.Copy (people,arypeople2,people.Length):
在Arrays中查找对象array是最collection最简单的一种.我们一般都用索引值来快速定位(index).
当然我们在查找object的时候,我们要弄清楚我们是要查找一个确定属性的object还是说要查找同目标object拥有相同值的object.是两个根本区别的概念哦.
ok
looking for an object in an array by reference
person [ ] people = { jimmy, stone, helen};
int indexofjimmy = Array.Indexof(people,jimmy);
Response.Write("jimmy is at "+indexofjimmy +" <br/>");
这个时候输出就是
jimmy is at 0
再来一个例子
person jimmy = new person("jimmy", "zeng");
person stone = new person("stone", "wang");
person helen = new person("Helen", "liu");
person[] people = { jimmy, stone, helen };
person jimmy2 = new person("jimmy","zeng");
int indexofjimmy2 = Array.Indexof(people,jimmy2);
Response.Write("jimmy2 is at " +indexofjimmy2+"<br/>");
运行的几个结果是: jimmy2 is at -1
当然了,表示是jimmy2根本不再people这个array里面.当然我们的意图倒不是查找jimmy2这个object,而是想查找同jimmy2一样属性的object.怎么办呢/?/?
说下两个object的比较吧.
首先利用object.Equals我们override一下.
打开APP_Code目录下的person.cs 输入下面的代码重载一下就可以.
public override bool Equals(object obj)
{
person other = obj as person;
return (other.LastName == this.LastName && other.FirstName == this.FirstName);
}
然后我们在showpeople.aspx.cs里面作个比较
person jimmy2 = new person("jimmy", "zeng");
if (jimmy2.Equals(helen) == true)
{
Response.Write("ok");
}
else
{
Response.Write("false");
}
}
输出为false. ok成功比较对象.
导入IComparable
为了便于为array排序,比较我们导入IComparable
导入办法就是修改 person.cs代码如下看红色的代码处
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// Summary description for person
/// </summary>
public class person:IComparable
{
string FirstName;
string LastName;
public person(string first,string last)
{
FirstName = first;
LastName = last;
//
// TODO: Add constructor logic her //
}
{
get
{
return FirstName + " " + LastName;
}
}
}
ok 现在已经植入了.开始用了.
首先进行一个排序的用法
排序就直接用 Array.Sort(people)
然后是查找相等的array 这里用Array.BinarySearch
int indexofjimm2 = Array.BinarySearch(people.jimmy2);
这样就返回桶jimmy2 一样的jimmy在array的哪个位置.
好了差不多不讲了,其实现在asp.net2.0中有更加强大的ArrayList可以用.