Example1: Returns true if the two specified lists are reference equal (are the same instance).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | using System; using System.Collections.Generic; using System.Linq; class Program { static void Main(string[] args) { var listA = new List<int>() { 10, 20, 30 }; var listB = listA; bool result = listA.Equals(listB); Console.Write(result); Console.ReadLine(); } } |
Output:
1 2 3 | True |
Example 2: Returns false if the specified two lists are not the same instance.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | using System; using System.Collections.Generic; using System.Linq; class Program { static void Main(string[] args) { var listA = new List<int> { 100, 200, 300, 400, 500, 600, 700}; var listB = new List<int> { 100, 200, 300, 400, 500, 600, 700 }; bool result = listA.Equals(listB); Console.WriteLine(result); Console.ReadLine(); } } |
Output:
1 2 3 | False |
Example 3:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | using System; using System.Collections.Generic; class Program { static void Main() { List<int> la = new List<int>() { 1, 0, 4, 200, -40 }; List<int> lb = new List<int>() { -40, 200, 4, 1, 0 }; List<int> lc = new List<int>() { 3, 5, 4, 9, 11 }; List<int> ld = new List<int>() { 6, 6, 100 }; List<int> le = new List<int>() { 6, 100, 100 }; Console.WriteLine(UnorderedEqual(la, lb)); // true Console.WriteLine(UnorderedEqual(la, lc)); // false Console.WriteLine(UnorderedEqual(lc, ld)); // false Console.WriteLine(UnorderedEqual(ld, le)); // false int[] a1 = new int[] { 1, 2, 5 }; int[] a2 = new int[] { 2, 5, 1 }; int[] a3 = new int[] { 1, 1, 3 }; int[] a4 = new int[] { 3, 3, 1 }; Console.WriteLine(UnorderedEqual(a1, a2)); // true Console.WriteLine(UnorderedEqual(a1, a3)); // false Console.WriteLine(UnorderedEqual(a3, a4)); // false } static bool UnorderedEqual<T>(ICollection<T> a, ICollection<T> b) { // 1 // Require that the counts are equal if (a.Count != b.Count) { return false; } // 2 // Initialize new Dictionary of the type Dictionary<T, int> d = new Dictionary<T, int>(); // 3 // Add each key's frequency from collection A to the Dictionary foreach (T item in a) { int c; if (d.TryGetValue(item, out c)) { d[item] = c + 1; } else { d.Add(item, 1); } } // 4 // Add each key's frequency from collection B to the Dictionary // Return early if we detect a mismatch foreach (T item in b) { int c; if (d.TryGetValue(item, out c)) { if (c == 0) { return false; } else { d[item] = c - 1; } } else { // Not in dictionary return false; } } // 5 // Verify that all frequencies are zero foreach (int v in d.Values) { if (v != 0) { return false; } } // 6 // We know the collections are equal return true; } } |
Output:
1 2 3 4 5 6 7 8 9 | True False False False True False False |
Source: www.dotnetperls.com/list-equals