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
31
votes
2 answers

Recommended way to declare delegate properties with ARC

I used to declare all delegate properties as @property (assign) id delegate; I was under the impression that all assign properties should now be weak pointers, is this correct? If I try to declare as: @property (weak) id
cfischer
  • 24,452
  • 37
  • 131
  • 214
31
votes
7 answers

Why can't I put a delegate in an interface?

Why can't I add a delegate to my interface?
user73936
  • 311
  • 1
  • 3
  • 3
31
votes
5 answers

Calling functions by array index in Python

I have a bunch of functions in Python out1, out2, out3 etc. and would like to call them based on an integer I pass in. def arryofPointersToFns (value): #call outn where n = value Is there an easy way to do this?
Dirk
  • 3,073
  • 4
  • 31
  • 36
31
votes
7 answers

Can I ignore delegate parameters with lambda syntax?

I am curious why C# allows me to ignore delegate parameters in some cases but not others. For instance this is permitted: Action action = delegate { Console.WriteLine("delegate"); }; but this is not: Action action = () =>…
Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
31
votes
3 answers

Operator '?' cannot be applied to operand of type 'T'

Trying to make Feature generic and then suddenly compiler said Operator '?' cannot be applied to operand of type 'T' Here is the code public abstract class Feature { public T Value { get { return GetValue?.Invoke(); } // here is…
Sinatr
  • 20,892
  • 15
  • 90
  • 319
31
votes
4 answers

how to extend a protocol for a delegate in objective C, then subclass an object to require a conforming delegate

I want to subclass UITextView, and send a new message to the delegate. So, I want to extend the delegate protocol. What's the correct way to do this? I started out with this: interface: #import @class…
fess .
  • 1,489
  • 1
  • 15
  • 13
30
votes
2 answers

QML: Component vs Item as a container

What is the difference between Component and Item in QML ? The documentation is not absolutely clear here. What is the preferred type to use as a container for several widgets? Can it be replacable by Rectangle? For example, what is the difference…
Dmitry
  • 1,912
  • 2
  • 18
  • 29
30
votes
12 answers

Get the current view controller from the app delegate

i am new to ios. I need to know the current view controller from app delegate.. i have no idea about this and i don't knowto implement this. i am using this code toimplemnt this but it return null values. I followed this link- Get current view…
user3459648
  • 351
  • 1
  • 3
  • 9
30
votes
4 answers

C# - ThreadPool QueueUserWorkItem Use?

Just right now I'm using following code to add queued threads. I don't like it. And my colleagues won't either because they don't know C# very well. All I want is of course to queue a method to be executed in a new thread. private static void…
Bitterblue
  • 13,162
  • 17
  • 86
  • 124
30
votes
6 answers

Equivalent of C# anonymous methods in Java?

In C# you can define delegates anonymously (even though they are nothing more than syntactic sugar). For example, I can do this: public string DoSomething(Func someDelegate) { // Do something involving someDelegate(string s) }…
Callum Rogers
  • 15,630
  • 17
  • 67
  • 90
30
votes
2 answers

Compiler generated incorrect code for anonymous methods [MS BUG FIXED]

See the following code: public abstract class Base { public virtual void Foo() where T : class { Console.WriteLine("base"); } } public class Derived : Base { public override void Foo() { …
Cheng Chen
  • 42,509
  • 16
  • 113
  • 174
30
votes
4 answers

Event and delegate contravariance in .NET 4.0 and C# 4.0

While investigating this question I got curious about how the new covariance/contravariance features in C# 4.0 will affect it. In Beta 1, C# seems to disagree with the CLR. Back in C# 3.0, if you had: public event EventHandler
Daniel Earwicker
  • 114,894
  • 38
  • 205
  • 284
29
votes
5 answers

The proper way of raising events in the .NET framework

Currently "Avoid checking for null event handlers" is at the top of the answers to the post titled Hidden Features of C# and it contains severely misleading information. While I understand that Stack Overflow is a "democracy" and the answer rose to…
Mike Dinescu
  • 54,171
  • 16
  • 118
  • 151
29
votes
3 answers

Handling app delegates and switching between views

I'm getting a warning about a semantic issue pertaining to passing a *const _strong to type id and cannot seem to fix it no matter what I change. I have two views at the moment, and have written this code. In iPadSpeckViewController.m, here is the…
Chris
  • 11,819
  • 19
  • 91
  • 145
29
votes
3 answers

Why is compilation OK, when I use Invoke method, and not OK when I return Func directly?

I don't understand this case: public delegate int test(int i); public test Success() { Func f = x => x; return f.Invoke; // <- code successfully compiled } public test Fail() { Func f = x => x; return f; // <-…
Evgeniy Terekhin
  • 596
  • 3
  • 10