generic list how to add item at specific point C#

ok I’ve figured that I’m able to remove item at specific point but how do I add it back to specific point?

and equip.count doesn’t seem to work some said that like that I’m able to tell what’s the max number of items in specific generic list

code is C#

Item is class

public List<Item> equip ;
	void Start () {
	equip.Capacity = 2;
		for (int g = 0; g < 8; g++) {
			equip.Add(new Item(g));
		}
		// equip.RemoveAt(5);
		equip[5] = (new Item(5));
	}

ok I’ve worked around a bit made that equip at 5 is = to what I want instead to delete it and putting new one in it

but if someone knows how to put 1 inside of in the middle of list I’d be happy to hear it

like deleting at 5 and giving 5 at 5 so 6 isn’t named as 5

For the List generic, the key operations I use most often:

List<T> someList = new List();
someList.Add(x)        // Adds x to the end of the list
someList.Insert(0, x)  // Adds x at the given index
someList.Remove(x)     // Removes the first x observed
someList.RemoveAt(0)   // Removes the item at the given index
someList.Count()       // Always good to know how many elements you have!

You should use the Insert method of the List class to be able to add an item at a particular position.
Basic syntax is:
listName.Insert(position, item);

A few caveats:
When you insert the item at a particular position, the items following it have to be adjusted which results in a lot of copy operations. You should use LinkedList instead for the performance benefit.
Do not use capacity if you don’t know the number of elements your list will have beforehand.

Have a look at:
C# List Insert Example - Dot Net Perls and
C# List Examples - Dot Net Perls

I think you are looking for the insert method. You’ll have to save the index of where you want to insert the new Item. Something like this:

		int magicNumbersAreBad = 8;
		int desiredPosition;
		for(int index = 0; index < magicNumbersAreBad; index ++) {
			equip.Add(new Item(null, null, "sadda", 1, 10, 1, g));
            if(index == 5)//the specific spot you want to insert your new Item
               desiredPosition = index;
		}
		equip.RemoveAt(5);
		equip.Insert(desiredPosition, (ew Item(null,null,"asdf",1,10,1,desiredPosition?)));

To answer your question, replace the reference in your list with null. This will “remove” the item from the list, but won’t change the list itself or the order of the remaining entries. When processing the list, just test the enries for null before trying to access them. To “reinsert” an entry, you can use the assignment you made above, equip[5] = new Item(5);

Otherwise, see @rajabala’s answer.

If the items themselves are not modified, you could also use a fixed array as a “pool” of objects, which you would only have to create once at initialization.

Then use a parallel bool[] to flag whether a particular item is available or not.