码迷,mamicode.com
首页 > 编程语言 > 详细

C#中List〈string〉和string[]数组之间的相互转换

时间:2016-08-26 19:47:32      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:

 

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

C#中List〈string〉和string[]数组之间的相互转换

标签:

原文地址:http://www.cnblogs.com/wangfuyou/p/5811371.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!