Our Client's database returns a set of prices in an array, but they sometimes don't include all prices, i.e., they have missing elements in their array. We return what we find as an IList, which works great when we retrieve content from the database. However, we are having difficulties setting the elements in the proper position in the array.
Is it possible to create an IList then add an element at a particular position in the IList?
var myList = new List<Model>();
var myModel = new Model();
myList[3] = myModel; // Something like what we would want to do
Lists grow dynamically to accommodate items as they are added. You would have to initialize the list with a predefined size. The easiest way I can think of to do that would be:
var myList = new Model[100].ToList();
That'll give you a list with 100 items, all null. You're then free to assign a value to myList[3].
Note that in your code you are trying to instantiate an IList<Model>
which isn't possible - you need a concrete type (like List<Model>
) rather than an interface.