C# Freely convert between IList and IEnumerable

來源:互聯網
上載者:User

項目中有一處用到將List<T>串連起來。可是在調用Concat方法後,串連後結果卻轉換為 IEnumerable<T>,如何將其轉換回來?

正在躊躇,忽然間一眼發現了IEnumerable介面竟然已經存在了轉換方法:public static List<TSource> ToList<TSource>(this IEnumerable<TSource> source);大喜!

附,一樣本:

 

代碼

IList<Student> studentList1 = new List<Student>();
        Student s1   = new Student();
        s1.ID = "1";
        s1.Name = "張三";

        studentList1.Add(s1);

        IList<Student> studentList2 = new List<Student>();
        Student s2 = new Student();
        s2.ID = "2";
        s2.Name = "李四";

        studentList2.Add(s2);

        List<Student> xxx = studentList1.ToList();
        xxx.AddRange(studentList2.ToList());

        int c1 = xxx.Count;

        IList<Student> yyy = studentList1.Concat(studentList2).ToList<Student>();

        int c2 = yyy.Count;

 

 

網上樣本地址

備忘:在同事的提醒下,又發現一轉換方法AddRange,並將其修改到範例程式碼中。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.