How to join int[] to a character separated string in .NET?

后端 未结 11 830
广开言路
广开言路 2020-11-29 19:17

I have an array of integers:

int[] number = new int[] { 2,3,6,7 };

What is the easiest way of converting these into a single string where

11条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-29 19:42

    Although the OP specified .NET 3.5, people wanting to do this in .NET 2.0 with C#2 can do this:

    string.Join(",", Array.ConvertAll(ints, Convert.ToString));
    

    I find there are a number of other cases where the use of the Convert.xxx functions is a neater alternative to a lambda, although in C#3 the lambda might help the type-inferencing.

    A fairly compact C#3 version which works with .NET 2.0 is this:

    string.Join(",", Array.ConvertAll(ints, item => item.ToString()))
    

提交回复
热议问题