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
43
votes
10 answers

Non-retaining array for delegates

In a Cocoa Touch project, I need a specific class to have not only a single delegate object, but many of them. It looks like I should create an NSArray for these delegates; the problem is that NSArray would have all these delegates retained, which…
wh1t3cat1k
  • 3,146
  • 6
  • 32
  • 38
43
votes
2 answers

How do I describe an Action delegate that returns a value (non-void)?

The Action delegate return void. Is there any other built-in delegate which returns non void value?
user496949
  • 83,087
  • 147
  • 309
  • 426
43
votes
16 answers

UISearchBar x button pressed

How handle the event when press the 'x' button? I try this method but not works. -(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{ }
Fabio
  • 1,913
  • 5
  • 29
  • 53
43
votes
2 answers

Getting a reference to the UIApplication delegate

I'm writing my first iPhone application and I'm having trouble switching views. I have 2 views and a reference to each in the AppDelegate (an instance of UIApplicationDelegate). I create instances of both in the applicationDidFinishLaunching and…
derGral
  • 1,836
  • 4
  • 19
  • 29
42
votes
4 answers

Creating a property setter delegate

I have created methods for converting a property lambda to a delegate: public static Delegate MakeGetter(Expression> propertyLambda) { var result = Expression.Lambda(propertyLambda.Body).Compile(); return result; } public static…
Jim C
  • 4,517
  • 7
  • 29
  • 33
42
votes
2 answers

How do C# Events work behind the scenes?

I'm using C#, .NET 3.5. I understand how to utilize events, how to declare them in my class, how to hook them from somewhere else, etc. A contrived example: public class MyList { private List m_Strings = new List(); public…
Matt
  • 41,216
  • 30
  • 109
  • 147
41
votes
2 answers

+= operator for Delegate

I know that the += operator will add a method to the invocation list maintained by the Delegate base object, for example using System; class Program { delegate void MyDelegate(int n); void Foo(int n) { Console.WriteLine("n =…
jensa
  • 2,792
  • 2
  • 21
  • 36
40
votes
1 answer

nil object in iOS8 delegate methods - custom keyboards

I'm building a custom keyboard and I'm implementing the following delegate methods in my InputViewController. But I always get _textInput = nil_ - (void)textWillChange:(id)textInput - (void)textDidChange:(id)textInput -…
nurnachman
  • 4,468
  • 2
  • 37
  • 40
40
votes
3 answers

Become UIScrollViewDelegate delegate for UITableView

I have a UITableView instance variable. I want to be able to register my view controller to be the UIScrollViewDelegate for my UITableViewController. I have already tried tableView.delegate = self; But when scrolling, my methods -…
Brian
  • 3,571
  • 7
  • 44
  • 70
40
votes
2 answers

Can I have an Action<> or Func<> with an out param?

I have a method with an out parameter, and I'd like to point an Action or Func (or other kind of delegate) at it. This works fine: static void Func(int a, int b) { } Action action = Func; However this doesn't static void OutFunc(out int a,…
Orion Edwards
  • 121,657
  • 64
  • 239
  • 328
40
votes
7 answers

C# cannot convert method to non delegate type

I have a class called Pin. public class Pin { private string title; public Pin() { } public setTitle(string title) { this.title = title; } public String getTitle() { return title; } } From another class…
gts13
  • 1,048
  • 1
  • 16
  • 29
40
votes
6 answers

How do I invoke an extension method using reflection?

I appreciate that similar questions have been asked before, but I am struggling to invoke the Linq Where method in the following code. I am looking to use reflection to dynamically call this method and also dynamically build the delegate (or lambda)…
Jon Simpson
  • 993
  • 2
  • 8
  • 17
39
votes
7 answers

Why are delegates reference types?

Quick note on the accepted answer: I disagree with a small part of Jeffrey's answer, namely the point that since Delegate had to be a reference type, it follows that all delegates are reference types. (It simply isn't true that a multi-level…
Dan Tao
  • 125,917
  • 54
  • 300
  • 447
39
votes
2 answers

Setting delegate for UIImagePicker returns error

Having a problem in some swift code I had written for an OCR translation app. The code snippet is below: @IBAction func btnOCR(sender: AnyObject) { var languageAlert = UIAlertController(title: "For Your Information...", message: "The OCR…
Jacob King
  • 6,025
  • 4
  • 27
  • 45
39
votes
2 answers

Removing event handlers

Is this: Button.Click -= new EventHandler(Button_Click); the same as this: Button.Click -= Button_Click; I ask because to me it seems that the former is removing a new reference to a method, and the latter one is removing a method itself. But then…
Carlo
  • 25,602
  • 32
  • 128
  • 176