且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何替换在***的方式列表项

更新时间:2023-12-05 14:56:40

您可以使其更具可读性,更高效:

You could make it more readable and more efficient:

string oldValue = valueFieldValue.ToString();
string newValue = value.ToString();
int index = listofelements.IndexOf(oldValue);
if(index != -1)
    listofelements[index] = newValue;

这对于指数只要求一次。你的方法是使用包含第一个需要循环中的所有项目(在最坏情况下),那么你使用的IndexOf 这就需要重新枚举项目。

This asks only once for the index. Your approach uses Contains first which needs to loop all items(in the worst case), then you're using IndexOf which needs to enumerate the items again .