Last active
May 14, 2020 17:20
-
-
Save escalonn/ef2197c72828e39c52db54933ac548ae to your computer and use it in GitHub Desktop.
delegate-based polymorphism without generics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
namespace DelegateExample | |
{ | |
class Program | |
{ | |
static void Main() | |
{ | |
var list1 = new ArrayList { 1, 2, 3 }; | |
var list2 = new ArrayList { 1, 3, 2 }; | |
bool sequenceEqual = CompareList(list1, list2, (a, b) => (int)a == (int)b); | |
int duplicate = 0; | |
bool hasNoDuplicates = CompareList(list1, list2, (a, b) => { | |
if ((int)a != (int)b) return true; | |
duplicate = (int)a; | |
return false; | |
}); | |
} | |
delegate bool Checker(object a, object b); | |
static bool CompareList(ArrayList listA, ArrayList listB, Checker check) | |
{ | |
if (listA.Count != listB.Count) return false; | |
for (int i = 0; i < listB.Count; i++) | |
{ | |
if (!check(listA[i], listB[i])) | |
{ | |
return false; | |
} | |
} | |
return true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment