Skip to content

Instantly share code, notes, and snippets.

@escalonn
Last active May 14, 2020 17:20
Show Gist options
  • Save escalonn/ef2197c72828e39c52db54933ac548ae to your computer and use it in GitHub Desktop.
Save escalonn/ef2197c72828e39c52db54933ac548ae to your computer and use it in GitHub Desktop.
delegate-based polymorphism without generics
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