在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。