gpt4 book ai didi

c# - 列表排序编译错误

转载 作者:行者123 更新时间:2023-11-30 13:47:31 25 4
gpt4 key购买 nike

我正在尝试获取一个唯一的、按字母顺序排列的行业名称(字符串)列表。这是我的代码:

HashSet<string> industryHash = new HashSet<string>();
List<string> industryList = new List<string>();
List<string> orderedIndustries = new List<string>();

// add a few items to industryHash

industryList = industryHash.ToList<string>();
orderedIndustries = industryList.Sort(); //throws compilation error

最后一行抛出一个编译错误:“无法将类型‘void’隐式转换为‘System.Collections.Generic.List’

我做错了什么?

最佳答案

List.Sort对原始列表进行排序并且不返回新列表。因此,要么使用此方法,要么使用 Enumerable.OrderBy + ToList:

高效:

industryList.Sort();

效率较低:

industryList = industryList.OrderBy(s => s).ToList();

关于c# - 列表排序编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17047381/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com