Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 列表和ICollections_C#_Inheritance_List_Interface - Fatal编程技术网

C# 列表和ICollections

C# 列表和ICollections,c#,inheritance,list,interface,C#,Inheritance,List,Interface,查看接口System.Collections.Generic.ICollection其定义要求继承成员包含属性bool IsReadOnly{get;} 然而,我随后查看了类System.Collections.Generic.List,该类继承了System.Collections.Generic.ICollection,并且该类不包含bool IsReadOnly{get;}的定义。继承链是如何断开的,还是我遗漏了什么?存在IsReadOnly属性,但是List正在实现它 要让自己相信这一点

查看接口System.Collections.Generic.ICollection其定义要求继承成员包含属性bool IsReadOnly{get;}


然而,我随后查看了类System.Collections.Generic.List,该类继承了System.Collections.Generic.ICollection,并且该类不包含bool IsReadOnly{get;}的定义。继承链是如何断开的,还是我遗漏了什么?

存在
IsReadOnly
属性,但是
List
正在实现它

要让自己相信这一点,您可以:

List<T> genericList = new List<T>();
IList explicitIList = genericList;

bool isReadOnly = explicitIList.IsReadOnly;
List genericList=new List();
IList explicitIList=通用列表;
bool isReadOnly=explicitIList.isReadOnly;
这应该可以编译


您可能还希望了解如何显式实现接口,以及如何从类型外部引用类型上显式实现的成员。

存在
IsReadOnly
属性,但
List
正在实现它

要让自己相信这一点,您可以:

List<T> genericList = new List<T>();
IList explicitIList = genericList;

bool isReadOnly = explicitIList.IsReadOnly;
List genericList=new List();
IList explicitIList=通用列表;
bool isReadOnly=explicitIList.isReadOnly;
这应该可以编译


您可能还希望了解如何显式实现接口,以及如何从类型外部引用类型上显式实现的成员。

它位于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

}
公共类列表:IList、ICollection、IEnumerable、IList、ICollection、IEnumerable
{
公共列表();
公共列表(国际容量);
公共列表(IEnumerable集合);
公共整数容量{get;set;}
#国际劳工组织成员区域
int IList.Add(对象项);
bool IList.Contains(对象项);
void ICollection.CopyTo(数组,int-arrayIndex);
int IList.IndexOf(对象项);
void IList.Insert(int索引,对象项);
作废IList.Remove(对象项);
bool IList.IsFixedSize{get;}
bool-IList.IsReadOnly{get;}
bool ICollection.IsSynchronized{get;}
对象ICollection.SyncRoot{get;}
对象IList.this[int index]{get;set;}
#端区
等等
}

它位于ILST部分:

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

}
公共类列表:IList、ICollection、IEnumerable、IList、ICollection、IEnumerable
{
公共列表();
公共列表(国际容量);
公共列表(IEnumerable集合);
公共整数容量{get;set;}
#国际劳工组织成员区域
int IList.Add(对象项);
bool IList.Contains(对象项);
void ICollection.CopyTo(数组,int-arrayIndex);
int IList.IndexOf(对象项);
void IList.Insert(int索引,对象项);
作废IList.Remove(对象项);
bool IList.IsFixedSize{get;}
bool-IList.IsReadOnly{get;}
bool ICollection.IsSynchronized{get;}
对象ICollection.SyncRoot{get;}
对象IList.this[int index]{get;set;}
#端区
等等
}

成员是明确实现的:


成员是明确实现的:


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

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

从System.Collections.Generic.List的.NET reflector中的反汇编代码中,它确实包含
IsReadOnly
属性

 bool ICollection<T>.IsReadOnly { get; }
bool ICollection.IsReadOnly{get;}

根据System.Collections.Generic.List的.NET reflector中的反汇编代码,它确实包含
IsReadOnly
属性

 bool ICollection<T>.IsReadOnly { get; }
bool ICollection.IsReadOnly{get;}