Alex Rivera | Logout

Fixed Size to List

Asked 2009-10-26T14:54:07.923
25

For declaration perspective the following is allowed

    IList<string> list= new string[3];
    list.Add("Apple");
    list.Add("Manago");
    list.Add("Grapes");

1) It compiles fine,But runtime i am getting "Collection was of fixed size" error. Ofcourse ,collection is dynamically grown by size,why did such declaration is accepted by complier ?

2) What are the different lists that i can assign to IList ? Example

IList<string> fruits=new List<string>();

Here I am assigning List to IList ,What are the various collection classes can i assign to IList?

Edit
Report

1 Answer

6

When you call list.Add, you are trying to insert an item to the end of you array. Arrays are a fixed size collection so you can't do an Add. Instead you will have to assign the entries via the indexer:

list[0] = "a";
list[1] = "b";
list[2] = "c";
answered 2009-10-26T14:55:37.330

Your Answer