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();
测试如下:
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();
}
}
}
结果显示:

相关文章
C#获取某路径文件夹中全部图片或其它指定格式的文件名的实例方法
在本篇文章里小编给大家整理的是关于C#获取某路径文件夹中全部图片或其它指定格式的文件名的实例方法,需要的朋友们参考下。2019-10-10C# string转换为几种不同编码的Byte[]的问题解读
这篇文章主要介绍了C# string转换为几种不同编码的Byte[]的问题解读,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2023-02-02基于Unity Line Renderer组件的常用属性说明
这篇文章主要介绍了基于Unity Line Renderer组件的常用属性说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2021-04-04C# 计算DataTime的4种时间差的方法(相差天数、相差小时、相差分钟、相差秒)
这篇文章主要介绍了C# 计算DataTime的4种时间差(相差天数、相差小时、相差分钟、相差秒),本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2023-05-05
最新评论