我使用了这段代码:
List<string> lists=new List<string>("apple","orange","banana","apple","mang0","orange");
string names;
names=lists.Distinct()
那是对的吗?
我使用了这段代码:
List<string> lists=new List<string>("apple","orange","banana","apple","mang0","orange");
string names;
names=lists.Distinct()
那是对的吗?
不,变量names
必须是一个集合。该Distinct
方法返回一个枚举器,因此您可能希望枚举结果并将其实现为一个列表:
List<string> names = lists.Distinct().ToList();
您可以对列表进行排序,然后检查两个和两个:
list.Sort();
Int32 index = 0;
while (index < list.Count - 1)
{
if (list[index] == list[index + 1])
list.RemoveAt(index);
else
index++;
}