Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any Method Available That Will Execute Func<Foo> N Times And Get An IEnumerable<Foo>?

Tags:

c#

I may only be asking this because I've blinded myself to something brilliantly obvious but,

I'm looking for something like this...

IEnumerable<Foo> fooSeq = Enumerable.Generate(()=> new Foo(), 5);

which would make fooSeq a sequence of 5 new Foo instances.

Can't seem to find it though.

like image 762
Grokodile Avatar asked May 20 '11 23:05

Grokodile


People also ask

How do I know if IEnumerable has an item?

IEnumerable. Any() will return true if there are any elements in the sequence and false if there are no elements in the sequence. This method will not iterate the entire sequence (only maximum one element) since it will return true if it makes it past the first element and false if it does not.

How do I add items to an IEnumerable list?

What you can do is use the Add extension method to create a new IEnumerable<T> with the added value. var items = new string[]{"foo"}; var temp = items; items = items. Add("bar");

Does IEnumerable have count?

IEnumerable doesn't have a Count method.


1 Answers

Not sure what exactly you are trying to do but if you are trying to generate 5 Foo instances, this will work

IEnumerable<Foo> fooSeq = Enumerable.Range(1, 5).Select(x => new Foo())
like image 164
Bala R Avatar answered Oct 05 '22 23:10

Bala R