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

推荐订阅源

Google DeepMind News
Google DeepMind News
N
Netflix TechBlog - Medium
The Register - Security
The Register - Security
C
Cybersecurity and Infrastructure Security Agency CISA
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The Hacker News
The Hacker News
P
Proofpoint News Feed
Project Zero
Project Zero
The GitHub Blog
The GitHub Blog
The Last Watchdog
The Last Watchdog
F
Fortinet All Blogs
S
Schneier on Security
Help Net Security
Help Net Security
Security Archives - TechRepublic
Security Archives - TechRepublic
C
Check Point Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
P
Proofpoint News Feed
I
InfoQ
T
The Blog of Author Tim Ferriss
Cisco Talos Blog
Cisco Talos Blog
Stack Overflow Blog
Stack Overflow Blog
T
Troy Hunt's Blog
人人都是产品经理
人人都是产品经理
T
Threatpost
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
Cyber Attacks, Cyber Crime and Cyber Security
雷峰网
雷峰网
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿
Forbes - Security
Forbes - Security
Vercel News
Vercel News
S
Security Affairs
美团技术团队
P
Privacy & Cybersecurity Law Blog
N
News and Events Feed by Topic
Cyberwarzone
Cyberwarzone
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Jina AI
Jina AI
Spread Privacy
Spread Privacy
Attack and Defense Labs
Attack and Defense Labs
IT之家
IT之家
U
Unit 42
Recorded Future
Recorded Future
W
WeLiveSecurity
PCI Perspectives
PCI Perspectives
P
Palo Alto Networks Blog
H
Hacker News: Front Page
S
Security @ Cisco Blogs
博客园 - 【当耐特】

博客园 - 小乔的闺房

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

发展:数组-->集合-->泛型

(1)数组


1. 数组数据结构是System.Array类的一个实例.
2. System.Array类的语法为
[SerializableAttribute]
[ComVisibleAttribute(true)]
public abstract class Array : ICloneable, IList, ICollection, IEnumerable
3. 下面看一个使用数组的例子(我称之为隐式实现)
protected void Page_Load(object sender, EventArgs e)
{
    string[] strArrName = new string[3] { "Jim", "Andy", "Sun" };
    foreach (string strName in strArrName)
    {
        lblName.Text += strName + ",";
    }
}
或者这样写
protected void Page_Load(object sender, EventArgs e)
{
    string[] strArrName = new string[3];
    strArrName[0] = "Jim";
    strArrName[1] = "Andy";
    strArrName[2] = "Sun";
    foreach (string strName in strArrName)
    {
        lblName.Text += strName + ",";
    }
}
显示结果如下
Jim,Andy,Sun,
4. 下面看另一个使用数组的例子(我称之为显式实现)
protected void Page_Load(object sender, EventArgs e)
{
    Array myArray = Array.CreateInstance(typeof(string), 3);
    myArray.SetValue("Jim", 0);
    myArray.SetValue("Andy", 1);
    myArray.SetValue("Sun", 2);
    foreach (string strName in myArray)
    {
        lblName.Text += strName + ",";
    }
}
显示结果如下
Jim,Andy,Sun,
5. 优点:可以高效的访问给定下标的元素;System.Array类有自己的C#语法,使用它编程非常的直观.
6. 缺点:在实例化时必须指定数组的大小,以后也不能添加,插入,删除元素.

(2)集合
1. 针对数组数据结构的缺点,我们使用集合数据结构.
2. 集合数据结构中的类都位于System.Collections命名空间中.
3. 说到集合,我们必须先了解几个接口.想具体了解以下接口,可参考(3)集合接口
3.1 IEnumerable接口和IEnumerator接口
3.2 ICollection接口
public interface ICollection : IEnumerable
3.3 IList接口
public interface IList : ICollection, IEnumerable
3.4 IDictionary接口
public interface IDictionary : ICollection, IEnumerable
4. 说明一下:
4.1 ICollection接口是System.Collections命名空间中类的基接口.
4.2 ICollection接口扩展IEnumerable;IDictionary和IList则是扩展ICollection的更为专用的接口.IDictionary实现是键/值对的集合,如Hashtable类.IList实现是值的集合,其成员可通过索引访问,如ArrayList类.
4.3 某些集合(如Queue类和Stack类)限制对其元素的访问,它们直接实现ICollection接口.
4.4 如果IDictionary接口和IList接口都不能满足所需集合的要求,则从ICollection接口派生新集合类以提高灵活性.

