您的位置:首页 > 编程语言 > C#

c#不同数组之间的转换【转载,消化自动删除】

2017-01-25 15:05 429 查看

c#中从string数组转换到int数组

string[] input = { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
int[] output = Array.ConvertAll<string, int>(input, delegate(string s)

{

return int.Parse(s);

});

注:

使用Array类中的静态泛形式方法ConvertAll进行转换

delegate(string s) { return int.Parse(s); }

这句表示:建立一个匿名委托,该委托关联的方法体是:return int.Parse(s);

将数组中的每个字符串强制转换成整形并返回添加给 output

c#中如何将一个string数组转换为int数组

string[] strArray = "a,b,c,d,e,f,g".Split(new char[]{ ',' });
int[] intArray;

//C# 3.0下用此句
intArray = Array.ConvertAll<string, int>(strArray, s => int.Parse(s));
//2.0下用以下的语句替换上例。
//intArray = Array.ConvertAll<string, int>(strArray, delegate (string s) { return int.Parse(s); } );

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();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: