473,396 Members | 2,036 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Converting a IEnumberable sequence into an array - an example here

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<intisAeven = (delegate(int x) { return x % 2 ==
0; });
Console.WriteLine("Even(t)?: {0}, Even(f)?: {1}",
isAeven(numbers[0]), isAeven(numbers[1])); //predicate logic

IEnumerable<intnumbers3 = 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 . .

Oct 26 '08 #1
2 2197
int[] numbers2;
Predicate<intisAeven = (delegate(int x) { return x % 2 ==
0; });
Instead of:
Predicate<intisAeven = (delegate(int x) { return x % 2 == 0; });

You could:
Predicate<intisAeven = i =i % 2 == 0;
I am a little annoyed that I can do this

List<intnumbers = ............;
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

Oct 26 '08 #2
Peter Morris wrote:
> int[] numbers2;
Predicate<intisAeven = (delegate(int x) { return x % 2 ==
0; });

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

You could:
Predicate<intisAeven = i =i % 2 == 0;
I am a little annoyed that I can do this

List<intnumbers = ............;
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.
Oct 26 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Keith MacDonald | last post by:
Hello, Is there a portable (at least for VC.Net and g++) method to convert text between wchar_t and char, using the standard library? I may have missed something obvious, but the section on...
4
by: j | last post by:
In a footnote in the c99 standard the following is labeled as undefined: a = i; And in the second clause of section 6.5 the following is stated: "Between the previous and next sequence...
14
by: Charles L | last post by:
I don't know if this is a stupid quesiton or not. I would like to know how to convert an array of characters generated from a previous operation to a string ie how do I append a null character at...
53
by: Deniz Bahar | last post by:
I know the basic definition of a sequence point (point where all side effects guaranteed to be finished), but I am confused about this statement: "Between the previous and next sequence point an...
37
by: Vadim | last post by:
Hi! I have a STL string, which I need to convert to low case. In VC 6 I used: string sInputBuffer = _strlwr((char*)sBuffer.c_str()); However, now in MSVC.NET 2005 I am getting a warning, that...
8
by: tbh | last post by:
for historical reasons i need to be able to call, from C# under DotNet 2, as COM+ DLL function that returns a "string" which is really an array of seemingly arbitrary bytes (presumably non-zero)....
5
by: metaperl.etc | last post by:
The following program does not work if you uncomment #lis = + list(args) Evidently Python is opting for the nullary constructor list() as opposed to the other one which takes a sequence....
14
by: pat270881 | last post by:
hello, I have to implement a sequence class, however the header file is predefined class sequence { public: // TYPEDEFS and MEMBER CONSTANTS
2
by: Jason James | last post by:
Guys, can anyone confirm the process on converting the OID into an array of bytes for sending to the SNMP device. The code I have seems to only work for values in the OID that are less than...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.