前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C#中string[]数组和list<string>泛型的相互转换 【转】

C#中string[]数组和list<string>泛型的相互转换 【转】

作者头像
DougWang
发布2020-02-18 16:17:51
1.4K0
发布2020-02-18 16:17:51
举报
文章被收录于专栏:java跬步

C#中string[]数组和list<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();

测试如下:

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

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();        

}     } }

结果显示:

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2012-11-17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档