Geeks With Blogs

News My Blog has been MOVED to https://mfreidge.wordpress.com
Michael Freidgeim's OLD Blog My Blog has been MOVED to https://mfreidge.wordpress.com

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 on Friday, December 12, 2008 10:34 PM .Net Framework | Back to top


Comments on this post: ToString function for Generic List

# re: ToString function for Generic List
Requesting 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.
Left by Yann Schwartz on Dec 14, 2008 10:08 PM

# re: ToString function for Generic List
Requesting 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.
Left by Michael Freidgeim on Dec 14, 2008 11:33 PM

# re: ToString function for Generic List
Requesting 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();
}

Left by James Curran on Dec 15, 2008 10:00 AM

# re: ToString function for Generic List
Requesting 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.
Left by Michael Freidgeim on Dec 15, 2008 10:52 PM

# re: ToString function for Generic List
Requesting Gravatar...
Hi James,
Actually I think your code is more concise and efficient(no conversion to Array and subsequent Join).
Left by Michael Freidgeim on Dec 15, 2008 11:46 PM

Your comment:
 (will show your gravatar)


Copyright © Michael Freidgeim | Powered by: GeeksWithBlogs.net