ToString function for Generic List

Below is the method to output Generic list of objects as string.

 

UPDATE: I've replaced my original function with the code suggested by James Curran (see his comment below).

 

    public static string ToString<T>(IEnumerable<T> messages, string sComment)
    {
        StringBuilder sRet = new StringBuilder(sComment);
        if (messages != null)
        {
            foreach (T msg in messages)
            {
                sRet.AppendLine(msg.ToString());
            }
        }
        return sRet.ToString();
    }

 

 

       /// <summary>

        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="messages"></param>
        /// <param name="separator"></param>
        /// <param name="sComment"></param>
        /// <returns></returns>
        /// See also http://www.codemeit.com/linq/c-array-delimited-tostring.html
        public static string ToString<T>(this IEnumerable<T> messages, string separator, string sComment)
        {
            if (messages == null)
                throw new ArgumentException("source can not be null.");
            StringBuilder sb = new StringBuilder(sComment);
            if (string.IsNullOrEmpty(separator))
            {
                separator = Environment.NewLine;
            }
            if (messages != null)
            {
                foreach (T msg in messages)
                {
                    if (msg != null)         
{

sb.Append(msg.ToString());

sb.Append(separator);

}
}

            }
            string sRet = StringHelper.TrimEnd(sb.ToString(), separator);
            return sRet;
        }
 

Similar function implemented as an extension method described in post:

Separator Delimited ToString for Array, List, Dictionary, Generic IEnumerable

 

posted @ Friday, December 12, 2008 10:34 PM

Print

Comments on this entry:

# re: ToString function for Generic List

Left by Yann Schwartz at 12/14/2008 10:08 PM
Gravatar
Great way to thrash your memory and CPU.

You really should use a StringBuilder instead of string concatenation when concatening an arbitrary number of strings.

# re: ToString function for Generic List

Left by Michael Freidgeim at 12/14/2008 11:33 PM
Gravatar
Yann Schwartz,
Good point, thanks.
My blog has reference to http://www.yoda.arachsys.com/csharp/stringbuilder.html , but I made this mistake myself.

# re: ToString function for Generic List

Left by James Curran at 12/15/2008 10:00 AM
Gravatar
Also, It's particularly unclear what's the purpose of "sComment". I guess it's supposed to be a header for the list, although, unless it specifically includes a NewLine, it'll be in front of the first item.

Also, a bit questionable is the line

sRet = sRet + sComment;

since, at the point it's called, there's no way for sRet to be anything beside "".

And, "string" + null == "string", so the first four lines can be reduced to

string sRet = sComment;

Finally, instead of forcing your users to always pass a List<T>, it should accepted a IEnumerable<T>, since that's the any ability you are using, and this will allow you to pass Arrays or Stacks or Queue or any of a whole bunch else beside Lists.
Putting it all together:
public static string ToString<T>(IEnumerable<T> messages, string sComment)
{
StringBuilder sRet = new StringBuilder(sComment);
if (messages != null)
{
foreach (T msg in messages)
{
sRet.AppendLine(msg.ToString());
}
}
return sRet.ToString();
}

# re: ToString function for Generic List

Left by Michael Freidgeim at 12/15/2008 10:52 PM
Gravatar
Hi James,
I agree with all your comments. The function was actually refactoring of application specific code into generic function without much reviewing the code.
I am going to replace the whole function with extension method from Separator Delimited ToString for Array, List, Dictionary, Generic IEnumerable.
Thanks for pointing to redundant conditions for header sComment. The same bad pattern I've used in a few methods of my TraceHelper class.
Thanks.

# re: ToString function for Generic List

Left by Michael Freidgeim at 12/15/2008 11:46 PM
Gravatar
Hi James,
Actually I think your code is more concise and efficient(no conversion to Array and subsequent Join).

Your comment:



 (will not be displayed)


 
 
 
 
 

Live Comment Preview:

 
«November»
SunMonTueWedThuFriSat
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345