PC Review


Reply
Thread Tools Rate Thread

Converting a IEnumberable sequence into an array - an example here

 
 
raylopez99
Guest
Posts: n/a
 
      26th Oct 2008
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 . .

 
Reply With Quote
 
 
 
 
Peter Morris
Guest
Posts: n/a
 
      26th Oct 2008
> 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.



--
Pete
====
http://mrpmorris.blogspot.com
http://www.capableobjects.com

 
Reply With Quote
 
Jeroen Mostert
Guest
Posts: n/a
 
      26th Oct 2008
Peter Morris wrote:
>> 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.

--
J.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting byte array to short array? Ole Microsoft C# .NET 2 8th Jul 2006 01:28 AM
Problem converting managed array (C++) to C# array Dick Swager Microsoft C# .NET 2 30th Aug 2005 02:53 AM
Converting a double array to byte array =?Utf-8?B?UmFncw==?= Microsoft Dot NET Framework 5 10th Aug 2005 01:22 PM
converting an array of bytes to an array of chars Claire Microsoft C# .NET 1 8th Jun 2004 04:58 PM
Converting Decimal to Time Sequence =?Utf-8?B?Ym1pbHM=?= Microsoft Access Queries 2 21st Oct 2003 07:45 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:49 AM.