草庐IT

collection-initializer

全部标签

c# - EF 4.1 和 "Collection was modified; enumeration operation may not execute."异常

在过去的2天里,这让我抓狂。我有3个非常基本的类(好吧,为了便于阅读而减少了)publicclassEmployee{publicstringName{set;get;}virtualpublicEmployerEmployer{set;get;}publicEmployee(stringname){this.Name=name;}},//thisbasicallytiesEmployeeandhisroleinacompany.publicclassEmployeeRole{publicintId{set;get;}virtualpublicEmployeeEmployee{set;

c# - “不要公开通用列表”,为什么在方法参数中使用 collection<T> 而不是 list<T>

我正在使用FxCop,它显示“不要公开通用列表”的警告,建议使用Collection而不是List.首选它的原因,我知道所有这些东西,如thisSOpost中所述和MSDN以及我浏览过的更多文章。但我的问题是,我很少有方法可以进行如此繁重的计算,并且方法接受List的参数。就性能而言,这应该更快更好。但是FxCop也为此发出警告。所以一个选择是我应该将参数声明为Collection,然后使用ToList()在方法内部,然后使用它。那么优化了哪一个呢?“抑制这种情况下的警告”或“在参数中使用Collection,然后在方法本身内部使用ToList()”。 最佳

c# - 错误 : Cannot implicitly convert type 'void' to 'System.Collections.Generic.List'

我正在尝试使用该控件从.ascx设置我的.ascx控件的属性。所以在我的一个包含此控件的.aspx中,我有以下代码试图设置我的嵌入式.ascx的ItemsList属性:Itemitem=GetItem(itemID);myUsercontrol.ItemList=newList().Add(item);我尝试设置的.ascx中的属性如下所示:publicListItemsList{get{returnthis.itemsList;}set{this.itemsList=value;}}错误:无法将类型“void”隐式转换为“System.Collections.Generic.List

C# 6.0 的新字典初始化器 - 说明

这个问题在这里已经有了答案:Whatbenefitsdoesdictionaryinitializersaddovercollectioninitializers?(3个答案)关闭2年前。我读过:Theteamhavegenerallybeenbusyimplementingothervariationsoninitializers.ForexampleyoucannowinitializeaDictionaryobject但是看着:varDic=newDictionary{{"x",3},{"y",7}};对比varDic=newDictionary{["x"]=3,["y"]=7}

c# - Initialize 方法是一种代码味道吗?

我现在正在编写一堆系统。它们不是从公共(public)接口(interface)派生的。一些示例系统:MusicSystem、PhysicsSystem、InputSystem等等。目前,MusicSystem在其构造函数中加载了大量音频文件,因此,在首次创建对象时可能会有一些短暂的滞后。因此,加载所有音频文件的代码是否应该放在Initialize()方法中?这允许程序员确定何时加载音频文件,但如果他忘记调用Initialize(),程序就会崩溃。因为并非所有系统都需要Initialize()方法,程序员必须查看每个系统以查看该类是否有Initialize()方法,如果有,调用它。这有

C# 找不到类型或命名空间名称 `List'。但我正在导入 System.Collections.Generic;

我有一个错误Thetypeornamespacename`List'couldnotbefound.Areyoumissingausingdirectiveoranassemblyreference?示例代码:usingUnityEngine;usingSystem.Collections;usingSystem.Collections.Generic;publicclasscity1:MonoBehaviour{publicstaticListitems=newList();publicstaticListitemsprice=newList();publicstaticListqu

c# - 组合列表初始化器和对象初始化器

是否可以同时组合列表初始化器和对象初始化器?给定以下类定义:classMyList:List{publicstringText{get;set;}}//wecandothisvarobj1=newMyList(){Text="Hello"};//wecanalsodothatvarobj2=newMyList(){1,2,3};//butthisonedoesn'tcompile//varobj3=newMyList(){Text="Hello",1,2,3};这是设计使然,还是只是C#编译器的错误或缺失的功能? 最佳答案 不,查看

c# - LINQ to Entities Group By 表达式给出 'Anonymous type projection initializer should be simple name or member access expression'

我在这个表达式中遇到了上述错误:varaggregate=fromtinentities.TraceLinesjoinminentities.MethodNames.Where("it.NameLIKE@searchTerm",newObjectParameter("searchTerm",searchTerm))ont.MethodHashequalsm.MethodHashwhere(t.CallTypeId&(int)types)==t.CallTypeId&&t.UserSessionProcessId==m_SessionIdgrouptbym.Nameintodselect

C#语言 : Garbage Collection, SuppressFinalize

我正在阅读“TheC#Language”,第4版,它讨论垃圾收集如下:"BILLWAGNER:ThefollowingruleisanimportantdifferencebetweenC#andothermanagedenvironments.Priortoanapplication’stermination,destructor'sforallofitsobjectsthathavenotyetbeengarbagecollectedarecalled,unlesssuchcleanuphasbeensuppressed(byacalltothelibrarymethodGC.Su

c# - 内部 System.Linq.Set<T> 与公共(public) System.Collections.Generic.HashSet<T>

检查Linq.Enumerable类中的这段代码:staticIEnumerableDistinctIterator(IEnumerablesource,IEqualityComparercomparer){Setset=newSet(comparer);foreach(TSourceelementinsource)if(set.Add(element))yieldreturnelement;}为什么Microsoft的人决定使用Set的这个内部实现而不是常规的HashSet?如果它在任何方面都更好,为什么不向公众公开呢? 最佳答案