草庐IT

gs-collections

全部标签

C# GC.Collect 如果对象是使用实例构造函数初始值设定项构造的,则不会销毁该对象

这个问题在这里已经有了答案:关闭9年前。PossibleDuplicate:ResurrectiondifferenceinusingObjectInitializer我很难理解垃圾收集器在C#中的工作原理(我使用的是2012,所以是c#4.5)。这是我的示例代码:publicclassA{publicintc;publicA(){}publicA(intpC){c=pC;}}publicstaticvoidMain(){//Test1vara=newA{c=199};varaRef=newWeakReference(a);a=null;Console.WriteLine(aRef.I

c# - System.Collections.Generic.List<T> 需要 '1' 类型参数

我在以下代码中遇到此错误:string[]colors={"green","brown","blue","red"};varlist=newList(colors);IEnumerablequery=list.Where(c=>c.length==3);list.Remove("red");Console.WriteLine(query.Count());此外,Count()似乎不再被允许。它被弃用了吗? 最佳答案 您正在尝试创建一个List你应该告诉编译器varlist=newList(colors);没有List,有一个名为Li

c# - 奇怪的 "Collection was modified after the enumerator was instantiated"异常

也许有人可以为我指出正确的方向,因为我对此一头雾水。我有一个函数可以简单地打印出类的LinkedList:LinkedListcomponents=newLinkedList();...privatevoidPrintComponentList(){Console.WriteLine("---ComponentList:"+components.Count+"entries---");foreach(Componentcincomponents){Console.WriteLine(c);}Console.WriteLine("------");}Component对象实际上有一个自定

c# - 避免 InvalidOperationException : Collection was modified? 的最佳实践

我经常需要这样的东西:foreach(Linelineinlines){if(line.FullfilsCertainConditions()){lines.Remove(line)}}这不起作用,因为我总是得到一个InvalidOperationException,因为Enumerator在循环期间被更改了。所以我将所有此类循环更改为以下内容:Listremove=newList();foreach(Linelineinlines){if(line.FullfilsCertainConditions()){remove.Add(line)}}foreach(Linelineinrem

c# - "Collection was of a fixed size"带有 POCO 的 EF4 异常

我将EF4与WCF和POCO结合使用。我删除了POCO实体中的所有虚拟关键字。我有Employee和Team实体,两者之间的关系是1:N,意味着一名员工只能分配到一个团队。我想在现有团队中添加新员工。以下代码在客户端。privatevoidbtnAdd_Click(objectsender,RoutedEventArgse){TeamteamFromDb=ServiceProxy.GetService.GetTeamById(181);EmployeenewEmp=newEmployee{UserName="username"};newEmp.Team=teamFromDb;Servi

c# - DDD : entity's collection and repositories

假设我有publicclassProduct:Entity{publicIListItems{get;set;}}假设我想找到一个最大的项目...我可以添加方法Product.GetMaxItemSmth()并使用Linq(fromiinItemsselecti.smth).Max())或使用手动循环或其他方式。现在,问题是这会将整个集合加载到内存中。正确的解决方案是进行特定的数据库查询,但域实体无权访问存储库,对吧?所以要么我做productRepository.GetMaxItemSmth(product)(这很丑,不是吗?),或者即使实体可以访问存储库,我也使用来自实体的IPro

c# - System.Collections.Generic.List 中的某项在什么情况下不会被删除成功?

在什么情况下System.Collections.Generic.List中的item不会被成功移除?来自http://msdn.microsoft.com/en-us/library/cd666k3e.aspx:trueifitemissuccessfullyremoved;otherwise,false.ThismethodalsoreturnsfalseifitemwasnotfoundintheList(OfT).他们表达它的方式让我认为对List(OfT)中找到的项目的删除操作实际上可能会失败,因此这个问题。 最佳答案 查

C# 应用程序设置 : Is there a easy way to put a collection into <appSetting>

我试过了和System.Configuration.ConfigurationManager.AppSettings.GetValues("List");但我只得到最后一个成员。我怎样才能轻松解决这个问题? 最佳答案 我处理过类似的问题,我是用这段代码解决的。希望这对您的问题有所帮助。在这种情况下,列表(类似于我的URLSection)将在web.config中有一个完整的配置部分,然后您可以从该部分获取所有值。我为此创建了三个类:ConfigElement、ConfigElementCollection、WebConfigSect

c# - 如何就地对 Collection<T> 进行排序?

我有一个通用集合:publicItems:Collection{protectedoverridevoidInsertItem(intindex,Objectitem){base.InsertItem(index,item);...}protectedoverridevoidRemoveItem(intindex){base.RemoveItem(index);...}protectedoverridevoidSetItem(intindex,Objectitem){base.SetItem(index,item);...}protectedoverridevoidClearItems

c# - 无法将带有 [] 的索引应用于 mvc Controller 中类型为“System.Collections.Generic.ICollection<int>”的表达式

publicActionResultaddstandardpackage1(ICollectionSingleStay,ICollectionDOUBLESTAY,ICollectionTRIBLESTAY,ICollectionFAMILYSTAY,ICollectionEXTRABED){vars=SingleStay;for(inti=0;i在for循环中,我收到类似无法将带[]的索引应用于类型表达式的错误,但我需要在for循环中,在我得到的每个中。因为基于for循环,我会将详细信息与其他集合列表绑定(bind)。请帮助我。我在varcal=Singlestay[i]中遇到错误。