ienumerable

C#: How can I make an IEnumerable<T> thread safe?

Say I have this simple method: public IEnumerable<uint> GetNumbers() { uint n = 0; while(n < 100) yield return n++; } How would you make this thread safe? And by that I mean that you would get that enumerator once, and have multiple threads handle all the numbers without anyone getting duplicates. I suppose a lock nee...

Using List<Person> Distinct() to return 2 values

I have a Person class, with Name and AreaID properties. public class Person { public string Name; public int AreaID; // snip } I have a List<Person> with the potential for hundreds of Person objects in the list. e.g., 100 Persons with AreaID = 1 and 100 Persons with AreaID = 2 I want to return distinct list of AreaID's and ...

Would C# benefit from distinctions between kinds of enumerators, like C++ iterators?

I have been thinking about the IEnumerator.Reset() method. I read in the MSDN documentation that it only there for COM interop. As a C++ programmer it looks to me like a IEnumerator which supports Reset is what I would call a forward iterator, while an IEnumerator which does not support Reset is really an input iterator. So part one of ...

Optimal LINQ query to get a random sub collection - Shuffle

Please suggest an easiest way to get a random shuffled collection of count 'n' from a collection having 'N' items. where n <= N ...

When do I have to specify type <T> for IEnumerable extension methods?

I'm a bit confused about the use of all the IEnumerable<T> extension methods, intellisense is always asking for <T>, but I don't think it's necessary to specify <T> at all times. Let's say I have the following: List<Person> people = GetSomePeople(); How is this: List<string> names = people.ConvertAll<string>(p=>p.Name).Distinct<stri...

Simple sort verification for unit testing an ORDER BY?

I'm working on a DAL method that uses an ORDER BY clause in a SELECT. The return value from the method is an IEnumerable<T>, where T is a class that encapsulates a domain entity, and the sort order would be based on one of the properties of this class, namely, "Priority." It's important that this ORDER BY works, of course, so I want to...

What is the benefit to using List<T> over IEnumerable<T>?

or the other way around? I use generic lists all the time. But I hear occasionally about IEnumerables, too, and I honestly have no clue (today) what they are for and why I should use them. So, at the risk of having something on the net forever more proclaiming my ignorance, I humbly post this question. ...

IEnumerable question: Best performance?

Quick question: Which one is faster? foreach (Object obj in Collection) { if(obj.Mandatory){ ... } } or foreach (Object obj in Collection.FindAll(o => o.Mandatory)) { ... } and if you know a faster suggestion, i'd be pleased to know. Thank you ...

What is the most efficient way to create a distinct list of items using .NET?

I have a large list of values (100-200 character strings) and I need to return a distinct listing of them. What is the most efficient way to do this using .NET? The 2 ways that I can think of are: Use the Distinct() method of the IEnumerable class Use a Dictionary If the Dictionary approach is faster in raw terms, consider a trade-...

A problem with exception handling for IEnumerable<T>, it's lazyness-dependent.

I used to create interfaces with IEnumerable<T> as return type, whenever I want to specify that a particular output is read-only. I like it as it's minimalistic, hides implementation details and decouples a callee from a caller. But recently a colleague of mine argued that IEnumerable<T> should be kept for scenarios which involve lazy e...

Looking for a faster implementation for IEnumerable/IEnumerator

I'm trying to optimize a concurrent collection that tries to minimize lock contention for reads. First pass was using a linked list, which allowed me to only lock on writes while many simultaneous reads could continue unblocked. This used a custom IEnumerator to yield the next link value. Once i started comparing iteration over the colle...

Whats a quick way to convert an IEnumerable<Foo> to List<Foo> in C# 2.0?

we all know the slow way: foreach.. ...

Is it common (or encouraged) practice to overload a function to accept IEnumerable<T>, ICollection<T>, IList<T>, etc.?

EDIT: From the answers given, it's been made rather clear to me how the design I'm asking about below should actually be implemented. With those suggestions in mind (and in response to a comment politely pointing out that my example code does not even compile), I've edited the following code to reflect what the general consensus seems t...

LINQ to IEnumerable<MyObj>

Hi, I have a class MyObj and a collection IEnumerable. Some of the columns are wholly empty (i.e. == NULL) across all rows and therefore I want to create an IEnumerable<> of the members of MyObj which hold a non-null value. If I could predict the members of MyObj which would be of interest I'd do something like: var part = from e...

Cast/Convert IEnumerable<T> to IEnumerable<U> ?

The following complies but at run time throws an exception. What I am trying to do is to cast a class PersonWithAge to a class of Person. How do I do this and what is the work around? class Person { public int Id { get; set; } public string Name { get; set; } } class PersonWithAge { public int Id { get; set; } public string Name { ...

Can it be advantageous for a method to return IOrderedEnumerable<T> instead of IEnumerable<T>?

Can it be advantageous for a method to return IOrderedEnumerable instead of IEnumerable? ...

Does a foreach loop copy each item of an IEnumerable while iterating?

I have a foreach loop like below foreach (XYZ split in this.splits ) { // this code is inserted for debug purpose only bool check = object.ReferenceEquals(splits.First(), split); ..... } When I have single element in this.splits, check is returning false. I have checked by so...

System.Linq and IEnumerable Group Help

I'm just getting my feet wet with Linq and IEnumerable, and I'm needing help in trying to determine if my objects contain matches for a card game. I think if I get the first one figured out, the other match checking I need to do will fall in place. public class Card { pubic int Value { get; set; } public Card(int value) { ...

What is the reason for IEnumerable/IEnumerable<T> interfaces to only have MoveNext?

Basically I am wondering why MS decided to implement an enumerator that only supports going forward: MoveNext(). Is it not more flexible to also enforce MovePrevious for this widely used interface throughout the .NET framework? I can imagine it making the Linq.Reverse far easier to implement for MS and more efficient in performance, bu...

Are there any non-repeatable IEnumerable classes?

Are there any non-repeatable IEnumerable classes? By non-repeatable I mean where you can't safely call GetEnumerator multiple times or where calling GetEnumerator has observable side effects. ...