1

I have two lists in the Main method and pass them to another method where I want to find the unique values. I can find the unique values but I keep getting errors when I try to return both string arrays. Here is my code. I want to return names1 and names2 from UniqueNames

 using System;
 using System.Linq;

 namespace ConsoleApp1
 {
   class Program
   {
    static void Main(string[] args)
    {
        string[] names1 = new string[] { "Ava", "Emma", "Olivia" };
        string[] names2 = new string[] { "Olivia", "Sophia", "Emma" };
        Console.WriteLine(string.Join(", ", Program.UniqueNames(names1, names2)));
    }

    public static string[] UniqueNames(string[] names1, string[] names2)
    {

        names1 = names1.Where(s => !names2.Contains(s)).ToArray();
        names2 = names2.Where(s => !names1.Contains(s)).ToArray();
        return (names1, names2);

    }
  }
}
BrianMichaels
  • 522
  • 1
  • 7
  • 16

2 Answers2

3

As of C# version 7.0, tuples are supported as such:

public static (string[], string[]) UniqueNames(string[] names1, string[] names2){/*…*/}
rfmodulator
  • 3,638
  • 3
  • 18
  • 22
2

https://learn.microsoft.com/en-us/dotnet/csharp/tuples

    ...
    (names1, names2) = Program.UniqueNames(names1, names2);
    Console.WriteLine(string.Join(", ", names1.Union(names2)));
}

public static (string[], string[]) UniqueNames(string[] names1, string[] names2)
{
    names1 = names1.Where(s => !names2.Contains(s)).ToArray();
    names2 = names2.Where(s => !names1.Contains(s)).ToArray();
    return (names1, names2);
}
Igor
  • 15,833
  • 1
  • 27
  • 32