HI WELCOME TO SIRIS

What are the advantages and disadvantages of using arrays

Leave a Comment


Advantages of using arrays:
1. Arrays are strongly typed, meaning you can only have one type of elements in the array. The strongly typed nature of arrays gives us 2 advantages. One, the performance will be much better because boxing and unboxing will not happen. Second, run time errors can be prevented because of type mis matches. Type mis matches and runtime errors are most commonly seen with collection classes like ArrayList, Queue, Stack etc, that are present in System.Collections namespace. 

In the example below, Numbers is an integer array. When we try to store a string in the integer array, a compiler error is reported stating cannot implicitly convert string to integer. This is why we call arrays are strongly typed.



In the example below
, Numbers is an ArrayList. Collections of type arraylist are loosely typed. This means any type of elements can be added to the collection. ArrayList operate on object type, which makes them 
loosely typed. No compiler error is reported, but when we run the application, a runtime error is reported as shown. In software development, it is always better to catch errors at compile time rather than at runtime.




Disadvantages of using arrays:
1. Arrays are fixed in size and cannot grow over time, where ArrayList in System.Collections namespace can grow dynamically.
2. Arrays are zero index based, and hence a little difficult to work with. The only way to store or retrieve elements from arrays, is to use integral index. Arrays donot provide convinient methods like Add(), Remove() etc provided by collection classes found in System.Collections or System.Collections.Generics namespaces, which are very easy to work with.

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.