c# 數組間相互轉換 int[] string[] object[]

ee數組

//字符串數組(源數組)
string[] sNums = new[] {"1", "2"};

//整型數組(目標數組)
int[] iNums;

//轉換方法
iNums = Array.ConvertAll<string, int>(sNums , s => int.Parse(s));

//string轉Object
object[] objArr = Array.ConvertAll<string, object>(sNums , s => (object)s);

//轉換方法-簡寫
iNums = Array.ConvertAll<string, int>(sNums , int.Parse);

//轉換方法-繼續簡寫
iNums = Array.ConvertAll(sNums , int.Parse);

 

  • Array.ConvertAll(sNums , int.Parse) : 將一種類型的數組轉換成另外一種類型的數組,轉換方法支持Lambda表達式
  • sNums : 要轉換成目標數組的源數組
  • int.Parse : 將源數據類型轉換成目標數據類型的強制轉換方法

 

出處:https://blog.csdn.net/u012143455/article/details/70157233spa

相關文章
相關標籤/搜索