Monday, January 30, 2012

Best Extension Methods: From IEnumerable To String

This extension method creates a string with specified separator from an IEnumerable List.
/// 
        /// Concatenates a specified separator String between each element of a specified enumeration, yielding a single concatenated string.
        /// 
        /// any object
        /// The enumeration
        /// A String
        /// A String consisting of the elements of value interspersed with the separator string.
        public static string ToString(this IEnumerable list, string separator)
        {
            StringBuilder sb = new StringBuilder();
            foreach (var obj in list)
            {
                if (sb.Length > 0)
                {
                    sb.Append(separator);
                }
                sb.Append(obj);
            }
            return sb.ToString();
        }

Compiled By: Rajesh Rolen

Share This!


No comments:

Powered By Blogger · Designed By Seo Blogger Templates