C# 合併多個 list

我想合併2個List<string>,而且要求去除重複的項

C# code
?
1
2
3
4
5
6
7
8
9
            List<string> str1 = new List<string>();
            List<string> str2 = new List<string>();
            str1.Add("FUCK");
            str1.Add("lb");
            str1.Add("sorr");
            str2.Add("FUCK");
            str2.Add("12");
 
            str1.AddRange(str2);


AddRange用來合併List<string>是沒什麼問題,問題是不能除去重複的項,後來我這樣

C# code
?
1
2
3
4
5
6
7
8
9
10
11
            List<string> str1 = new List<string>();
            List<string> str2 = new List<string>();
            str1.Add("FUCK");
            str1.Add("lb");
            str1.Add("sorr");
            str2.Add("FUCK");
            str2.Add("12");
 
            for (int i = 0; i < str2.Count; i++)
                if (!str1.Contains(str2[i]))
                    str1.Add(str2[i]);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章