How to replace list item in best way

前端 未结 11 2231
天涯浪人
天涯浪人 2020-11-30 21:52
if (listofelements.Contains(valueFieldValue.ToString()))
{
    listofelements[listofelements.IndexOf(valueFieldValue.ToString())] = value.ToString();
}
11条回答
  •  猫巷女王i
    2020-11-30 22:55

    Use Lambda to find the index in the List and use this index to replace the list item.

    List listOfStrings = new List {"abc", "123", "ghi"};
    listOfStrings[listOfStrings.FindIndex(ind=>ind.Equals("123"))] =  "def";
    

提交回复
热议问题