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
50
votes
5 answers

How does a delegate work in objective-C?

Does anyone know where I can find a good explanation/tutorial of what and how an application delegate works in objective-C? The two books I have don't dwell on delegates enough and do not explain them very well for me to truly understand their…
Josh Bradley
  • 4,630
  • 13
  • 54
  • 79
49
votes
4 answers

Difference between Delegate.Invoke and Delegate()

delegate void DelegateTest(); DelegateTest delTest; Whats the difference between calling delTest.Invoke() and delTest()? Both would execute the delegate on the current thread, right?
remdao
  • 885
  • 3
  • 17
  • 23
48
votes
3 answers

What is the difference between new Action() and a lambda?

So when I write something like this Action action = new Action(()=>_myMessage = "hello"); Refactor Pro! Highlights this as a redundant delegate creation and allows me to to shorten it to Action action = () => _myMessage="hello"; And this usually…
George Mauer
  • 117,483
  • 131
  • 382
  • 612
48
votes
3 answers

What is meant by .delegate=self?

Could anyone explain the meaning of someViewController.delegate = self and self.delegate? Where do they help us?
Rajkanth
48
votes
3 answers

Swift delegation - when to use weak pointer on delegate

Can someone explain when and when not to use a 'weak' assignment to a delegate pointer in Swift, and why? My understanding is that if you use a protocol that is not defined as a class you cannot, nor want to, assign your delegate pointer to…
47
votes
9 answers

Return a value from an Event -- is there a Good Practice for this?

I'm doing a small multi-threaded app that uses asynchronous TCP sockets, but I will get to the point: I'm using a custom event to read a value from a form and the delegate used by the event returns a string when finished. My question here is: is…
Hugo
  • 6,244
  • 8
  • 37
  • 43
46
votes
5 answers

Is using Action.Invoke considered best practice?

If I have the below code, should I just call the Action or should it call Action.Invoke? public class ClassA { public event Action OnAdd; private void SomethingHappened() { if (OnAdd != null) OnAdd("It Happened"); //Should it…
Jon
  • 38,814
  • 81
  • 233
  • 382
46
votes
5 answers

C# - using List.Find() with custom objects

I'm trying to use a List with a custom class of mine, and being able to use methods like Contains(), Find(), etc., on the list. I thought I'd just have to overload the operator == but apparently, one way of doing that is to use a delegate method…
Pacane
  • 20,273
  • 18
  • 60
  • 97
46
votes
5 answers

Meaning of () => Operator in C#, if it exists

I read this interesting line here, in an answer by Jon Skeet. The interesting line is this, where he advocated using a delegate: Log.Info("I did something: {0}", () => action.GenerateDescription()); Question is, what is this ()=> operator, I…
Orca
  • 2,035
  • 4
  • 24
  • 39
46
votes
5 answers

Verifying a delegate was called with Moq

i got a class that gets by argument a delegate. This class invokes that delegate, and i want to unit test it with Moq. how do i verify that this method was called ? example class : public delegate void Foo(int number); public class A { int a =…
Lironess
  • 823
  • 1
  • 11
  • 19
45
votes
3 answers

How to create a delegate from a MethodInfo when method signature cannot be known beforehand?

I need a method that takes a MethodInfo instance representing a non-generic static method with arbitrary signature and returns a delegate bound to that method that could later be invoked using Delegate.DynamicInvoke method. My first naïve try looked…
Zakharia Stanley
  • 1,196
  • 1
  • 9
  • 10
44
votes
6 answers

Delegate vs. delegate keyword

If you like to create custom delegates you would use the delegate keyword in lowercase. What can you do with the actual Delegate Class? What is this good for? I don't understand the exact difference.
Houman
  • 64,245
  • 87
  • 278
  • 460
44
votes
5 answers

What is the lifetime of a delegate created by a lambda in C#?

Lambdas are nice, as they offer brevity and locality and an extra form of encapsulation. Instead of having to write functions which are only used once you can use a lambda. While wondering how they worked, I intuitively figured they are probably…
Steven Jeuris
  • 18,274
  • 9
  • 70
  • 161
44
votes
6 answers

What's the difference between data source and delegate?

I have a fundamental question related to Cocoa frameworks design patterns. What's the difference between delegate and data source? Both of them could use @protocols declaration, but some classes or frameworks are using delegate, and some others are…
Jesse Armand
  • 1,842
  • 3
  • 17
  • 26
43
votes
4 answers

How do you declare a Predicate Delegate inline?

So I have an object which has some fields, doesn't really matter what. I have a generic list of these objects. List myObjects = new List(); myObjects.Add(myObject1); myObjects.Add(myObject2); myObjects.Add(myObject3); So I want…
Curtis
  • 1,189
  • 2
  • 11
  • 22