当前位置: 移动技术网 > IT编程>开发语言>c# > C#中List〈string〉和string[]数组之间的相互转换

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

2019年07月18日  | 移动技术网IT编程  | 我要评论

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

结果显示:

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网