How to replace list item in best way

前端 未结 11 2237
天涯浪人
天涯浪人 2020-11-30 21:52
if (listofelements.Contains(valueFieldValue.ToString()))
{
    listofelements[listofelements.IndexOf(valueFieldValue.ToString())] = value.ToString();
}
11条回答
  •  囚心锁ツ
    2020-11-30 22:52

    Or, building on Rusian L.'s suggestion, if the item you're searching for can be in the list more than once::

    [Extension()]
    public void ReplaceAll(List input, T search, T replace)
    {
        int i = 0;
        do {
            i = input.FindIndex(i, s => EqualityComparer.Default.Equals(s, search));
    
            if (i > -1) {
                FileSystem.input(i) = replace;
                continue;
            }
    
            break;  
        } while (true);
    }
    

提交回复
热议问题