Evolution of delegates [closed]
Asked Answered
O

4

5

I was trying to understand the evolution of delegates. And how best can this be understood than by understanding what came before it? I understand that the concept of delegates has come from function pointers in C++.

1. What is the primary reason for introducing the function pointers? Is it to support multi-threading?

Henk Holterman: Delegates / function-pointers exist to provide a level of flexibility. The selection of a method can be decoupled form its invocation.

I have been introduced to delegates while looking at this great threading resource

http://www.albahari.com/threading/

This is what Joe Albahari has to say about asynchronous delegates:

ThreadPool.QueueUserWorkItem doesn’t provide an easy mechanism for getting return values back from a thread after it has finished executing. Asynchronous delegate invocations (asynchronous delegates for short) solve this, allowing any number of typed arguments to be passed in both directions. Furthermore, unhandled exceptions on asynchronous delegates are conveniently rethrown on the original thread (or more accurately, the thread that calls EndInvoke), and so they don’t need explicit handling.

*2. Are all delegates asynchronous by nature?

Jon: A delegate is a pointer to code. It is not inherently either synchronous or asynchronous; the manner on which it is invoked and the result returned determines that.

Henk Holterman:When you have a delegate instance f you can synchronously call f() or you can call the asynchronous f.BeginInvoke()

  1. Is there any other uses of asynchronous delegates other than events?

dasblinkenlight: There are many uses of delegates in asynchronous APIs, for example, .NET Asynchronous I/O.

  1. How has the delegate/thread support in C# evolved over the versions?
    *

dasblinkenlight: The core support of delegates remained the same, but the language added many important features to make it more convenient to define delegates.

C# 2.0: Introduction of anonymous delegates
C# 3.5: Added lambda expressions
C# 4.0: Added Task Parallel Library.

Edit: Most of my queries here have been clarified. If I could get a "history" for delegates and more practical usages for them, it would be great!

Outturn answered 18/9, 2012 at 11:41 Comment(2)
A delegate is a pointer to code. It is not inherently either synchronous or asynchronous; the manner on which it is invoked and the result returned determines that. The delegate is unaware of it.Cnut
@Jon, Thanks! Updated the question with this.Outturn
D
4
  1. The primary reason for introducing delegates is to provide a mechanism for manipulating snippets of executable code: storing pointers to it, passing to functions, organize in data structures, and so on.

  2. Like all function invocations, invoking an individual delegate is synchronous, in the sense that the caller must wait for the delegate to finish before getting the control back. However, you can use delegates to implement asynchronous APIs by passing a piece of code that could be stored and executed later on (asynchronously).

  3. There are many uses of delegates in asynchronous APIs, for example, .NET Asynchronous I/O.

  4. The core support of delegates remained the same, but the language added many important features to make it more convenient to define delegates. For example, C#2.0 added anonymous delegates, and C#3.5 added lambdas.

Domitian answered 18/9, 2012 at 11:53 Comment(0)
B
4

Let's try a few:

1) delegates / function-pointers exist to provide a level of flexibility.
They allow the selection of a method to be be decoupled from its invocation.

2) When you have a delegate instance f you can synchronously call f() or you can call the asynchronous f.BeginInvoke()

3) Yes, many. And events are usually invoked synchronously.

4) Is that relevant for understanding them?

Bunny answered 18/9, 2012 at 11:51 Comment(1)
For point 4: Yes, I believe so. I want to understand how a particular feature became what it is today. What need was the mother of that particular invention.Outturn
D
4
  1. The primary reason for introducing delegates is to provide a mechanism for manipulating snippets of executable code: storing pointers to it, passing to functions, organize in data structures, and so on.

  2. Like all function invocations, invoking an individual delegate is synchronous, in the sense that the caller must wait for the delegate to finish before getting the control back. However, you can use delegates to implement asynchronous APIs by passing a piece of code that could be stored and executed later on (asynchronously).

  3. There are many uses of delegates in asynchronous APIs, for example, .NET Asynchronous I/O.

  4. The core support of delegates remained the same, but the language added many important features to make it more convenient to define delegates. For example, C#2.0 added anonymous delegates, and C#3.5 added lambdas.

Domitian answered 18/9, 2012 at 11:53 Comment(0)
P
2

Function variables come from way back in the 60's, in LISP. Even the term Lambda Expression is from there. The entire concept is called "functions as first class objects," or just "1st-class functions." And LISP is famous - people still study it in school because it handles 1st-class functions so smoothly - better than function pointers or delegates. When people made new languages, they all knew about that cool LISP feature they should try to copy.

Used in a sentence "delegates are the way C# implements 1st-class functions."

Since C# tries to use C++ syntax, why didn't it use C++-style function pointers? I assume it's the whole unsafe pointers thing, and that most C# users are steered away from using pointers anyway.

The keyword "delegate" is C#-specific. My best guess is it's because of the Delegate Design Pattern (which uses function-pointers.) Patterns were huge back then, everyone wanted their code to clearly be using them. Plus most C# users didn't have a lot of formal training. So delegate as a keyword was a clever way to say "this is the thing you need for the Delegate pattern." I assume the decline in popularity of Patterns is why the newer (x)=>{return x+1;} version of anonymous functions removes that keyword (and uses the more general term, lambda expression.)

Passbook answered 28/2, 2017 at 2:26 Comment(0)
G
0

1.Primary use of function pointer is for supporting tow thing Functions as Arguments to Other Functions and Callback Functions, and i think it is not multi-threaded .

2.All delegates are not asynchronous by nature and must be used with Thread to running async as in the mentioned article

3.One of famous usage is in Lambda expressions and LINQ, callbacks, starting threads and event handling

4.I think basically delegate is using from function pointer mechanism

Gonick answered 18/9, 2012 at 12:1 Comment(1)
If you could explain your point about LINQ and call back functions, it would be great!Outturn

© 2022 - 2024 — McMap. All rights reserved.