我不想有任何不必要的代码,但我也想“安全”。经验观察表明,下面的 OrderBy 什么都不做 - List 已经正确排序。我可以依靠这种情况,并删除该 OrderBy 行吗?
HashSet<int> hashSet = new HashSet<int>();
List<int> listInts = new List<int>();
using (var file = new System.IO.StreamReader(selectedFile)) {
string line;
int lineNum = 0;
int Offset = (int)numericUpDownLinesOfContext.Value;
while ((line = file.ReadLine()) != null) {
lineNum++;
if (line.Contains(PlatypusToSearchFor)) {
// This adds the lines before and after that will provide the desired context
// (N lines from the log file before and after the searched for value)
hashSet.UnionWith(Enumerable.Range(lineNum - Offset, Offset * 2 + 1));
}
}
// Remove any negative numbers, as well as 0, that might have been added
// (0, -1, -2, or -3 are all possibilities, but the first line is #1)
listInts = hashSet.Where(i => i >= 1).ToList();
// They seem to be ordered correctly already, but this is just in case:
listInts = listInts.OrderBy(i => i).ToList();
}