C # way to add value to <T> list by index
Is it possible to add a value at a specific index? I am trying to make an indexer and I have lists. Is there any trick to do this in this context: D
public class Multime<T> { private List<Multime<T>> multiSets; private List<T> multimea; ***public Multime<T> this[int index] { get { return this.Multisets.ElementAt(index); } set { this.Multisets.CopyTo(value,index); } }*** public List<Multime<T>> Multisets { get { return this.multiSets; } set { this.multiSets = value; } }//accesori Multimea de multimi public List<T> Multimea { get { return this.multimea; } set { this.multimea = value; } }//Accesori Multime List<T>.Insert , perhaps?
But I would suggest that you probably just want to get / write - don't embed:
public Multime<T> this[int index] { get { return Multisets[index]; } set { Multisets[index] = value; } } Note that with C # 3, there are simpler ways to write these properties: btw:
public List<T> Multimea { get; set; } public List<Multime<T>> Multisets { get; set; } In addition, it is not always a good idea to actually publish written collections directly - this means that you cannot control what is happening in these collections.
Try using List.Insert
This should solve the problem you are facing.
Perhaps you are looking for something more like a dictionary or map. These are collection types that allow you to assign an object using key-value pairs, so you can always assign an object at a specific position and retrieve it from the same position. You might think that you need a list because you do not know how many records you will keep, but if you have an idea of ββthe maximum number of records, this will help you decide if you can use something like a dictionary. I think the limit is pretty high, but you can look at the Dictionary class to see the limitations.