Geeks With Blogs
Stephanie Grima C#, ASP.NET and my life.

 

  • Array is: a datatype, thatcan be used by calling indexes. during runtime, one cannot really change the size of the array, unless you use the method of copying the array and getting rid of the old one.
    In .NET, the Visual Studio makes use of a special class to store the data. Because of this, the performance is actually quite fast. This is also because in an array, you need to specify the size and thus, the data is stored one after the other.
  • Examples:
    • int[ ] myNumbers= new int[5];
    • myNumbers[0] = 16;
  • ArrayList is: a datatype collection. In order to fill an ArrayList, one can use the .Add property. ArrayLists are very dynamic in the sense that when you add and/or remove items from it, the performace stays the same.
    The internal structure of an ArrayList is an array.
  • Examples:
    • ArrayList myArray = new ArrayList();

    • myArray .Add(“Steph”);

    • string str = myArray [0];

Most of the time, we tend to choose array lists rather than arrays since we have no idea how big it is going to turn out. Arrays are ideal when you know how many items you are going to put in it. Whenever possible, it is recommended to use arrays as this drastically improves the performance.

Posted on Thursday, October 30, 2008 6:14 PM C# | Back to top


Comments on this post: [C#] Array vs ArrayList

# re: [C#] Array vs ArrayList
Requesting Gravatar...
[C#] Array vs ArrayList
Left by hakimi on Jun 08, 2009 1:36 AM

# re: [C#] Array vs ArrayList
Requesting Gravatar...
Arrays CAN be resized at runtime

Array.Resize(ref MyArray, newsize);
Left by GuessWho on Jul 07, 2009 12:07 PM

# re: [C#] Array vs ArrayList
Requesting Gravatar...
@GuessWho: Technically, the array is being copied to a new sized one with the Array.Resize function. This also hurts performance.
Left by CSharper on Mar 14, 2011 10:55 AM

# re: [C#] Array vs ArrayList
Requesting Gravatar...
If you see the structure for ArrayList carefully, its nothing but an array of objects. It has a default value during creation (4,16,32 as per framework) and if you try to add more elements, it does a shallow copy into a higher Capacity Array while discarding the older one. It also implements the interfaces required for recursion and indexed access. This is one of the faster for index and recursion however as right told by others the best use it by having generic list so as to avoid boxing/unboxing. In case you want faster access, you use simple list.. so it all depends on the purpose.

Regards,
Varun Sharma
Left by Varun Sharma on Mar 14, 2011 11:49 PM

# re: [C#] Array vs ArrayList
Requesting Gravatar...
Thank you very much! Just what i was looking for.
Left by Christopher on Nov 08, 2011 8:18 PM

# re: [C#] Array vs ArrayList
Requesting Gravatar...
Here is nice Link
http://www.tanvtech.com/Userrequestpage.aspx?Id=2
Left by Nick on Jun 06, 2012 3:17 PM

Your comment:
 (will show your gravatar)


Copyright © Steffi | Powered by: GeeksWithBlogs.net