Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to join these two methods into one using List<t></t>.
How to do it?

Current methods look as follows:

C#
protected string JoinList(List<int> list)
        {
            string result = string.Empty;
            if (list != null && list.Count > 0)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    result += list[i].ToString();
                    result += (i != list.Count - 1) ? "," : string.Empty;
                }
            }
            return result;
        }
        protected string JoinList(List<string> list)
        {
            string result = string.Empty;
            if (list != null && list.Count > 0)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    result += list[i];
                    result += (i != list.Count - 1) ? "," : string.Empty;
                }
            }
            return result;
        }
Posted

Here is a little example for you:
C#
// Our generic method
protected static string JoinList<T>(List<T> list)
{
    string result = string.Empty;
    if (list != null && list.Count > 0)
    {
        for (int i = 0; i < list.Count; i++)
        {
            result += list[i].ToString();
            result += (i != list.Count - 1) ? "," : string.Empty;
        }
    }
    return result;
}

// Test the method
static void Main(string[] args)
{
    // Prepare to lists
    List<int> lstInt = new List<int>();
    List<string> lstString = new List<string>();
    lstInt.Add(1);
    lstInt.Add(2);
    lstInt.Add(3);
    lstString.Add("one");
    lstString.Add("two");
    lstString.Add("three");

    // And the results...
    Console.WriteLine(JoinList<int>(lstInt));
    Console.WriteLine(JoinList<string>(lstString));
    Console.ReadKey();
}


For more information about generic methods see here[^]. :)
 
Share this answer
 
Comments
Pavel Yermalovich 13-May-10 8:14am    
Thank you
Beyond making the method static, here's an improved version of your method.

public static string JoinList(List<int> list)
{
    // start with an empty string
    StringBuilder builder = new StringBuilder("");
    if (list != null)
    {
        // build the string
        foreach(int number in list)
        {
            builder.AppendFormat(",{0}", number);
        }
        // remove the leading comma
        if (builder.Length > 0)
        {
            builder.RemoveAt(0);
        }
    }
    return builder.ToString();
}</int>


Using the StringBuilder is more efficient because it doesn't reallocate the string every time you add something to it.

Also, in the foreach loop, I could have checked to see if the string was empty for each item to avoid having the comma show up at the beginning of the string, but it's more efficient just to go ahead and create the string, and then take care of the leading comma one time after the string has been built. Just because computers have a lot of memory nowadays doesn't remove our responsibility for making the code as efficient as possible.
 
Share this answer
 
v2
Comments
Pavel Yermalovich 13-May-10 8:15am    
Thnx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900