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...
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 ...
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 ...
Please suggest an easiest way to get a random shuffled collection of count 'n' from a collection having 'N' items. where n <= N
...
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...
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...
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.
...
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
...
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-...
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...
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...
we all know the slow way:
foreach..
...
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...
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...
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 instead of IEnumerable?
...
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...
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)
{
...
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?
By non-repeatable I mean where you can't safely call GetEnumerator multiple times or where calling GetEnumerator has observable side effects.
...