Converting a IEnumberable sequence into an array - an example here

R

raylopez99

Here is a short program demonstrating using IEnumberable, Linq,
predicates, extension methods and some tricks and how to convert an
IEnumberable sequence into an array.

For future reference, not intended as a question.

RL

Main():
int[] numbers = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11};

foreach (int i in numbers)
{ Console.Write(" {0} ", i); }

Console.WriteLine("");


double average_numbers_here = numbers.Average(); //
extension method

Console.WriteLine("average numbers: {0}",
average_numbers_here);

int[] numbers2;
Predicate<int> isAeven = (delegate(int x) { return x % 2 ==
0; });
Console.WriteLine("Even(t)?: {0}, Even(f)?: {1}",
isAeven(numbers[0]), isAeven(numbers[1])); //predicate logic

IEnumerable<int> numbers3 = numbers.Reverse(); //key, must
defined IEnumerable as such, not really an array but can be made into
one see **&&** below, thus int[] WillNotWorkArray =
numbers.Reverse(); //will not work directly, but you must do **&&**
below

Console.WriteLine("reverse");
foreach (int i3 in numbers3)
{
Console.Write(" {0} ", i3);
}
numbers3 = numbers3.Reverse(); //works fine
Console.WriteLine("\n Re-reverse");
foreach (int i32 in numbers3)
{
Console.Write(" {0} ", i32);
}

numbers3 = numbers3.Reverse().Where(n => n % 2 == 0); //
reverse and use even ints only

int ACountVar_of_numbers3IEnumberable = numbers3.Count(); //
store # of elements of numbers3 IEnumberable sequence
int ACountV = ACountVar_of_numbers3IEnumberable; //shorten
the variable name

int[] arrayNumbers3 = new int[ACountV];

int arrayNumbers3_int = 0;
Console.WriteLine("\n ...");
foreach (int jfill in numbers3)
{
arrayNumbers3[arrayNumbers3_int] = jfill; //**&&**
arrayNumbers3_int++;
}


for (int ie = 0; ie < ACountV; ie++)
{
Console.Write(" !: {0},", arrayNumbers3[ie]);

}

Console.WriteLine("\n Even only, reversed again");
foreach (int i32 in numbers3)
{
Console.Write(" {0} ", i32);
}

Console.WriteLine("count is: {0}",
ACountVar_of_numbers3IEnumberable);

Output:
0 1 2 3 4 5 6 7 8 9 10 11
average numbers: 5.5
Even(t)?: True, Even(f)?: False
reverse
11 10 9 8 7 6 5 4 3 2 1 0
Re-reverse
0 1 2 3 4 5 6 7 8 9 10 11
...
!: 10, !: 8, !: 6, !: 4, !: 2, !: 0,
Even only, reversed again
10 8 6 4 2 0 count is: 6
Press any key to continue . .
 
P

Peter Morris

int[] numbers2;
Predicate<int> isAeven = (delegate(int x) { return x % 2 ==
0; });

Instead of:
Predicate<int> isAeven = (delegate(int x) { return x % 2 == 0; });

You could:
Predicate<int> isAeven = i => i % 2 == 0;


I am a little annoyed that I can do this

List<int> numbers = ............;
numbers.ForEach(.........);

but I cannot do this

int[] numbers = ...................;
numbers.ForEach(.......);

Can anyone tell me why this is? I don't mean why I can't do it, I mean why
it may have been decided to implement in such a way that I can't.
 
J

Jeroen Mostert

Peter said:
int[] numbers2;
Predicate<int> isAeven = (delegate(int x) { return x % 2 ==
0; });

Instead of:
Predicate<int> isAeven = (delegate(int x) { return x % 2 == 0; });

You could:
Predicate<int> isAeven = i => i % 2 == 0;


I am a little annoyed that I can do this

List<int> numbers = ............;
numbers.ForEach(.........);

but I cannot do this

int[] numbers = ...................;
numbers.ForEach(.......);

Can anyone tell me why this is? I don't mean why I can't do it, I mean
why it may have been decided to implement in such a way that I can't.
Most operations on arrays take the form of static methods of Array,
..ForEach() is no exception. Arrays are special that way.

In any case, Array.ForEach() and List<T>.ForEach() existed before LINQ. If
you ask me they're pretty much useless, since they're no clearer than a
foreach statement. Maybe if your language doesn't support foreach, but
otherwise the only appeal is turning three lines into one -- and you could
actually do that with the foreach statement too.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top