草庐IT

ienumerable

全部标签

c# - 使用 Newtonsoft Json.Net 反序列化为 IEnumerable 类

我有一个项目目前正在使用Json.Net来处理像这样的Json反序列化类:publicclassFoo{publicGuidFooGuid{get;set;}publicstringName{get;set;}publicListBars{get;set;}}publicclassBar{publicGuidBarGuid{get;set;}publicstringDescription{get;set;}}到目前为止一切正常。为了让迭代更简单,我做了Foo类(class)工具IEnumerable像这样:publicclassFoo:IEnumerable{publicGuidFoo

c# - IEnumerable Linq 方法是线程安全的吗?

我想知道Linq扩展方法是否是原子的?或者我是否需要在任何类型的迭代之前锁定跨线程使用的任何IEnumerable对象?将变量声明为volatile对此有任何影响吗?总而言之,以下哪项是最好的线程安全操作?1-没有任何锁:IEnumerable_objs=//...varfoo=_objs.FirstOrDefault(t=>//somecondition2-包括锁定语句:IEnumerable_objs=//...lock(_objs){varfoo=_objs.FirstOrDefault(t=>//somecondition}3-将变量声明为volatile:volatileIE

c# - EditorFor IEnumerable<T> 与 TemplateName

假设我有一个简单的模型来解释用途:publicclassCategory{...publicIEnumerableProducts{get;set;}}查看:@modelCategory...@Html.EditorFor(m=>m.Products)编辑器模板:@modelProduct...@Html.EditorFor(m=>m.Name)请注意,我不必为IEnumerable定义EditorTemplate,我只能为Product创建它模型和MVC框架足够聪明,可以使用自己的IEnumerable模板。它遍历我的集合并调用我的EditorTemplate。输出的html会是这样

c# - 为什么 Func<T> 与 Func<IEnumerable<T>> 不明确?

这个让我感到困惑,所以我想我会在这里问,希望C#专家能向我解释。为什么这段代码会产生错误?classProgram{staticvoidMain(string[]args){Foo(X);//theerrorisonthisline}staticStringX(){return"Test";}staticvoidFoo(Func>x){}staticvoidFoo(Funcx){}}有问题的错误:Error1Thecallisambiguousbetweenthefollowingmethodsorproperties:'ConsoleApplication1.Program.Foo(

c# - 断言 IEnumerables

由于我们公司不使用单元测试,所以我正在自学对自己的代码进行单元测试。我正在使用标准的.net测试框架进行一些非常基本的单元测试。我的一个方法返回一个IEnumerable我想测试它的输出。所以我创建了一个IEnumerable预计将对其进行测试。我以为我记得有一种方法可以Assert.ArePartsEqual或类似的东西,但我似乎找不到它。简而言之,我如何测试两个IEnumerable包含相同的字符串? 最佳答案 我不知道您指的是哪个“标准.net测试框架”,但如果它是VisualStudioTeamSystem单元测试内容,您可

c# - 既然有IEnumerator<T>,为什么还需要IEnumerable<T>?

免责声明:我了解IEnumerable之间的区别和IEnumerator以及如何使用两者。这不是this的副本或this.这更像是一个设计问题-因为IEnumerator已经封装了关于可以枚举的事物的所有必要信息(.Current,.MoveNext()),那么引入一个类型(IEnumerable)的唯一目的是返回前者的实例的意义何在?具体来说:为什么不能foreach被设计为直接遍历IEnumerator,像这样://foreach(vareinanEnumerator){//...}while(anEnumerator.MoveNext()){doSomething(anEnume

C# 关于 IEnumerable<T>.Aggregate

我做了一些关于IList.Aggregate()的测试,但答案对我来说没有意义。ListData1=newList{1,0,0,0,0};varresult=Data1.Aggregate((total,next)=>total+total);结果是16.我预计它会是32.谁能解释一下? 最佳答案 Aggregate不会对列表中的第一个元素运行回调。相反,第一个元素用作累加器的初始值(total)。因此,您的回调仅运行四次,而不是五次,并且24=16。 关于C#关于IEnumerable

c# - Parallel.Foreach 中的 block 分区 IEnumerable

有谁知道让Parallel.Foreach循环使用block分区的方法,我相信默认情况下是范围分区。使用数组时这看起来很简单,因为您只需创建一个自定义分区程序并将负载平衡设置为true。由于直到运行时才知道IEnumerable中的元素数量,因此我似乎无法找到一种让block分区起作用的好方法。如有任何帮助,我们将不胜感激。谢谢!我尝试在每个对象上执行的任务需要花费截然不同的时间来执行。最后我通常要等待几个小时等待最后一个线程完成它的工作。我想要实现的是让并行循环请求分block进行,而不是为每个线程预先分配项目。 最佳答案 如果你

c# - 返回一个通用的 IEnumerable<T>

我正在摆弄泛型和IEnumerableabit,但我有点卡住了。这是我正在尝试做的事情:我想要一个返回任何集合类型的方法-实现IEnumerable(?)-(例如:List,Stack,Queue,...)此外,我希望能够返回任何数据类型的任何集合类型。所以我希望这个方法能够返回List,以及Stack,以及List……等等等等。publicIEnumerablereturnSomething(){StackstackOfInts=newStack();ListlistOfStrings=newList();returnstackOfInts;}这是我迄今为止尝试过的。但是这不起作用,

c# - 使用 IEnumerable 与 ICollection 与 IList 的自定义集合

我需要设计自己的定制GenericCollection类(class)。现在我有很多选择可以使用IEnumerable导出它,ICollection,和IList,稍后会提供一些附加功能。如果我选择IEnumerable,我有点困惑我可能需要声明对象以实际保存集合,就像在这种情况下一样_list.publicclassGenericCollection:IEnumerable{privateList_list;//...}但如果我选择ICollection或IList,我不需要申报List对象,因为它是隐式可用的。publicclassGenericCollection:IList{/