0

I read in this article that delegates are late binding mechanism, but this SO answer and this SO answer are stating that this is not the case.

If I have this code sample where I try to pass different different delegate to my Sort method at runtime, is this a late binding ?

To me it seems like passing a reference to a delegate instance is not a late binding mechanism, because this is a just normal(not 100% sure about that) reference like any other reference to an instance of an object. Is this correct ?


delegate int Comparer(int x, int y);

class Program
{
    static void Main(string[] args)
    {
        Comparer comparer;
        var key = Console.ReadLine();
        if (key == "asc")
        {
            comparer = (x, y) => x.CompareTo(y); 
        }
        else
        {
            comparer = (x, y) => -x.CompareTo(y);
        }

        ListSorter myList = new ListSorter(ListGenerator.RandomList(size: 25));
        myList.Sort(comparer);
    }
}

class ListSorter
{
    private List<int> _list;

    public ListSorter(IEnumerable<int> list)
    {
        this._list = new List<int>(list);
    }

    public void Sort(Comparer comparer)
    {
        QuickSort(comparer, 0, this._list.Count - 1);
    }

    private void QuickSort(Comparer comparer, int left, int right)
    {
        if (left >= right)
        {
            return;
        }

        int partition = Partition(comparer, left, right);
        QuickSort(comparer, left, partition - 1);
        QuickSort(comparer, partition + 1, right);
    }

    private int Partition(Comparer comparer, int left, int right)
    {
        int s = left - 1;
        int c = left;
        int p = this._list[right];

        while (c < right)
        {
            if (comparer(this._list[c], p) < 0)
            {
                s++;
                Swap(s, c);
            }
            c++;
        }

        s++;
        Swap(s, right);

        return s;
    }
}
Ivan Ruski
  • 1,123
  • 1
  • 13
  • 19

1 Answers1

0

This is what wikipedia has to say: "Late binding, dynamic binding[1], or dynamic linkage[2] is a computer programming mechanism in which the method being called upon an object or the function being called with arguments is looked up by name at runtime."

Comapred to early or static binding, Delegates could qualify as Dynamic Binding. Delegates are the backbone behind Events, wich are propably the most common example of Late/Dynamic Binding you can find in programming. All Events do is wrap a Delegate into add and remove accessors.

However: "With early binding, or static binding, in an object-oriented language, the compilation phase fixes all types of variables and expressions. This is usually stored in the compiled program as an offset in a virtual method table ("v-table") and is very efficient. With late binding the compiler does not read enough information to verify the method exists or bind its slot on the v-table. Instead the method is looked up by name at runtime." does sound more like Reflection is the droid you are looking for.

Primarily Delegates are a replacement for Pointers. One of the many replacements the "let us avoid naked pointers" rule made nessesary. They are a very typesafe version of it however. So it is a clear "yes and no". It has part of the properties, but not all of them.

Christopher
  • 9,634
  • 2
  • 17
  • 31