うにてぃブログ

主にUnityとC#に関する記事を書いていきます

【C#】配列をString化して返す

using System.Collections.Generic;
 
public static class ListUtil
{
    public static string Display<T>(this IList<T> self)
    {
        if (self == null || self.Count <= 0)
                return "";
  
        var builder = new System.Text.StringBuilder();
        builder.AppendLine("[Display IList]");
        for (var i = 0; i < self.Count; i++)
            builder.AppendLine(string.Format("[{0}] : {1}", i, self[i]));
 
        return builder.ToString();
    }
}
using System.Collections.Generic;
 
private void Sample()
{
    var list = new List<int>()
    {
        1, 2, 3, 4, 5
    };
    // [Display IList]
    // [0] : 1
    // [1] : 2
    // [2] : 3
    // [3] : 4
    // [4] : 5
    list.Display();
}