Questions tagged [ienumerable]

IEnumerable, and its generic counterpart IEnumerable are .NET interfaces for iterating (or enumerating) through a collection of items.

The IEnumerable interface (in the System.Collections namespace) contains only a single member, the GetEnumerator() method, which returns an IEnumerator.

The generic counterpart to IEnumerable, IEnumerable<T> (added to the System.Collections.Generic namespace in the .NET framework 2.0) also contains only a single member, the GetEnumerator() method; this returns an IEnumerator<T>.

Interaction with the IEnumerable<T> interface is often done through extension methods, for example First(), Last() and Count().

It is possible to obtain a generic IEnumerable<T> from an IEnumerable by calling either of the extension methods Cast<T>(), or OfType<T>(), depending on the case.

3522 questions
2
votes
3 answers

Standard way of returning an internal collection as IEnumerable

Say I have: class foo { private List bar; public IEnumerable GetBar(); } where GetBar() should return that very same List, without exposing internals (i.e. no other class should be able to remove elements from the internal list, reorder…
J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
2
votes
3 answers

Casting List to IEnumerable

I'm still learning some of this c# stuff, and I couldn't find an answer to this question. Assuming that I have a list of MyObject implementing MyInterface public class MyObject : IMyInterface { ...} public List MyObjectList; How can I…
Habitante
  • 233
  • 3
  • 8
2
votes
1 answer

f# member constraints and IEnumerable collections

I'm new to F# and today I saw the member constraint feature of F#. I was thinking of if one could (ab)use it in the following way... Say I want to iterate get the texts of the items in a ListView. I could do it like this: let listView = new…
johv
  • 4,424
  • 3
  • 26
  • 42
2
votes
2 answers

Remove Duplicates From BindingList

Does BindingList have any solution to remove duplicate elements? I've tried: BindingList accounts = new BindingList(); accounts.add(new Account("username", "password")); accounts.add(new Account("username", "password")); …
Torra
  • 1,182
  • 5
  • 15
  • 23
2
votes
2 answers

IEnumerable.ConvertAll & DDD

I have an interesting need for an extension method on the IEumerable interface - the same thing as List.ConvertAll. This has been covered before here and I found one solution here. What I don't like about that solution is he builds a List to hold…
n8wrl
  • 19,439
  • 4
  • 63
  • 103
2
votes
2 answers

I am not able to read the IEnumerable items in foreach loop

I am trying to build a Events Calendar, where i am getting the dates/events from an XML file in C# I am loading my xml document through this: XDocument myEventsDocument = XDocument.Load(Server.MapPath("Events.xml")); Then I am storing the events in…
Ron
  • 1,901
  • 4
  • 19
  • 38
2
votes
6 answers

How can I query the contents of a List via LINQ based on string values?

I am trying to query a collection of records using a ">=" for a string column/member. What I want/am expecting is that such a condition would, given the following values in the "musician" member: Clarence "Gatemouth" Brown Merle Travis Eddie Van…
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
2
votes
2 answers

How to sort IEnumerable with limited result count? (another implementation of .OrderBy.Take)

I have a binary file which contains more than 100 millions of objects and I read the file using BinaryReader and return (Yield) the object (File reader and IEnumerable implementation is here: Performance comparison of IEnumerable and raising event…
Amir Pournasserian
  • 1,600
  • 5
  • 22
  • 46
2
votes
5 answers

How can I protect an accessor only IEnumerable property from mutation?

Suppose I have this: public class MyClass { private readonly List _strings = new List(); public IEnumerable MyStrings { get { return _strings; } } } How can I prevent…
Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
2
votes
1 answer

Performance comparison of IEnumerable and raising event for each item in source?

I want to read big binary file containing millions of records and I want to get some reports for the records. I use BinaryReader to read (which I think has the best performance in readers) and convert read bytes to data model. Due to the count of…
Amir Pournasserian
  • 1,600
  • 5
  • 22
  • 46
2
votes
2 answers

How to convert a IEnumerable to Dictionary

I have dictionary like this Dictionary MyStructure means public struct ... MyObject means a specific entity ... Then through events I pass my dictionary as an IEnumerable col =…
Maximus Decimus
  • 4,901
  • 22
  • 67
  • 95
2
votes
2 answers

Enumerator and function scopes

Why an Enumerator does not keep track of the item in the same function but not if the MoveNext operation happens in other function ? Example: public static void Test() { var array = new List(new Int32[] { 1, 2, 3, 4, 5 }); …
vtortola
  • 34,709
  • 29
  • 161
  • 263
2
votes
3 answers

What is the best way to take generically a container in C++ using interfaces (i.e. equivalent of taking IEnumerable as argument in C#)

I would like a C++ constructor/method to be able to take any container as argument. In C# this would be easy by using IEnumerable, is there an equivalent in C++/STL ? Anthony
BlueTrin
  • 9,610
  • 12
  • 49
  • 78
2
votes
3 answers

How to update a property for a specific element of an IEnumerable List in C#

I have an IEnumerable list, IEnumerable sorts; I am updating its property as: sorts.Where(a => a.DisplayName.ToLower() == "abc") .ToList() .ForEach(sl => sl.DisplayName = "Testing"); But it does not change the property for that…
csharp coder
  • 19
  • 2
  • 4
2
votes
2 answers

How to Get Key-Value Pairs of a Specific Type Only from Dictionary

I have an IDictionary (not generic, so I am dealing with object's here), and I would like to get from it only the elements in that IDictionary which correspond to a specific key type K and a specific value type V. There are a billion ways to do…
Maltor
  • 553
  • 6
  • 16