หากต้องการแยกอาร์เรย์สองอาร์เรย์ ให้ใช้เมธอด Intersect เป็นวิธีการขยายจากเนมสเปซ System.Linq
วิธีการส่งคืนองค์ประกอบทั่วไประหว่างสองอาร์เรย์
ตั้งค่าสองอาร์เรย์ก่อน -
int[] arr1 = { 44, 76, 98, 34 };
int[] arr2 = { 24, 98, 44, 55, 47, 86 }; ตอนนี้ใช้ Intersect กับอาร์เรย์ทั้งสอง -
Arr1.Intersect(arr2);
ต่อไปนี้เป็นรหัสที่สมบูรณ์ -
ตัวอย่าง
using System;
using System.Linq;
class Program {
static void Main() {
int[] arr1 = { 44, 76, 98, 34 };
int[] arr2 = { 24, 98, 44, 55, 47, 86 };
var intersect = arr1.Intersect(arr2);
foreach (int res in intersect) {
Console.WriteLine(res);
}
}
} ผลลัพธ์
44 98