In the code below I'm running the RepeatedNames method on a list called listOfNames with some of them being repeated. So the method is supposed to store the repeated ones in another list (_repeatedNames) and printing them out in the console.
But it doesn't run.
Just to mention: I'm new to C# and trying to learn in a practical way through coding, so there must be something obvious that I'm doing wrong.
static void Main(string[] args)
{
List<string> listOfNames = new List<string>() { "Sam", "Mark", "Nick", "Laura", "Mark", "Helen", "Nick" };
RepeatedNames(listOfNames);
}
private List<string> _names = new List<string>();
private List<string> _repeatedNames = new List<string>();
static void RepeatedNames(List<string> NameCollection)
{
foreach (var name in NameCollection)
{
if (!_names.Contains(name))
{
_names.Add(name);
}
else
{
_repeatedNames.Add(name);
Console.Write(name + "\t");
}
}