Sunday 7 June 2009

Array To String

Oh, how I hate String.join(string, string[]) in mscorlib. And it is because of string[] argument. Why couldn't they make it accept array of objects and, before joining, cast every object with ToString() method?

Had to make it myself:

public string ListJoin<T>(string separator, T[] objs)//C# v3
{
  return string.Join(separator, Array.ConvertAll(objs, item => item.ToString()));
}

public string ListJoin<T>(string separator, T[] objs)//C# v2
{
  return string.Join(separator, Array.ConvertAll<T,string>(value, new Converter<T,string>(delegate(T i){return i.ToString();})));
}

No comments:

Post a Comment