jjzjj

collection

全部标签

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# - 从 Active Directory 获取所有直接报告

我正在尝试通过ActiveDirectory递归地获取用户的所有直接下属。因此,给定一个用户,我最终会得到一个所有用户的列表,这些用户有这个人作为经理,或者有一个人作为经理,有一个人作为经理......最终有输入用户作为经理。我目前的尝试很慢:privatestaticCollectionGetDirectReportsInternal(stringuserDN,outlongelapsedTime){Collectionresult=newCollection();Collectionreports=newCollection();Stopwatchsw=newStopwatch()

c# - C# List<T> 的线程安全给读者

我计划在静态构造函数中创建一次列表,然后让该类的多个实例同时读取它(并枚举它)而不进行任何锁定。在本文中http://msdn.microsoft.com/en-us/library/6sh2ey19.aspxMS是这样描述线程安全问题的:Publicstatic(SharedinVisualBasic)membersofthistypearethreadsafe.Anyinstancemembersarenotguaranteedtobethreadsafe.AListcansupportmultiplereadersconcurrently,aslongasthecollectio

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# - 如何在反射中迭代列表

我有一个名为Students的属性,类型为List.通过反射我可以得到StudentsProperty的值。现在的问题是如何迭代学生列表。我需要检查StudentID[somevalue]是否在该集合中。varcollection=studentPro.GetValue(studentObj,null);//Ineedtoiteratelikethis,foreach(varitemincollection){if(item.StudentID==33)//Dostuff}请帮帮我。 最佳答案 你只需要施放它:varcollecti

c# - 我们可以在 foreach 中使用多个变量吗

我们可以在foreach中使用多个变量吗foreach(varitem1incollection1;varitems2incollection2){}我想这样做是因为我需要从数据库中获取两个集合并将它们都附加到ComboBox。 最佳答案 使用LINQ连接数组,将结果放入匿名类型,然后迭代生成的集合。varcol=collection1.Join(collection2,x=>x,y=>y,(x,y)=>new{X=x,Y=y});foreach(varentryincol){//entry.X,entry.Y}编辑:在发布答案时,

c# - 两个或多个线程可以毫无问题地迭代同一个 List<t> 吗?

谈论System.Collections.Generic.List在这里。通过下面的例子,Method1和Method2可以在不同的线程上同时执行吗?谢谢classTest{privatereadonlyList_data;publicTest(){_data=LoadData();}privateListLoadData(){//Getdatafromdv.}publicvoidMethod1(){foreach(varlistin_data){//dosomething}}publicvoidMethod2(){foreach(varlistin_data){//dosomethi

c# - LINQ to Entities 无法识别方法 'System.String get_Item (System.String)' ,

我该如何解决这个问题?这是我的代码:DateTimedtInicio=newDateTime();DateTimedtFim=newDateTime();Int32codStatus=0;if(!string.IsNullOrEmpty(collection["txtDtInicial"]))dtInicio=Convert.ToDateTime(collection["txtDtInicial"]);if(!string.IsNullOrEmpty(collection["txtDtFinal"]))dtFim=Convert.ToDateTime(collection["txtDt