在C#中,可以通过以下几种方法将List转为其他集合:
List<int> list = new List<int> { 1, 2, 3, 4, 5 };
List<int> newList = list.ToList();
List<int> list = new List<int> { 1, 2, 3, 4, 5 };
int[] array = list.ToArray();
List<KeyValuePair<int, string>> list = new List<KeyValuePair<int, string>> {
new KeyValuePair<int, string>(1, "One"),
new KeyValuePair<int, string>(2, "Two"),
new KeyValuePair<int, string>(3, "Three")
};
Dictionary<int, string> dictionary = list.ToDictionary(x => x.Key, x => x.Value);
List<int> list = new List<int> { 1, 2, 3, 4, 5 };
HashSet<int> hashSet = list.ToHashSet();
这些方法可以根据具体需求选择合适的集合类型来转换List。