代码之家  ›  专栏  ›  技术社区  ›  Michael Edwards

列表和图标集

  •  0
  • Michael Edwards  · 技术社区  · 14 年前

    查看界面 System.Collections.Generic.ICollection 其定义要求继承成员包含属性 bool isreadonly_get; .

    但是我看了看班上的学生 system.collections.generic.列表 继承的 System.Collections.Generic.ICollection 而这个类不包含 bool isreadonly_get; . 继承链是如何断开的,还是我丢失了什么?

    5 回复  |  直到 14 年前
        1
  •  1
  •   stephbu    14 年前
        2
  •  1
  •   CRice    14 年前

    在IList部分:

    IList实现ICollection

        public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable
        {
            public List();
            public List(int capacity);
            public List(IEnumerable<T> collection);
            public int Capacity { get; set; }
    
            #region IList Members
    
            int IList.Add(object item);
            bool IList.Contains(object item);
            void ICollection.CopyTo(Array array, int arrayIndex);
            int IList.IndexOf(object item);
            void IList.Insert(int index, object item);
            void IList.Remove(object item);
            bool IList.IsFixedSize { get; }
            bool IList.IsReadOnly { get; }
            bool ICollection.IsSynchronized { get; }
            object ICollection.SyncRoot { get; }
            object IList.this[int index] { get; set; }
    
            #endregion
    
    ...and so on
    
    }
    
        3
  •  1
  •   Community CDub    8 年前

    这个 IsReadOnly 财产 那里,但是 List<T> 正在实施 explicitly .

    为了让自己相信这一点,你可以做到:

    List<T> genericList = new List<T>();
    IList explicitIList = genericList;
    
    bool isReadOnly = explicitIList.IsReadOnly;
    

    这应该编译。

    你也可能想看看这个 question 而这 article 关于如何显式实现接口,以及如何从类型外部引用类型上显式实现的成员。

        4
  •  0
  •   Dima    14 年前

    对。它是显式实现的。所以您应该以这种方式访问它的成员(显式地将其强制转换为接口) ((ICollection)列表).isreadonly;

        5
  •  0
  •   Cheng Chen    14 年前

    从System.Collections.Generic.List的.NET Reflector中的反汇编代码中,它确实包含 IsReadOnly 财产。

     bool ICollection<T>.IsReadOnly { get; }