Common method for outputting arrays and lists of any type

In unit tests I might want to output the results of a returned list of any type. Instead of writing a new method for each type, here are some techniques with thanks to:

Using Join

Split is a useful function, here is the reverse Join

int[] actual = new int[] {1,2,3}

string actual = new string[] {“one”, “two”, “three”}

TestContext.WriteLine(string.Join(“, “, actual));


Using Generics

public void PrintCollection<T>(IEnumerable<T> col)
foreach (var item in col) TestContext.WriteLine(item);


Using ForEach(Action a)

publicvoid AListTest()
List<string> target = newList<string>();

privatevoid PrintCollection2(string s)




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: