Tuesday, 10 March 2009

Implementing Generic method in C# to print contents of List and Dictionary

public static string ToString<T>(List<T> list)
{
  StringBuilder builder = new StringBuilder();
  if (list != null)
  {
    foreach (T t in list)
    {
      builder.Append(t.ToString());
    }
  }
  return builder.ToString();
}
public static string ToString<T,K>(Dictionary<T,K> dictionary)
{
  StringBuilder builder = new StringBuilder();
  if (dictionary != null)
  {
    foreach (KeyValuePair<T, K> pair in list)
    {
      builder.Append(pair.ToString());
    }
  }
  return builder.ToString();
}

No comments:

Post a Comment