惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Secure Thoughts
S
Securelist
P
Proofpoint News Feed
D
DataBreaches.Net
Cisco Talos Blog
Cisco Talos Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
Project Zero
Project Zero
A
About on SuperTechFans
罗磊的独立博客
WordPress大学
WordPress大学
月光博客
月光博客
Latest news
Latest news
C
Cyber Attacks, Cyber Crime and Cyber Security
GbyAI
GbyAI
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
博客园 - 三生石上(FineUI控件)
F
Fortinet All Blogs
W
WeLiveSecurity
Attack and Defense Labs
Attack and Defense Labs
V
Visual Studio Blog
Blog — PlanetScale
Blog — PlanetScale
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
P
Privacy International News Feed
AI
AI
博客园 - 司徒正美
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Stack Overflow Blog
Stack Overflow Blog
M
MIT News - Artificial intelligence
Help Net Security
Help Net Security
T
Tor Project blog
V
Vulnerabilities – Threatpost
C
Cisco Blogs
I
Intezer
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
MyScale Blog
MyScale Blog
雷峰网
雷峰网
MongoDB | Blog
MongoDB | Blog
Forbes - Security
Forbes - Security
V
V2EX
Apple Machine Learning Research
Apple Machine Learning Research
T
Threat Research - Cisco Blogs
B
Blog RSS Feed
博客园 - 叶小钗
N
News and Events Feed by Topic
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Simon Willison's Weblog
Simon Willison's Weblog
C
CERT Recently Published Vulnerability Notes
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
N
News and Events Feed by Topic

博客园 - 小乔的闺房

学习ID,ClientID,UniqueID 基础知识1 (2)最简单的Remoting程序 (1)将对象序列化为bin,soap,xml (4)迭代器 (3)集合接口 (1)学习数组,集合,IEnumerable接口,引申学习迭代器 自定义服务器控件(1)整体把握(未完待续) FindControl实现原理 location详解 使用ASP.NET AJAX实现(图片)幻灯片效果 固定GridView列字符串长度,多于的用...代替 读取Excel数据到GridView相关问题(待完善) 说明nchar(10),char(10),nvarchar(10),varchar(10) syscolumns 获得数据库里所有表的名称 类[属性扩展],属性[属性扩展](待完善) 获得数据库表的列数 WebForm里弹出警告框之内的自定义类MessageBox
(2)学习集合,引申学习索引器和泛型
小乔的闺房 · 2007-11-01 · via 博客园 - 小乔的闺房

有1个Person类,下面我们要在前台利用集合去定义3个Person对象,然后返回这3个对象的Name

public class Person
{
    private string _firstName;
    private string _lastName;

    public string Name
    {
        get
        {
            return _firstName + _lastName;
        }
    }

    public Person(string strFirstName, string strLastName)
    {
        _firstName = strFirstName;
        _lastName = strLastName;
    }
}

(1)使用数组
protected void Page_Load(object sender, EventArgs e)
{
    Person[] myPerson = new Person[3];
    myPerson[0] = new Person("Jim", "Jack");
    myPerson[1] = new Person("Jim", "Andy");
    myPerson[2] = new Person("Sun", "Bob");

    for (int i = 0; i < myPerson.Length; i++)
    {
        lblName.Text += myPerson[i].Name + "<br />";
    }
}

页面显示如下:

JimJack
JimAndy
SunBob

(2)使用ArrayList
protected void Page_Load(object sender, EventArgs e)
{
    ArrayList myArrayListPerson = new ArrayList();
    myArrayListPerson.Add(new Person("Jim", "Jack"));
    myArrayListPerson.Add(new Person("Jim", "Andy"));
    myArrayListPerson.Add(new Person("Sun", "Bob"));

    for (int i = 0; i < myArrayListPerson.Count; i++)
    {
        lblName.Text += ((Person)myArrayListPerson[i]).Name + "<br />";
    }
}

页面显示如下:

JimJack
JimAndy
SunBob

说明:
注意紫色代码部分,使用ArrayList获得Name时,我们需要进行强制转换,这样做是有风险的.(具体什么风险,等查阅相关书籍后补充)

(3)为了避免上面的风险,我们使用泛型
protected void Page_Load(object sender, EventArgs e)
{
    List<Person> myPerson = new List<Person>();
    myPerson.Add(new Person("Jim", "Jack"));
    myPerson.Add(new Person("Jim", "Andy"));
    myPerson.Add(new Person("Sun", "Bob"));

    for (int i = 0; i < myPerson.Count; i++)
    {
        lblName.Text += myPerson[i].Name + "<br />";
    }
}

页面显示如下:
JimJack
JimAndy
SunBob

说明:
1. 这里我们使用了System.Collections.Generic命名空间里已经定义好的一个泛型类List
2. 先简单说明下System.Collections.Generic命名空间里的类,具体见第二部分的详细说明
List 是对应于 ArrayList 的泛型类。
Dictionary 是对应于 Hashtable 的泛型类。
Collection 是对应于 CollectionBase 的泛型类。Collection 可以用作基类,但是与 CollectionBase 不同的是它不是抽象的,因而更易于使用。
ReadOnlyCollection 是对应于 ReadOnlyCollectionBase 的泛型类。ReadOnlyCollection 不是抽象的,它具有一个构造函数,该构造函数使其更易于将现有的 List 公开为只读集合。
Queue、Stack 和 SortedList 泛型类分别对应于与其同名的非泛型类。
具体可以参考

http://msdn2.microsoft.com/zh-cn/library/ms172181(VS.80).aspx何时使用泛型集合