39

Is there any way you can add a value at a specific index? I try to do indexator and I have Lists. Is there any trick for making this 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
TyGerX
  • 513
  • 1
  • 6
  • 11
  • 1
    Did you try looking at the methods already provided by List? There's a distinct possibility one does exactly what you need. – Servy Apr 23 '12 at 17:07
  • INSERT is not good :D, i dont want the rest of the list to be modified and i want to add/replace at index – TyGerX Apr 23 '12 at 17:10

5 Answers5

84

The .Insert() method on List<T> is exactly for this purpose:

someList.Insert(2, someValue);

This would modify the someList collection to insert someValue at index 2, pushing other values up one index.

More information here.

David
  • 208,112
  • 36
  • 198
  • 279
35

List<T>.Insert, perhaps?

But I'd suggest you probably just want to fetch/write - not insert:

public Multime<T> this[int index]
{
    get { return Multisets[index]; }
    set { Multisets[index] = value; }
}

Note that as of C# 3, there are simpler ways of writing those properties, btw:

public List<T> Multimea { get; set; }
public List<Multime<T>> Multisets { get; set; }

It's also not generally a good idea to actually expose composed collections directly - it means you have no control over what happens in those collections.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • i think it moves element[index] to index+1 i dont want to affect the rest of the list,i need to replace just the object at index – TyGerX Apr 23 '12 at 17:08
  • @TyGerX: So you should have asked about *replacing*, not *adding*. See my edit. – Jon Skeet Apr 23 '12 at 17:08
  • set { Multisets[index] = value; } does not work i dont know why some compile error becouse Multisetst is List i think – TyGerX Apr 23 '12 at 17:11
  • @TyGerX: "some compile error" leaves a lot to be desired as descriptions go. Please read http://tinyurl.com/so-hints – Jon Skeet Apr 23 '12 at 17:12
  • An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll Additional information: Index was out of range. Must be non-negative and less than the size of the collection. – TyGerX Apr 23 '12 at 17:14
  • 1
    @TyGerX That's not a compile time error...that's a runtime error. And it's because you used an index that's outside of the valid range, i.e. accessing an item that doesn't exist. The code listed here is fine. – Servy Apr 23 '12 at 17:16
3

Can you use List.Insert?

Matthew
  • 24,703
  • 9
  • 76
  • 110
2

Try using List.Insert

This should solve the problem you are having.

Jack
  • 10,943
  • 13
  • 50
  • 65
-2

You may be looking for something more like a Dictionary or Map. These are types of collections that allow you to assign an object using key value pairs so you can always assign an object at a certain position and retrieve it from that same position. You may think that you need the list because you don't know how many records you will store, but if you have some sense of the maximum number of records it will help you decide if you can use something like a Dictionary. The limit is pretty high I think, but you can look at the Dictionary class to see the limits.

user3930061
  • 47
  • 1
  • 3