CSharp examples for Language Basics:Array
Set operation on array
using System;//from w w w .j av a 2 s.c o m using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Net.Http; using System.Threading.Tasks; using static System.Console; using static System.Diagnostics.Process; class Program { static void Main(string[] args) { var test1 = new string[]{ "A", "B", "C", "D" }; var test2 = new string[]{ "Jack", "S", "X", "Jack", "Z" }; var test3 = new string[]{ "D", "Jack", "Jack", "G", "Conor" }; Output(test1, "Test 1"); Output(test2, "Test 2"); Output(test3, "Test 3"); Output(test2.Distinct(), "test2.Distinct(): removes duplicates"); Output(test2.Union(test3),"test2.Union(test3): combines and removes duplicates"); Output(test2.Concat(test3),"test2.Concat(test3): combines but leaves duplicates"); Output(test2.Intersect(test3),"test2.Intersect(test3): items that are in both sequences"); Output(test2.Except(test3),"test2.Except(test3): removes items from the first sequence that are in the second sequence"); Output(test1.Zip(test2,(c1, c2) => $"{c1} matched with {c2}"),"test1.Zip(test2, (c1, c2) => $\"{c1} matched with {c2}\")" +": matches items based on position in the sequence"); } private static void Output(IEnumerable<string> test, string description = "") { if (!string.IsNullOrEmpty(description)) { WriteLine(description); } Write(" "); WriteLine(string.Join(", ", test.ToArray())); } }