C#中List〈string〉和string[]數組之間的相互轉換


 

1,從System.String[]轉到List<System.String>

System.String[] str={"str","string","abc"};

List<System.String> listS=new List<System.String>(str);

 

2, 從List<System.String>轉到System.String[]

List<System.String> listS=new List<System.String>();

listS.Add("str");

listS.Add("hello");

System.String[] str=listS.ToArray();

 

 測試:

 protected void Page_Load(object sender, EventArgs e)
        {
            System.String[] sA = { "str", "string1", "sting2", "abc" };
            List<System.String> sL = new List<System.String>();
            for (System.Int32 i = 0; i < sA.Length; i++)
            {
                Console.WriteLine("sA[{0}]={1}", i, sA[i]);
            }
            sL = new List<System.String>(sA);
            sL.Add("Hello!");
            foreach (System.String s in sL)
            {
                Response.Write(s);
                Response.Write("<br/>");
                //Console.WriteLine(s);
            }
            System.String[] nextString = sL.ToArray();
            //Console.WriteLine("The Length of nextString is {0}", nextString.Length);
            //Console.Read();
            Response.Write("The Length of nextString is :"+nextString.Length);
        }

結果:

參考:http://www.jb51.net/article/32390.htm


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM