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
1136
votes
12 answers

Why would you use Expression> rather than Func?

I understand lambdas and the Func and Action delegates. But expressions stump me. In what circumstances would you use an Expression> rather than a plain old Func?
Richard Nagle
  • 11,974
  • 3
  • 21
  • 16
895
votes
13 answers

Pass Method as Parameter using C#

I have several methods all with the same parameter types and return values but different names and blocks. I want to pass the name of the method to run to another method that will invoke the passed method. public int Method1(string) { // Do…
user31673
  • 13,245
  • 12
  • 58
  • 96
863
votes
3 answers

Func vs. Action vs. Predicate

With real examples and their use, can someone please help me understand: When do we need a Func delegate? When do we need an Action delegate? When do we need a Predicate delegate?
InfoLearner
  • 14,952
  • 20
  • 76
  • 124
772
votes
20 answers

How do I create delegates in Objective-C?

I know how delegates work, and I know how I can use them. But how do I create them?
Andy Jacobs
  • 15,187
  • 13
  • 60
  • 91
599
votes
8 answers

How can I make a weak protocol reference in 'pure' Swift (without @objc)

weak references don't seem to work in Swift unless a protocol is declared as @objc, which I don't want in a pure Swift app. This code gives a compile error (weak cannot be applied to non-class type MyClassDelegate): class MyClass { weak var…
hnh
  • 13,957
  • 6
  • 30
  • 40
424
votes
8 answers

When & why to use delegates?

I'm relatively new in C#, & I'm wondering when to use Delegates appropriately. they are widely used in events declaration, but when should I use them in my own code and why are they useful? why not to use something else? I'm also wondering when I…
iChaib
  • 469
  • 4
  • 10
  • 17
374
votes
11 answers

What are the differences between delegates and events?

What are the differences between delegates and an events? Don't both hold references to functions that can be executed?
Sean Chambers
  • 8,572
  • 7
  • 41
  • 55
252
votes
14 answers

Unsubscribe anonymous method in C#

Is it possible to unsubscribe an anonymous method from an event? If I subscribe to an event like this: void MyMethod() { Console.WriteLine("I did it!"); } MyEvent += MyMethod; I can un-subscribe like this: MyEvent -= MyMethod; But if I…
Eric
  • 4,201
  • 5
  • 27
  • 36
224
votes
14 answers

Java Delegates?

Does the Java language have delegate features, similar to how C# has support for delegates?
markus
  • 2,608
  • 3
  • 19
  • 11
193
votes
6 answers

delegate keyword vs. lambda notation

Once it is compiled, is there a difference between: delegate { x = 0; } and () => { x = 0 } ?
MojoFilter
  • 12,256
  • 14
  • 53
  • 61
178
votes
6 answers

What is a C++ delegate?

What is the general idea of a delegate in C++? What are they, how are they used and what are they used for? I'd like to first learn about them in a 'black box' way, but a bit of information on the guts of these things would be great too. This is not…
Dollarslice
  • 9,917
  • 22
  • 59
  • 87
176
votes
4 answers

Why are Objective-C delegates usually given the property assign instead of retain?

I'm surfing through the wonderful blog maintained by Scott Stevenson, and I'm trying to understand a fundamental Objective-C concept of assigning delegates the 'assign' property vs 'retain'. Note, the both are the same in a garbage collected…
anon
166
votes
13 answers

What is Delegate?

I am confused that what is the actual role of a delegate? I have been asked this question many times in my interviews, but I don't think that interviewers were satisfied with my answer. Can anyone tell me the best definition, in one sentence, with…
Naveed
  • 41,517
  • 32
  • 98
  • 131
165
votes
4 answers

What's the best way to communicate between view controllers?

Being new to objective-c, cocoa, and iPhone dev in general, I have a strong desire to get the most out of the language and the frameworks. One of the resources I'm using is Stanford's CS193P class notes that they have left on the web. It includes…
A Hopeful Soul
164
votes
12 answers

Function Pointers in Java

This may be something common and trivial, but I seem to be having trouble finding a concrete answer. In C# there is a concept of delegates, which relates strongly to the idea of function pointers from C++. Is there a similar functionality in Java?…
dreadwail
  • 15,098
  • 21
  • 65
  • 96
1
2 3
99 100