Jack
Jack

Reputation: 16724

how to update the index of item in List?

I have the following list:

var list = new List<string>(); 

//

list.Add("foo");
list.Add("baa");

do something..

if(foo) {
  //how to put baa in as first element in list?
}

I'm looking for alternative for this:

string item = "baa";
docTypes.Remove(item);
docTypes.Insert(0, item);

Upvotes: 2

Views: 4152

Answers (1)

Ry-
Ry-

Reputation: 224867

There is no alternative, that's how you do it. If you want a bit of an efficiency improvement and you know the index, then you could use RemoveAt:

int index = 1;
docTypes.Insert(0, docTypes[index]);
docTypes.RemoveAt(index + 1);

Upvotes: 4

Related Questions