草庐IT

collections4

全部标签

c# - <Collection>.Count 使用成本高吗?

我正在编写一个基本上看起来像这样的缓存弹出方法:while(myHashSet.Count>MAX_ALLOWED_CACHE_MEMBERS){EjectOldestItem(myHashSet);}我的问题是关于Count是如何确定的:它只是一个private还是protectedint,还是通过计算元素来计算每次它被调用? 最佳答案 来自http://msdn.microsoft.com/en-us/library/ms132433.aspx:RetrievingthevalueofthispropertyisanO(1)op

c# - 无法将类型 'System.Linq.IQueryable' 隐式转换为 'System.Collections.Generic.IList'

我有一个方法:publicDzieckoAndOpiekunCollectionGetChildAndOpiekunByFirstnameLastname(stringfirstname,stringlastname){DataTransfer.ChargeInSchoolEntitiesdb=newDataTransfer.ChargeInSchoolEntities();DzieckoAndOpiekunCollectionresult=newDzieckoAndOpiekunCollection();if(firstname==null&&lastname!=null){ILis

c# - 无法将类型 'System.Linq.IQueryable' 隐式转换为 'System.Collections.Generic.IList'

我有一个方法:publicDzieckoAndOpiekunCollectionGetChildAndOpiekunByFirstnameLastname(stringfirstname,stringlastname){DataTransfer.ChargeInSchoolEntitiesdb=newDataTransfer.ChargeInSchoolEntities();DzieckoAndOpiekunCollectionresult=newDzieckoAndOpiekunCollection();if(firstname==null&&lastname!=null){ILis

c# - 创建一个 NUnit 约束,意思是 "{collection} does not contain {item}"

我正在努力断言枚举中缺少特定项目。具体来说,我的测试是这样的://Takeanitemfromaqueueofscheduleditems...ItemQueuependingQueue=schedule.PendingItems;//PendingItemsisanIEnumerableintitem=pendingQueue.FirstItem;//...processtheitem...processor.DoSomethingWith(item);//...andtheschedulemustnotcontaintheitemanymore:Assert.That(schedu

c# - 创建一个 NUnit 约束,意思是 "{collection} does not contain {item}"

我正在努力断言枚举中缺少特定项目。具体来说,我的测试是这样的://Takeanitemfromaqueueofscheduleditems...ItemQueuependingQueue=schedule.PendingItems;//PendingItemsisanIEnumerableintitem=pendingQueue.FirstItem;//...processtheitem...processor.DoSomethingWith(item);//...andtheschedulemustnotcontaintheitemanymore:Assert.That(schedu

c# - 无法比较类型为 'System.Collections.Generic.ICollection`的元素 1 只支持原始类型、枚举类型和实体类型

这段代码是我写的IQueryablesites=context.MainTable.Include("RelatedTable");if(!string.IsNullOrEmpty(param1)){sites=sites.Where(s=>s.RelatedTable!=null&&s.RelatedTable.Any(p=>p.Name==param1.ToLower()&&p.PolicyType=="primary"));}foreach(stringsecondaryPolicyinsecondaryPolicies){sites=sites.Where(s=>s.Relat

c# - 无法比较类型为 'System.Collections.Generic.ICollection`的元素 1 只支持原始类型、枚举类型和实体类型

这段代码是我写的IQueryablesites=context.MainTable.Include("RelatedTable");if(!string.IsNullOrEmpty(param1)){sites=sites.Where(s=>s.RelatedTable!=null&&s.RelatedTable.Any(p=>p.Name==param1.ToLower()&&p.PolicyType=="primary"));}foreach(stringsecondaryPolicyinsecondaryPolicies){sites=sites.Where(s=>s.Relat

c# - 无法将 List<T> 隐式转换为 Collection<T>

这是一个编译器错误(为了便于阅读而略有更改)。这个问题一直困扰着我。FxCop告诉我们返回List是一件坏事和派生自Collection的类作为返回类型应该更可取。此外,FxCop表示可以使用List用于内部数据存储/操作。好的,我明白了,但我不明白为什么编译器提示试图隐式转换List至Collection.不是List更多界面收费和功能?为什么禁止隐式转换?还有另一个问题来自上面:是newList(somecollection)构造函数贵吗? 最佳答案 为什么不只执行以下操作:Collectioncollection=newCol

c# - 无法将 List<T> 隐式转换为 Collection<T>

这是一个编译器错误(为了便于阅读而略有更改)。这个问题一直困扰着我。FxCop告诉我们返回List是一件坏事和派生自Collection的类作为返回类型应该更可取。此外,FxCop表示可以使用List用于内部数据存储/操作。好的,我明白了,但我不明白为什么编译器提示试图隐式转换List至Collection.不是List更多界面收费和功能?为什么禁止隐式转换?还有另一个问题来自上面:是newList(somecollection)构造函数贵吗? 最佳答案 为什么不只执行以下操作:Collectioncollection=newCol

c# - Collection.Contains() 使用什么来检查现有对象?

我有一个自定义对象的强类型列表,MyObject,它有一个属性Id,以及其他一些属性。假设Id的MyObject将其定义为唯一,我想检查我的收藏是否还没有MyObject具有Id的对象在我添加新的MyObject之前为1到集合。我想使用if(!List.Contains(myObj)),但我如何强制执行MyObject的只有一个或两个属性这一事实将其定义为唯一?我可以使用IComparable?或者我只需要重写Equals方法?如果是这样,我需要先继承一些东西,对吗? 最佳答案 List.Contains使用EqualityComp