当前位置:编程学习 > C#/ASP.NET >>

新手求教,关于C#中集合的问题!!!!!!!!!!!!!!

     各位前辈好,小弟刚开始学习C#,看的是《C#入门经典》,由于有些C++的基础,前面看的很顺利,结果看到第11章集合、比较和转换时一下子就卡住了,其中246页说“为便于完成任务,CollectionBase提供了两个受保护的属性,他们可以访问存储的对象本身。我们可以使用List和InnerList,List可以通过IList访问接口访问项,InnerList则是用于存储项的ArrayList对象。”
     1.  问题来了,这里面所说的,“List可以通过IList接口访问项”是什么意思??????

      public class Animals:CollectionBase

       ......
       public void Add(Animal newAnimal)
       {
           List.Add(newAnimal);
       }
       ......
}

     2.  第二个问题,List.Add(newAnimal);是怎么回事?List是CollectionBase中的一个属性啊?又不是类为什么可以这样访问IList接口中的成员???????
     3.   第三个问题,我看了CollectionBase类的源码,如下:
namespace System.Collections
{

    public abstract class CollectionBase : IList, ICollection, IEnumerable
    {
        protected CollectionBase();
        protected CollectionBase(int capacity);
        public int Capacity { get; set; }
        public int Count { get; }
        protected ArrayList InnerList { get; }
        protected IList List { get; }
        public void Clear();
        public IEnumerator GetEnumerator();
        protected virtual void OnClear();
        protected virtual void OnClearComplete();
        protected virtual void OnInsert(int index, object value);
        protected virtual void OnInsertComplete(int index, object value);
        protected virtual void OnRemove(int index, object value);
        protected virtual void OnRemoveComplete(int index, object value);
        protected virtual void OnSet(int index, object oldValue, object newValue);
        protected virtual void OnSetComplete(int index, object oldValue, object newValue);
        protected virtual void OnValidate(object value);
        public void RemoveAt(int index);
    }
}
       问题是,既然CollectionBase继承了IList接口,那为什么CollectionBase中却没有IList接口中方法的实现????????
       自学路上遇到诸多困难,请大家指教,小弟在这里先谢过了!!!!!!!! --------------------编程问答--------------------

            //问题1:
            IList<int> s = new List<int>();
            s.Add(0);
            s[0] = s[0];
            s.RemoveAt(0);
            //问题2:这个List是一个属性或变量,如下。属性可以与类同名而且某些情况下推荐这样做,不过List不是一个好属性名
            var List = new List<int>();
            List.Add(0);
            //问题3:不知道你从哪里看的源码,不过我的源码里CollectionBase是实现了IList的,不过是显式实现,也许这是你的源码里没列出的原因?

--------------------编程问答-------------------- 1、因为List就是一个IList的实例,注意那行protected IList List { get; }
2、正因为List是一个可读属性,你可以得到一个实现IList的实例,并对他进行访问。
3、你看到的是CollectionBase中public和protected的方法和属性,而CollectionBase对IList的实现用了”显式接口实现“,这种实现不是public或protected,Visual Studio显示Metadata时候,把它们排除在外。

显式接口实现:http://msdn.microsoft.com/zh-cn/library/vstudio/ms173157.aspx --------------------编程问答--------------------
引用 2 楼 gomoku 的回复:
1、因为List就是一个IList的实例,注意那行protected IList List { get; }
2、正因为List是一个可读属性,你可以得到一个实现IList的实例,并对他进行访问。
3、你看到的是CollectionBase中public和protected的方法和属性,而CollectionBase对IList的实现用了”显式接口实现“,这种实现不是public或protected,Visual Studio显示Metadata时候,把它们排除在外。

显式接口实现:http://msdn.microsoft.com/zh-cn/library/vstudio/ms173157.aspx

但是IList是一个接口啊,可以用接口产生实例吗? --------------------编程问答-------------------- 一个实现IList的实例 --------------------编程问答--------------------
引用 4 楼 gomoku 的回复:
一个实现IList的实例

还是有些没懂,这句话protected IList List { get; }说明了List实现了IList? --------------------编程问答-------------------- 第一第二,里氏替换原则,子类可以替换父类,
第三个  public void RemoveAt(int index); --------------------编程问答--------------------
引用 5 楼 inuyasha0618 的回复:
还是有些没懂,这句话protected IList List { get; }说明了List实现了IList?

这是恰巧这个属性的名字叫List。

如果它写成:
protected IList MyList { get; }
我可以说MyList是一个实现IList的实例,不是吗? --------------------编程问答-------------------- 你下载一个reflertor,反射这个类应该就明白了

protected IList List { get; }

内部 代码 如下

protected IList List
{
    get
    {
        return this;
    }
}

Add方法

int IList.Add(object value)
{
    this.OnValidate(value);
    this.OnInsert(this.InnerList.Count, value);
    int index = this.InnerList.Add(value);
    try
    {
        this.OnInsertComplete(index, value);
    }
    catch
    {
        this.InnerList.RemoveAt(index);
        throw;
    }
    return index;
}

 

 

 
--------------------编程问答--------------------
引用 8 楼 donglizhong 的回复:
你下载一个reflertor,反射这个类应该就明白了

protected IList List { get; }

内部 代码 如下

protected IList List
{
    get
    {
        return this;
    }
}

Add方法

int IList.Add(object value)
{
    this.OnValidate(value);
    this.OnInsert(this.InnerList.Count, value);
    int index = this.InnerList.Add(value);
    try
    {
        this.OnInsertComplete(index, value);
    }
    catch
    {
        this.InnerList.RemoveAt(index);
        throw;
    }
    return index;
}

 

 

 
请问接口IList中的Add()方法是在CollectionBase类中实现的吗?为什么在VS2010中看不到啊?
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,