(3)IEnumerable接口和IEnumerator接口

1. 我的理解:只有实现了IEnumerable接口的数据结构类才能使用foreach语句,下面给出例子
//Person类
public class Person
{
    private string _firstName;
    private string _lastName;

    public string FirstName
    {
        get { return _firstName; }
    }
    public string LastName
    {
        get { return _lastName; }
    }

    public Person(string strFirstName, string strLastName)
    {
        this._firstName = strFirstName;
        this._lastName = strLastName;
    }
}
//PersonList集合,实现IEnumerable接口
public class PersonList : IEnumerable
{
    private Person[] _arrPerson;

    public PersonList(Person[] myArrPerson)
    {
        _arrPerson = new Person[myArrPerson.Length];
        for (int i = 0; i < myArrPerson.Length; i++)
        {
            _arrPerson[i] = myArrPerson[i];
        }
    }

    public IEnumerator GetEnumerator()
    {
        return new PeopleEnumerator(_arrPerson);
    }
}
//实现IEnumerator接口
public class PeopleEnumerator : IEnumerator
{
    private int _position = -1;
    public Person[] _arrPerson;

    public object Current
    {
        get
        {
            try
            {
                return _arrPerson[_position];
            }
            catch (IndexOutOfRangeException)
            {
                throw new InvalidOperationException();
            }
        }
    }

    public PeopleEnumerator(Person[] myArrPerson)
    {
        _arrPerson = myArrPerson;
    }

    public bool MoveNext()
    {
        _position++;
        return (_position < _arrPerson.Length);
    }

    public void Reset()
    {
        _position = -1;
    }
}
//集合的使用
protected void Page_Load(object sender, EventArgs e)
{
    Person[] myArrPerson = new Person[3]
    {
        new Person("John", "Smith"),
        new Person("Jim", "Johnson"),
        new Person("Sue", "Rabon"),
    };

    PersonList myPersonList = new PersonList(myArrPerson);
    foreach (Person myPerson in myPersonList)
    {
        lblName.Text += myPerson.FirstName + " " + myPerson.LastName + ",";
    }
}
6. 说明一下我的理解,定义了一个集合类,实现IEnumerable接口,则必须定义一个与之相应的实现IEnumerator接口的类,这样是不是很麻烦呢?

(4)迭代器
1. 使用迭代器可避免上述麻烦,修改代码,注意橙色部分
public class Person
{
    private string _firstName;
    private string _lastName;

    public string FirstName
    {
        get { return _firstName; }
    }
    public string LastName
    {
        get { return _lastName; }
    }

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

public class PersonList : IEnumerable
{
    private Person[] _arrPerson;

    public PersonList(Person[] myArrPerson)
    {
        _arrPerson = new Person[myArrPerson.Length];
        for (int i = 0; i < myArrPerson.Length; i++)
        {
            _arrPerson[i] = myArrPerson[i];
        }
    }

    public IEnumerator GetEnumerator()
    {
        //当编译器检测到迭代器时,它将自动生成IEnumerable或IEnumerable<T>接口的Current,MoveNext和Dispose方法. 
        for (int i = 0; i < _arrPerson.Length; i++)
        {
            yield return _arrPerson[i];
        }

    }
}
2. 深入研究迭代器,请见(4)迭代器
3. 参考:http://msdn2.microsoft.com/zh-cn/library/dscyy5s0(VS.80).aspx