I have two methods that display: one a List of items and one a HashSet of items. The methods are identical. Is there a way to write the method with a parameter type so that I can use the same method regardless of the type of the argument passed to the method: List<T> or HashSet<T>?
The methods are:
public static void DisplayList<T>(List<T> list)
{
int i = 0;
foreach (T item in list)
{
Console.Write(item.ToString());
if (i < list.Count - 1)
{
Console.Write(", ");
}
i++;
}
Console.WriteLine();
}
public static void DisplayHashSet<T>(HashSet<T> set)
{
int i = 0;
foreach (T item in set)
{
Console.Write(item.ToString());
if (i < set.Count - 1)
{
Console.Write(", ");
}
i++;
}
Console.WriteLine();
}