本文对常用的数据结构详述:Array, ArrayList,List,IList,ICollection, Stack, Queue, HashTable, Dictionary, IQueryable ICollection 继承了IEnumberable,但是IEnumberable是基于索引的,ICollection不基于索引。 ? 1: //IList {indexer and Modify} vs ICollection {randomly and Modify} 2: //Collection can not be instantiate from ICollection , so it should be instantiate from List 3: System.Collections.Generic.ICollection *** 29: System.Collections.Generic.ICollection<Employee> objICollection = new List<Employee>();
非泛型版本的 ICollection 中有 IsSynchronized 属性和 SyncRoot 属性,这两个属性被用来设计成以线程安全的方式访问和修改集合。 虽然泛型版本的 ICollection<T> 已经改进了设计,不再引入 SyncRoot 这样的属性到接口中,但如果我们在某些场景下需要实现 ICollection 非泛型集合时,如何正确实现 SyncRoot 而 ICollection 接口中的 SyncRoot 属性在接口中必然是公开的,于是没有任何途径可以保证调用方不会发生死锁。 bool ICollection.IsSynchronized => false; // Synchronization root for this object. object ICollection.SyncRoot
(key);
}
///
public int StandardId { get; set; } public string StandardName { get; set; } public ICollection <Student> CurrentStudents { get; set; } public ICollection<Student> PreviousStudents { get; set; public string StandardName { get; set; } [InverseProperty("CurrentStandard")] public ICollection public string StandardName { get; set; } [InverseProperty("CurrentStandard")] public ICollection Student> CurrentStudents { get; set; } [InverseProperty("PreviousStandard")] public ICollection
4)IDictionary IDictionary实现是键/值对的集合,它本身实现了ICollection和IEnumerable接口 是键/值对的集合的基接口。 3)Hashtable 实现了接口:IDictionary、ICollection、IEnumerable 可以向Hashtable中自由添加和删除元素,有些像ArrayList,但没有那么大的性能开销 4)SortedList 实现了接口:IDictionary、ICollection、IEnumerable SortedLIst兼顾了ArrayList和Hashtable的优点,可按键值来排序 5 )Queue 实现了接口:ICollection、IEnumerable Queque是队列,先进先出的访问各个元素 可以调用Queque对象的GetEnumerator()方法,得到IEnumerator 对象,来遍历队列中的各个元素 6)Stack 实现了接口:ICollection、IEnumerable Stack是堆栈,后进先出的访问各个元素 可以调用Stack对象的GetEnumerator()
下面是一个用CuttingEdge.Conditions的例子: public ICollection GetData(Nullable<int> id, string xml, ICollection Check all postconditions: Condition.Ensures(result, "result") .IsOfType(typeof(ICollection )); // throws PostconditionException on failure return (ICollection)result; } public static
基类型/接口 Suffix System.Attribute 属性 System.EventArgs EventArgs System.Exception 例外 System.Collections.ICollection TValue> 字典 System.Collections.Queue 集合或队列 System.Collections.Stack 集合或堆栈 System.Collections.Generic.ICollection EventHandler 实现 ICollection 的类型是一种通用的数据结构类型(如字典、堆栈或队列),允许在名称中包含有关该类型预期用途的有用信息。 实现 ICollection 的类型是特定项的集合,其名称以单词 Collection 结尾。 例如,Queue 对象的集合的名称会是 QueueCollection。 这些集合通过 System.Data.InternalDataCollectionBase 基类实现 ICollection。 如何解决冲突 重命名该类型,使其带有正确的字词后缀。
System.Data.Metadata.Edm.MetadataArtifactLoaderCompositeResource.LoadResources(String assemblyName, String resourceName, ICollection System.Data.Metadata.Edm.MetadataArtifactLoaderCompositeResource.CreateResourceLoader(String path, ExtensionCheck extensionCheck, String validExtension, ICollection System.Data.Metadata.Edm.MetadataArtifactLoader.Create(String path, ExtensionCheck extensionCheck, String validExtension, ICollection
一、第二次优化 上述代码基本上囊括了大部分情况,但有时我们还会遇到一些集合只实现了 ICollection 而没有实现 IList 的情况,这种情况下我们代码中的 GetEnumerator 方法性能就不是很高了 public IEnumerator<T> GetEnumerator() { if(orgSequence == null) { if(orgSequence is ICollection <T>) { ICollection<T> src = orgSequence as ICollection<T>; orgSequence DemoStringEnumerator(sreSequence as string); } if(orgSequence == null) { if(orgSequence is ICollection <T>) { ICollection<T> src = orgSequence as ICollection<T>; orgSequence
如何解决冲突 若要解决此规则的冲突,请实现某个泛型集合接口: System.Collections.Generic.IEnumerable<T> System.Collections.Generic.ICollection 通过接口实现来解决 以下示例通过实现 IEnumerable<T>、ICollection<T> 和 IList<T> 等泛型接口来解决冲突。 <Book>.Add(Book item) { } bool ICollection<Book>.Contains(Book item) { return true; } void ICollection<Book>.CopyTo(Book[] array, int arrayIndex) { } bool ICollection <Book>.IsReadOnly { get { return false; } } bool ICollection<Book>.Remove(Book item
如果你用ICollection接口来写那段程序,你就不用修改那段程序了,对于任何实现ICollection接口的类型它都能很好的工作: 1 private void LoadList (ICollection ListBox l) 2 { 3 foreach (object o in items) 4 l.Items.Add (o.ToString ()); 5 } ICollection 此外,多维数组也支持ICollection接口。如果那还不够的话,数据库.NET类同样支持ICollection接口。用接口写的这个函数不用需改就可以才许多中情况下使用。 2.
public class Department { public string DepartmentName { get; set; } public ICollection public class Role { public string RoleName { get; set; } public ICollection <User> Users { get; set; } public ICollection<Action> Actions { get; set; } } /// < class Department: IDepartment { public string DepartmentName { get; set; } public ICollection class Department: IDepartment { public string DepartmentName { get; set; } public ICollection
ICollection是集合接口,支持着集合的Count属性和CopyTo操作,另外还有同步的属性IsSynchronized(判断是否线程安全)和SyncRoot(lock的对象)。 泛型部分基本是上面这些接口的泛型实现,不过IList<T>的一些操作放到ICollection<T>里了,可能微软也觉得对于集合的一些操作放到ICollection更合理吧。 <T> c = collection as ICollection<T>; 8 if (c ! 如果不是ICollection,不过由于是IEnumerable,所以可以遍历,一个一个加到_items里。 属性 Count 返回的是_size,这个是元素的实际个数,不是数组大小。 1 Object System.Collections.ICollection.SyncRoot 2 { 3 get 4 { 5 if (_syncRoot ==
System.Collections.Immutable.ImmutableArray<T> 此规则标记具有 Count 属性的以下集合类型上的 Count 调用: System.Collections.ICollection System.Collections.Generic.ICollection<T> System.Collections.Generic.IReadOnlyCollection<T> 分析后的集合类型可能会在将来扩展 System.Linq; class C { public int GetCount(int[] array) => array.Count(); public int GetCount(ICollection System.Collections.Generic; class C { public int GetCount(int[] array) => array.Length; public int GetCount(ICollection
Name; arr[2] = "> "; } // 判断泛型类型是否为 ICollection <T> else if (typeDefinition == typeof(ICollection<>)) { arr[0] = "ICollection<"; arr[1] = typeArgs[0].Name; arr
public class Cart { public int Id { get; set; } public ICollection<Item> Items { get; set; } get; set; } public string Name { get; set; } public int Quantity { get; set; } public ICollection 您会得到以下结果: Unable to determine the relationship represented by navigation property ‘Cart.Items’ of type ‘ICollection 【无法确定类型为“ICollection<Item>”的导航属性“Cart.Items”表示的关系。 get; set; } public string Name { get; set; } public int Quantity { get; set; } public ICollection
俗话说知其然,知其所以然,平常看到IEnumerable,IEnumerator,ICollection是不是知道他们之间各自的区别?除了List和Dictionary以外,你还用过哪些其它的集合类? <T>和ICollection 从最上面第一张图我们可以知道,ICollection是直接继承自IEnumerable。 ICollection 与ICollection<T> 略有不同,ICollection不提供编辑集合的功能,即Add和Remove。包括检查元素是否存在Contains也不支持。 IList<T>和IList IList则是直接继承自ICollection和IEnumerable。所以它包括两者的功能,并且支持根据下标访问和添加元素。 而ICollection支持的功能稍微多一点,不仅有遍历还有维护这个集合的功能。而IList是最全的版本。
public class ShoppingCart { public int Id { get; set; } public ICollection<Commodity> Commoditys { get; set; } public string Name { get; set; } public int Quantity { get; set; } public ICollection to determine the relationship represented by navigation property 'ShoppingCart.Commoditys' of type 'ICollection ShoppingCart和Commodity的导航属性: public class ShoppingCart { public int Id { get; set; } public ICollection { get; set; } public string Name { get; set; } public int Quantity { get; set; } public ICollection
基类型/接口 Suffix System.Attribute 属性 System.EventArgs EventArgs System.Exception 例外 System.Collections.ICollection TValue> 字典 System.Collections.Queue 集合或队列 System.Collections.Stack 集合或堆栈 System.Collections.Generic.ICollection EventHandler 实现 ICollection 的类型是一种通用的数据结构类型(如字典、堆栈或队列),允许在名称中包含有关该类型预期用途的有用信息。 实现 ICollection 的类型是特定项的集合,其名称以单词 Collection 结尾。 例如,Queue 对象的集合的名称会是 QueueCollection。 这些集合通过 System.Data.InternalDataCollectionBase 基类实现 ICollection。 如何解决冲突 重命名该类型,使其带有正确的字词后缀。
基类型/接口 Suffix System.Attribute 属性 System.EventArgs EventArgs System.Exception 例外 System.Collections.ICollection TValue> 字典 System.Collections.Queue 集合或队列 System.Collections.Stack 集合或堆栈 System.Collections.Generic.ICollection EventHandler 实现 ICollection 的类型是一种通用的数据结构类型(如字典、堆栈或队列),允许在名称中包含有关该类型预期用途的有用信息。 实现 ICollection 的类型是特定项的集合,其名称以单词 Collection 结尾。 例如,Queue 对象的集合的名称会是 QueueCollection。 这些集合通过 System.Data.InternalDataCollectionBase 基类实现 ICollection。 如何解决冲突 重命名该类型,使其带有正确的字词后缀。