ienumerable

IEnumerable<T> provides two GetEnumerator methods - what is the difference between them?

落花浮王杯 提交于 2019-12-22 04:29:09
问题 When I emplement IEnumerable<T> interface I see two GetEnumerator methods: one returning IEnumerator and other IEnumerator<T> . When would I use one or another? 回答1: If you are implementing the IEnumerable<T> generic interface, you will pretty much always have to use the generic GetEnumerator method - unless you cast your object explicitly to (non-generic) IEnumerable. The reason is backwards compatability with .NET 1.0/1.1 which didn't support generics. 回答2: You usually implement both. One

Serializing result of a LINQ IEnumerable

你。 提交于 2019-12-22 03:24:45
问题 I have a simple value type: [Serializable] private struct TimerInstance { public TimerInstance(string str, long nTicks) { _name = str; _ticks = nTicks; } private readonly string _name; private readonly long _ticks; public string Name { get { return _name; } } public long Ticks { get { return _ticks; } } public override string ToString() { return string.Format("{0,20}: {1,10:N}", Name, Ticks); } } which as you'll note is serializable. Then I have a list of these: static private List

Why is the C# compiler happy with double IEnumerable<T> and foreach T?

跟風遠走 提交于 2019-12-22 01:34:38
问题 I know this code does not work (and have no problems writing it in a way that will work). I was wondering how the compiler can build with out any errors. And you get run time errors if you where to run it? ( assuming data was not null ) using System; using System.Collections.Generic; public class Class1 { public void Main() { IEnumerable<IEnumerable<Foo>> data = null; foreach(Foo foo in data){ foo.Bar(); } } } public class Foo { public void Bar() { } } 回答1: This is because foreach does not do

Converting an array of type T to an array of type I where T implements I in C#

与世无争的帅哥 提交于 2019-12-21 18:04:08
问题 I am trying to accomplish something in C# that I do easily in Java. But having some trouble. I have an undefined number of arrays of objects of type T. A implements an interface I. I need an array of I at the end that is the sum of all values from all the arrays. Assume no arrays will contain the same values. This Java code works. ArrayList<I> list = new ArrayList<I>(); for (Iterator<T[]> iterator = arrays.iterator(); iterator.hasNext();) { T[] arrayOfA = iterator.next(); //Works like a charm

How do I convert IEnumerable to a custom type in C#?

狂风中的少年 提交于 2019-12-21 16:21:46
问题 I am using extension methods OrderBy and ThenBy to sort my custom collection on multiple fields. This sort does not effect the collection but instead returns and IEnumberable. I am unable to cast the IEnumerable result to my custom collection. Is there anyway to change the order of my collection or convert the IEnumerable result to my custom collection? 回答1: If your collection type implements IList<T> (to be able to Add() to it) you could write an extension method: public static Extensions {

Count an IOrderedEnumerable without consuming it

不羁的心 提交于 2019-12-21 12:37:38
问题 What I want to do, short version: var source = new[]{2,4,6,1,9}.OrderBy(x=>x); int count = source.Count; // <-- get the number of elements without performing the sort Long version: To determine the number of elements in an IEnumerable , it is neccessary to iterate over all elements. This could potentially be a very expensive operation. If the IEnumerable can be cast to ICollection , then the count can be determined quickly without iterating. The LINQ Count() method does this automatically.

Modelbinding IEnumerable in ASP.NET MVC POST?

╄→尐↘猪︶ㄣ 提交于 2019-12-21 09:09:10
问题 Is there any issues with modelbinding IEnumerable types to an MVC POST? Some properties in my Model are not being bound upon a post to an action. Seems that properties on the model like strings are ok, but my IEnumerable is what is not being bound. Here's a snippet of my code: <%: Html.TextBoxFor(m => m.ResponseInfo.SubsetInfo.Test) %> <% for (int i = 0; i < Model.ResponseInfo.SubsetInfo.BandAvailabilities.Count(); i++) {%> <%: Html.TextBoxFor(m => m.ResponseInfo.SubsetInfo.BandAvailabilities

Converting Array to IEnumerable<T>

☆樱花仙子☆ 提交于 2019-12-21 07:54:27
问题 To my surprise, I get the following statement: public static IEnumerable<SomeType> AllEnums => Enum.GetValues(typeof(SomeType)); to complain about not being able to convert from System.Array to System.Collection.Generic.IEnumerable . I thought that the latter was inheriting from the former. Apparently I was mistaken. Since I can't LINQ it or .ToList it, I'm not sure how to deal with it properly. I'd prefer avoiding explicit casting and, since it's a bunch of values for an enum , I don't think

How does IOrderedEnumerable.ThenBy() in .Net work?

久未见 提交于 2019-12-21 07:28:21
问题 I want to understand how ThenBy works in .Net. (I know how to use it, I just don't understand how Microsoft implemented it!) According to the documentation, string_list.OrderBy(Function (x) x.length).ThenBy(Function (x) x) should output a list of strings ordered by length and then alphabetically. How could it possibly work?!? The first sort is by length. The second sort should undo the sorting of the first one! Assume this code: Dim sorted_by_length As IOrderedEnumerable(Of String) sorted_by

Why do so many named collections in .NET not implement IEnumerable<T>?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-21 07:27:14
问题 Random example: ConfigurationElementCollection .Net has tons of these little WhateverCollection classes that don't implement IEnumerable<T> , which means I can't use Linq to objects with them out of the box. Even before Linq, you'd think they would have wanted to make use of generics (which were introduced all the way back in C# 2 I believe) It seems I run across these annoying little collection types all the time. Is there some technical reason? 回答1: The answer is in the question title: