473,396 Members | 2,011 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.

Expandable array

In c# is it possible to resize an array cleanly?

My application is that I have a list of numbers i'd like to return from a
method as an integer array. I don't know before hand how many integers i'm
going to have.

Simon.
Nov 15 '05 #1
5 4722
Hi

In this scenario I usually use an ArrayList to easily build the array. Once
finished, I use the ToArray method to get a real Array:

System.Collections.ArrayList al = new System.Collections.ArrayList();
al.Add(3);
al.Add(5);
///...
return al.ToArray(typeof(int));

--
Greetz

Jan Tielens
________________________________
Read my weblog: http://weblogs.asp.net/jan
"Simon Johnson" <ck***@hotmail.com> wrote in message
news:br**********@titan.btinternet.com...
In c# is it possible to resize an array cleanly?

My application is that I have a list of numbers i'd like to return from a
method as an integer array. I don't know before hand how many integers i'm
going to have.

Simon.

Nov 15 '05 #2
Perfect, Thanks..

Simon.
Nov 15 '05 #3
Good solution. You'll begin to take a boxing hit if the list becomes >50
items, since ArrayList stores as object type.

If its gonna be loading a lot of integers then I'd recommend building a
class for managing a int[] for efficiency reasons if this is gonna be a hard
hit area.

/me looks forward to C# generics ;-)

--
Eric Newton
C#/ASP Application Developer
http://ensoft-software.com/
er**@cc.ensoft-software.com [remove the first "CC."]

"Jan Tielens" <ja*@no.spam.please.leadit.be> wrote in message
news:Oh**************@TK2MSFTNGP10.phx.gbl...
Hi

In this scenario I usually use an ArrayList to easily build the array. Once finished, I use the ToArray method to get a real Array:

System.Collections.ArrayList al = new System.Collections.ArrayList();
al.Add(3);
al.Add(5);
///...
return al.ToArray(typeof(int));

--
Greetz

Jan Tielens
________________________________
Read my weblog: http://weblogs.asp.net/jan
"Simon Johnson" <ck***@hotmail.com> wrote in message
news:br**********@titan.btinternet.com...
In c# is it possible to resize an array cleanly?

My application is that I have a list of numbers i'd like to return from a method as an integer array. I don't know before hand how many integers i'm going to have.

Simon.


Nov 15 '05 #4
Eric

I agree, generics will be so cool in these scenarios...

--
Greetz,
Jan
__________________________________
Read my weblog: http://weblogs.asp.net/jan
"Eric Newton" <er**@cc.ensoft-software.com> schreef in bericht
news:uR**************@TK2MSFTNGP11.phx.gbl...
Good solution. You'll begin to take a boxing hit if the list becomes >50
items, since ArrayList stores as object type.

If its gonna be loading a lot of integers then I'd recommend building a
class for managing a int[] for efficiency reasons if this is gonna be a hard hit area.

/me looks forward to C# generics ;-)

--
Eric Newton
C#/ASP Application Developer
http://ensoft-software.com/
er**@cc.ensoft-software.com [remove the first "CC."]

"Jan Tielens" <ja*@no.spam.please.leadit.be> wrote in message
news:Oh**************@TK2MSFTNGP10.phx.gbl...
Hi

In this scenario I usually use an ArrayList to easily build the array. Once
finished, I use the ToArray method to get a real Array:

System.Collections.ArrayList al = new System.Collections.ArrayList();
al.Add(3);
al.Add(5);
///...
return al.ToArray(typeof(int));

--
Greetz

Jan Tielens
________________________________
Read my weblog: http://weblogs.asp.net/jan
"Simon Johnson" <ck***@hotmail.com> wrote in message
news:br**********@titan.btinternet.com...
In c# is it possible to resize an array cleanly?

My application is that I have a list of numbers i'd like to return
from a method as an integer array. I don't know before hand how many integers i'm going to have.

Simon.



Nov 15 '05 #5
Eric Newton <er**@cc.ensoft-software.com> wrote:
Good solution. You'll begin to take a boxing hit if the list becomes >50
items, since ArrayList stores as object type.

If its gonna be loading a lot of integers then I'd recommend building a
class for managing a int[] for efficiency reasons if this is gonna be a hard
hit area.

/me looks forward to C# generics ;-)


While generics will indeed be very handy here, I certainly wouldn't
start writing my own type to deal with this kind of thing unless I'd
already determined that the boxing hit was actually causing a problem.
Premature optimisation is a bad idea, IMO.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6

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

Similar topics

2
by: Ralf Höppner | last post by:
Does anyone know, if it is possible to create an explorer-like expandable tree from an XML File ? Maybe using XSLT ? I'd like to use it on an HTML web page. thanks ! Ralf
2
by: Amir S. | last post by:
Hi guys... Does anyone know a tool (preferably free) that would generates code for the cute expandable navigation system that uses pluses and minuses? An example can be seen on the left nav...
4
by: Doug | last post by:
Hi - I'm looking to code some javascript that has an event handler tied to an image ("a down arrow") that will expand code that was hidden on the page load. It's pretty simple, I have an...
2
by: Alfonso Morra | last post by:
Hi, I want to create a container structure (array/list etc), that is expandable. Currently, I have static arrays like this SomeStruct *ptrArray ; What I want to do is to be able to use...
0
by: Pradeep | last post by:
Hi all, When i change an expandable property of an object from the property grid, the set method of that property is not being called, where as the set methods of its sub properties are being...
0
by: Brian Young | last post by:
Hi all. I'm using the Property Grid control in a control to manage a windows service we have developed here. The windows service runs a set of other jobs that need to be managed. The control...
5
by: KitKat | last post by:
I've got two queries; one is a modification of an older query; the other I created from scratch. The old one is about 5 copies down the road from something that has been expandable when I'm in...
1
by: axlq | last post by:
I've seen a lot of examples of expandable/collapsable lists, such as these nice examples: http://www.karlnelson.net/nestedlists/ and http://dynamicdrive.com/dynamicindex1/navigate1.htm They...
1
by: Manil | last post by:
I usually have no problem with Firefox, but here is a rare instance of IE displaying a table with image slices correctly and Firefox adding space where I seemingly have none. Is there anything I can...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...
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.