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
70
votes
6 answers

NSNotificationCenter vs delegation( using protocols )?

What are the pros and cons of each of them? Where should I use them specifically?
EEE
  • 4,536
  • 3
  • 28
  • 34
69
votes
10 answers

What is a "delegate" in Objective C's iPhone development?

What is a "delegate" in Objective C's iPhone development?
MikeN
  • 45,039
  • 49
  • 151
  • 227
68
votes
6 answers

Delegates vs Interfaces in C#

I would like to pose this question as long as I am trying currently to dig into the use and the purpose of delegates, although it is likely to have been asked in similar formulations. I know that delegates serve as function pointers used in C++. The…
arjacsoh
  • 8,932
  • 28
  • 106
  • 166
67
votes
9 answers

a constructor as a delegate - is it possible in C#?

I have a class like below: class Foo { public Foo(int x) { ... } } and I need to pass to a certain method a delegate like this: delegate Foo FooGenerator(int x); Is it possible to pass the constructor directly as a FooGenerator value, without…
akavel
  • 4,789
  • 1
  • 35
  • 66
67
votes
7 answers

MethodInvoker vs Action for Control.BeginInvoke

Which is more correct and why? Control.BeginInvoke(new Action(DoSomething), null); private void DoSomething() { MessageBox.Show("What a great post"); } or Control.BeginInvoke((MethodInvoker) delegate { MessageBox.Show("What a great…
Mike_G
  • 16,237
  • 14
  • 70
  • 101
66
votes
9 answers

Eclipse gets stuck when trying to launch Android app

I'm trying to run helloandroid application on a Motorola Milestone A853. I typed "adb devices" and the mobile is properly recognized. However, when I try to run the application Eclipse always stuck at 27% "Launching delegate". Which could be the…
Jupiter Jones
  • 863
  • 1
  • 7
  • 12
63
votes
8 answers

Action vs delegate event

I have seen developers using the below codes quite alternatively. What is the exact difference between these, and which ones go by the standard? Are they same, as Action and Func is a delegate as well: public event Action
Bhaskar
  • 10,537
  • 6
  • 53
  • 64
62
votes
1 answer

Can I get the signature of a C# delegate by its type?

Is there a straightforward way using reflection to get at the parameter list for a delegate if you have its type information? For an example, if I declare a delegate type as follows delegate double FooDelegate (string param, bool condition); and…
fastcall
  • 2,564
  • 4
  • 20
  • 21
60
votes
5 answers

Are delegates and callbacks the same or similar?

Are delegates the same thing as callbacks? Or are they related somehow?
ASDFQWERTY
59
votes
7 answers

Difference between protocol and delegates?

What is the difference between a protocol and a delegate? and, How can we declare variables in a protocol class?
er.mobileapp
  • 1,287
  • 4
  • 15
  • 21
59
votes
5 answers

List.RemoveAll - How to create an appropriate Predicate
This is a bit of noob question - I'm still fairly new to C# and generics and completely new to predicates, delegates and lambda expressions... I have a class 'Enquiries' which contains a generic list of another class called 'Vehicles'. I'm building…
CJM
  • 11,908
  • 20
  • 77
  • 115
59
votes
4 answers

Cannot assign a value of type ViewController to a value of type UITextFieldDelegate?

Here's the error when I wrote the line self.MessageTextField.delegate = self: /ChatApp/ViewController.swift:27:42: Cannot assign a value of type 'ViewController' to a value of type 'UITextFieldDelegate?' Here's my Swift code…
David
  • 1,660
  • 3
  • 21
  • 33
59
votes
5 answers

Cannot convert lambda expression to type 'object' because it is not a delegate type

I have a base class that has a bool property which looks like this: public abstract class MyBaseClass { public bool InProgress { get; protected set; } } I am inheriting it another class from it and trying to add InProgress as a delegate to the…
Asdfg
  • 11,362
  • 24
  • 98
  • 175
58
votes
8 answers

Why do we need C# delegates

I never seem to understand why we need delegates? I know they are immutable reference types that hold reference of a method but why can't we just call the method directly, instead of calling it via a delegate? Thanks
InfoLearner
  • 14,952
  • 20
  • 76
  • 124
58
votes
13 answers

Delegates, Why?

Possible Duplicates: When would you use delegates in C#? The purpose of delegates I have seen many question regarding the use of delegates. I am still not clear where and WHY would you use delegates instead of calling the method directly. I have…
Mage
  • 969
  • 2
  • 13
  • 23