473,651 Members | 2,995 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.WriteLi ne("");
double average_numbers _here = numbers.Average (); //
extension method

Console.WriteLi ne("average numbers: {0}",
average_numbers _here);

int[] numbers2;
Predicate<intis Aeven = (delegate(int x) { return x % 2 ==
0; });
Console.WriteLi ne("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[] WillNotWorkArra y =
numbers.Reverse (); //will not work directly, but you must do **&&**
below

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

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

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

int[] arrayNumbers3 = new int[ACountV];

int arrayNumbers3_i nt = 0;
Console.WriteLi ne("\n ...");
foreach (int jfill in numbers3)
{
arrayNumbers3[arrayNumbers3_i nt] = jfill; //**&&**
arrayNumbers3_i nt++;
}
for (int ie = 0; ie < ACountV; ie++)
{
Console.Write(" !: {0},", arrayNumbers3[ie]);

}

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

Console.WriteLi ne("count is: {0}",
ACountVar_of_nu mbers3IEnumbera ble);

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 2215
int[] numbers2;
Predicate<intis Aeven = (delegate(int x) { return x % 2 ==
0; });
Instead of:
Predicate<intis Aeven = (delegate(int x) { return x % 2 == 0; });

You could:
Predicate<intis Aeven = 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<intis Aeven = (delegate(int x) { return x % 2 ==
0; });

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

You could:
Predicate<intis Aeven = 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
5478
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 codecvt, in Josuttis' "The Standard C++ Library", did not help, and I'm still awaiting delivery of Langer's "Standard C++ IOStreams and Locales". Thanks,
4
8184
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 point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value
14
12916
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 the end? I haven't been able to do it so far. Is there a string function I can use? Can anyone help? Charles L
53
4063
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 object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored." Can someone give me examples of expressions that "barely"...
37
22407
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 _strlwr is depricated. Instead, it's proposed to use _strlwr_s : http://msdn2.microsoft.com/en-us/library/y889wzfw.aspx
8
9299
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). however, C# treats this as a string and the only ways i have found to convert it to a byte array lose data (apparently in those cases when the "characters" are not legal UTF values). in this case (a program i only need to run once) i would be...
5
4583
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. But no newcomer would know this. And the Python docs dont give a good example of dealing with taking a sequence of args and converting it to a list. I must've spent 40 minutes looking through my ora books, googling and irc'ing in vane on this.
14
4794
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
6971
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 2^14 but some enterprise IDs are now larger than that. Do I just contine the conversion on to additional bytes needed to house the OID in full? So and enterprise ID of 25000 would be sent as three bytes of the following:
0
8278
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8701
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8466
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8584
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7299
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6158
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4144
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1912
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1588
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.