Books MVC ASP.NET C# SQL Server WCF Written Test HR Round Subscribe C# Videos C# Programs Buy DVD

What are the advantages and disadvantages of using arrays


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.


Another common C# interview question, that is asked is, what are the advantages and disadvantages of using collection classes present in System.Collections namespace. Click here to learn about the advantages and disadvantages of using Systen.Collections namespace classes.

10 comments:

  1. what do you mean by zero index based?

    _pankaj

    ReplyDelete
  2. @pankaj, i guess the author of the post meant, arrays have indexes starting from zero. however, if he/she meant that Arraylist don't, then that's incorrect. Arraylist and arrays both have indexes starting from zero.

    -RB

    ReplyDelete
  3. I guess the author means that: we can only access an element on array by numeric index, while with ArrayList we also have other ways besides numeric index.

    --Hirikarate

    ReplyDelete
  4. How can we provide the static class behavior to a normal class in c#?

    ReplyDelete
  5. How can we provide the abstract class behavior to a normal class in c#?

    ReplyDelete
  6. How can we enable one webbutton after clicking another webbutton

    ReplyDelete
  7. what are the advantages & dis advantages of array in c......? plz tell atleast 6 points each

    ReplyDelete
  8. in this ArrayList program it is not display string value.
    how can we resolve this

    ReplyDelete
  9. I have around 10 years of experience in IT industry.
    And found that in industry we arrays sparingly, it is used for some string manipulation like joining, splitting the string etc.
    So I find this question is irrelevant to the job.
    When it comes to comparing Array to ArrayList, .NET has introduced Generic list in 2.0 version in 2006. Hence I don't think anybody interested to use the ArrayList. When comparing the ArrayList with Generic List related to type safety. Both are equally safe. However, Generic list can grow in size while Array is not .
    Also related to the index in an array, nobody bothers to check it like Array[9].
    If I were you, I would learn more on Generic list and will not bother too much about Array.

    ReplyDelete
  10. One more item I forgot to update in my previous post.
    If we use generic collection,
    We can add as many number items as possible without any restrictions.
    It provides a options to traverse better than array.
    Easy to convert into other types like IEnumerable.
    Please go through the following articles
    1. http://stackoverflow.com/questions/434761/array-versus-listt-when-to-use-which
    2. http://stackoverflow.com/questions/2430884/c-sharp-array-vs-generic-list

    ReplyDelete

If you are aware of any other C# questions asked in an interview, please post them below. If you find anything missing or wrong, please feel free to correct by submitting the form below.

 
Disclaimer - Terms of use - Contact Us