List和string[]數組之間的相互轉換,須要的朋友能夠參考下

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

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

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

 

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

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

listS.Add("str");class

listS.Add("hello");foreach

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

 

測試以下:next

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;static

namespace ConsoleApplication1{    class Program    {        static void Main(string[] args)        {            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)            {                Console.WriteLine(s);            }            System.String[] nextString = sL.ToArray();            Console.WriteLine("The Length of nextString is {0}",nextString.Length);            Console.Read();        }    }}

相關文章
相關標籤/搜索