Questions tagged [delegates]

Delegates can refer to several concepts. An object can rely on another (a delegate) to perform a function. Delegation can also refer to programming language feature making use of the method lookup rules for dispatching self-calls. In C#, a delegate defines which method to call when an event is triggered.

In object-oriented programming, there are three related notions of delegation.

Most commonly, delegation refers to a programming language feature making use of the method lookup rules for dispatching so-called self-calls. Delegation as a language feature supports the prototype-based programming model.

Delegation can also refer to one object relying upon another to provide a specified set of functionalities. In research, this is often referred to as consultation or as aggregation in modeling.

In C#, a delegate is a way of telling which method to call when an event is triggered, keeping the method type.

A Delegate is a type that safely encapsulates a method, similar to a function pointer in C and C++. Unlike C function pointers, delegates are object-oriented, type safe, and secure.

Official Source

11320 questions
159
votes
10 answers

Delegates: Predicate vs. Action vs. Func

Can someone provide a good explanation (hopefully with examples) of these 3 most important delegates: Predicate Action Func
Sasha
159
votes
1 answer

Difference Between Invoke and DynamicInvoke

What is the difference between Invoke and DynamicInvoke in delegates? Please give me some code example which explain difference between that two methods.
testCoder
  • 7,155
  • 13
  • 56
  • 75
156
votes
12 answers

How to hide the keyboard when I press return key in a UITextField?

Clicking in a textfield makes the keyboard appear. How do I hide it when the user presses the return key?
aden
  • 1,907
  • 3
  • 16
  • 16
153
votes
8 answers

Why can't an anonymous method be assigned to var?

I have the following code: Func comparer = delegate(string value) { return value != "0"; }; However, the following does not compile: var comparer = delegate(string value) { return value != "0"; }; Why can't the compiler…
Marlon
  • 19,924
  • 12
  • 70
  • 101
152
votes
10 answers

How can I clear event subscriptions in C#?

Take the following C# class: c1 { event EventHandler someEvent; } If there are a lot of subscriptions to c1's someEvent event and I want to clear them all, what is the best way to achieve this? Also consider that subscriptions to this event could…
programmer
  • 4,342
  • 4
  • 24
  • 21
144
votes
15 answers

Delegates in swift?

How does one go about making a delegate, i.e. NSUserNotificationCenterDelegate in swift?
user3718173
  • 1,443
  • 2
  • 10
  • 4
143
votes
8 answers

Super-simple example of C# observer/observable with delegates

I recently started digging into C# but I can't by my life figure out how delegates work when implementing the observer/observable pattern in the language. Could someone give me a super-simple example of how it is done? I have googled this, but all…
Deniz Dogan
  • 25,711
  • 35
  • 110
  • 162
141
votes
9 answers

What is Func, how and when is it used

What is Func<> and what is it used for?
learning
  • 11,415
  • 35
  • 87
  • 154
139
votes
4 answers

How do I set up a simple delegate to communicate between two view controllers?

I have two UITableViewControllers and need to pass the value from the child view controller to the parent using a delegate. I know what delegates are and just wanted to see a simple to follow example. Thank You
Strong Like Bull
  • 11,155
  • 36
  • 98
  • 169
134
votes
9 answers

Uses of Action delegate in C#

I was working with the Action Delegates in C# in the hope of learning more about them and thinking where they might be useful. Has anybody used the Action Delegate, and if so why? or could you give some examples where it might be useful?
Biswanath
  • 9,075
  • 12
  • 44
  • 58
131
votes
9 answers

Why must a lambda expression be cast when supplied as a plain Delegate parameter

Take the method System.Windows.Forms.Control.Invoke(Delegate method) Why does this give a compile time error: string str = "woop"; Invoke(() => this.Text = str); // Error: Cannot convert lambda expression to type 'System.Delegate' // because it is…
xyz
  • 27,223
  • 29
  • 105
  • 125
130
votes
14 answers

Wrap a delegate in an IEqualityComparer

Several Linq.Enumerable functions take an IEqualityComparer. Is there a convenient wrapper class that adapts a delegate(T,T)=>bool to implement IEqualityComparer? It's easy enough to write one (if your ignore problems with defining a correct…
Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
127
votes
3 answers

"Delegate subtraction has unpredictable result" in ReSharper/C#?

When using myDelegate -= eventHandler ReSharper (version 6) issues: Delegate subtraction has unpredictable result The rational behind this is explained by JetBrains here. The explanation makes sense and, after reading it, I'm doubting all my uses…
user166390
124
votes
7 answers

Are C# events synchronous?

There are two parts to this question: Does raising an event block the thread, or does it start execution of EventHandlers asynchronously and the thread goes continues on at the same time? Are the individual EventHandlers (subscribed to the event)…
Alexander Bird
  • 38,679
  • 42
  • 124
  • 159
113
votes
22 answers

How do I know that the UICollectionView has been loaded completely?

I have to do some operation whenever UICollectionView has been loaded completely, i.e. at that time all the UICollectionView's datasource / layout methods should be called. How do I know that?? Is there any delegate method to know UICollectionView…
Jirune
  • 2,310
  • 3
  • 21
  • 19