Get Index of an Item in a List in C#

Suppose I have a generic list of some Strings :

List<String> lstCollection = new List<string>();

lstCollection.Add("Name");

lstCollection.Add("FirstName");

lstCollection.Add("LastName");

lstCollection.Add("City");

lstCollection.Add("State");

In order to get the index from the above collection, one can use the following :

1. //integer variable to hold the index value

int index = 0;

//loop through the collection

foreach( string str in lstCollection)

{

    MessageBox.Show(index.ToString());

    index++; 

}

 

2. //loop through the collection

foreach( string str in lstCollection)

{

    int index = lstCollection.IndexOf(str);

    MessageBox.Show(index.ToString());

}  

Above two methods can be used with other collections too.