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.

delegates
Filter by
Sorted by
Tagged with
1152 votes
15 answers
638k views

Can I call a constructor from another constructor (do constructor chaining) in C++?

As a C# developer I'm used to running through constructors: class Test { public Test() { DoSomething(); } public Test(int count) : this() { DoSomethingWithCount(count); ...
Stormenet's user avatar
  • 26.3k
1144 votes
12 answers
305k views

Why would you use Expression<Func<T>> rather than Func<T>?

I understand lambdas and the Func and Action delegates. But expressions stump me. In what circumstances would you use an Expression<Func<T>> rather than a plain old Func<T>?
Richard Nagle's user avatar
913 votes
13 answers
986k views

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 ...
user31673's user avatar
  • 13.5k
880 votes
3 answers
449k views

Func vs. Action vs. Predicate [duplicate]

With real examples and their use, can someone please help me understand: When do we need a Func<T, ..> delegate? When do we need an Action<T> delegate? When do we need a Predicate<T>...
InfoLearner's user avatar
  • 15.3k
774 votes
20 answers
385k views

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's user avatar
  • 15.2k
706 votes
7 answers
296k views

Func delegate with no return type

All of the Func<T> delegates return a value. What are the .NET delegates that can be used with methods that return void?
Marcel Lamothe's user avatar
602 votes
8 answers
138k views

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 ...
hnh's user avatar
  • 14.4k
430 votes
8 answers
620k views

When & why to use delegates? [duplicate]

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 ...
376 votes
11 answers
156k views

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's user avatar
256 votes
14 answers
90k views

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-...
Eric's user avatar
  • 4,261
224 votes
14 answers
302k views

Java Delegates?

Does the Java language have delegate features, similar to how C# has support for delegates?
markus's user avatar
  • 2,608
194 votes
6 answers
75k views

delegate keyword vs. lambda notation

Once it is compiled, is there a difference between: delegate { x = 0; } and () => { x = 0 } ?
MojoFilter's user avatar
  • 12.3k
178 votes
6 answers
204k views

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 ...
Dollarslice's user avatar
  • 10.1k
176 votes
4 answers
47k views

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 ...
user avatar
166 votes
13 answers
76k views

What is Delegate? [closed]

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 ...
Naveed's user avatar
  • 41.8k
165 votes
10 answers
40k views

Delegates: Predicate vs. Action vs. Func

Can someone provide a good explanation (hopefully with examples) of these 3 most important delegates: Predicate Action Func
user avatar
165 votes
4 answers
39k views

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 ...
user avatar
164 votes
12 answers
188k views

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 ...
dreadwail's user avatar
  • 15.2k
163 votes
1 answer
60k views

Difference Between Invoke and DynamicInvoke

What is the difference between Invoke and DynamicInvoke in delegates? Please give me some code example which explain difference between that two methods.
testCoder's user avatar
  • 7,285
156 votes
12 answers
147k views

How to hide the keyboard when I press return key in a UITextField?

Clicking in a textfield makes the keyboard appear. How do I hide it when the user presses the return key?
aden's user avatar
  • 1,907
153 votes
9 answers
49k views

Why can't an anonymous method be assigned to var?

I have the following code: Func<string, bool> comparer = delegate(string value) { return value != "0"; }; However, the following does not compile: var comparer = delegate(string value) { ...
Marlon's user avatar
  • 20.1k
152 votes
10 answers
129k views

How can I clear event subscriptions in C#?

Take the following C# class: c1 { event EventHandler someEvent; } If there are a lot of subscriptions to c1's someEvent event and I want to clear them all, what is the best way to achieve this? ...
programmer's user avatar
  • 4,372
145 votes
15 answers
189k views

Delegates in swift?

How does one go about making a delegate, i.e. NSUserNotificationCenterDelegate in swift?
user3718173's user avatar
  • 1,453
144 votes
9 answers
169k views

What is Func, how and when is it used

What is Func<> and what is it used for?
learning's user avatar
  • 11.5k
143 votes
8 answers
162k views

Super-simple example of C# observer/observable with delegates

I recently started digging into C# but I can't by my life figure out how delegates work when implementing the observer/observable pattern in the language. Could someone give me a super-simple example ...
Deniz Dogan's user avatar
139 votes
4 answers
135k views

How do I set up a simple delegate to communicate between two view controllers?

I have two UITableViewControllers and need to pass the value from the child view controller to the parent using a delegate. I know what delegates are and just wanted to see a simple to follow example. ...
Strong Like Bull's user avatar
134 votes
9 answers
123k views

Uses of Action delegate in C# [closed]

I was working with the Action Delegates in C# in the hope of learning more about them and thinking where they might be useful. Has anybody used the Action Delegate, and if so why? or could you give ...
Biswanath's user avatar
  • 9,115
133 votes
9 answers
71k views

Why must a lambda expression be cast when supplied as a plain Delegate parameter

Take the method System.Windows.Forms.Control.Invoke(Delegate method) Why does this give a compile time error: string str = "woop"; Invoke(() => this.Text = str); // Error: Cannot convert lambda ...
xyz's user avatar
  • 27.5k
131 votes
15 answers
50k views

Wrap a delegate in an IEqualityComparer

Several Linq.Enumerable functions take an IEqualityComparer<T>. Is there a convenient wrapper class that adapts a delegate(T,T)=>bool to implement IEqualityComparer<T>? It's easy enough ...
Marcelo Cantos's user avatar
127 votes
3 answers
34k views

"Delegate subtraction has unpredictable result" in ReSharper/C#?

When using myDelegate -= eventHandler ReSharper (version 6) issues: Delegate subtraction has unpredictable result The rational behind this is explained by JetBrains here. The explanation makes ...
user avatar
126 votes
7 answers
36k views

Are C# events synchronous?

There are two parts to this question: Does raising an event block the thread, or does it start execution of EventHandlers asynchronously and the thread goes continues on at the same time? Are the ...
Alexander Bird's user avatar
114 votes
9 answers
107k views

C# pattern to prevent an event handler hooked twice [duplicate]

Duplicate of: How to ensure an event is only subscribed to once and Has an event handler already been added? I have a singleton that provides some service and my classes hook into some events on it, ...
Ali Shafai's user avatar
  • 5,161
114 votes
22 answers
97k views

How do I know that the UICollectionView has been loaded completely?

I have to do some operation whenever UICollectionView has been loaded completely, i.e. at that time all the UICollectionView's datasource / layout methods should be called. How do I know that?? Is ...
Jirune's user avatar
  • 2,320
114 votes
6 answers
116k views

How to add a delegate to an interface C#

I need to have some delegates in my class. I'd like to use the interface to "remind" me to set these delegates. How to? My class look like this: public class ClsPictures : myInterface { // ...
Asaf's user avatar
  • 3,095
113 votes
4 answers
37k views

What is the difference between Func<string,string> and delegate?

I see delegates in two forms: A. Func<string, string> convertMethod = lambda B. public delegate string convertMethod(string value); I'm uncertain of what actually the difference between ...
Dietpixel's user avatar
  • 10.1k
112 votes
8 answers
50k views

Where do I use delegates? [closed]

What are some real world places that call for delegates? I'm curious what situations or patterns are present where this method is the best solution. No code required.
slipsec's user avatar
  • 3,022
111 votes
10 answers
80k views

Difference between events and delegates and its respective applications [closed]

I don't see advantages of using events over delegates, other than being syntactical sugar. Perhaps I am misunderstanding, but it seems that event is just a placeholder for delegate. Would you explain ...
user avatar
108 votes
9 answers
327k views

Invoke(Delegate)

Can anybody please explain this statement written on this link Invoke(Delegate): Executes the specified delegate on the thread that owns the control's underlying window handle. Can anybody explain ...
user1903439's user avatar
  • 1,981
104 votes
20 answers
43k views

When would you use delegates in C#? [closed]

What are your usage of delegates in C#?
Maxime Rouiller's user avatar
104 votes
4 answers
29k views

Compiler Ambiguous invocation error - anonymous method and method group with Func<> or Action

I have a scenario where I want to use method group syntax rather than anonymous methods (or lambda syntax) for calling a function. The function has two overloads, one that takes an Action, the other ...
Richard Ev's user avatar
  • 53.6k
98 votes
10 answers
20k views

Wrapping StopWatch timing with a delegate or lambda?

I'm writing code like this, doing a little quick and dirty timing: var sw = new Stopwatch(); sw.Start(); for (int i = 0; i < 1000; i++) { b = DoStuff(s); } sw.Stop(); Console.WriteLine(sw....
Jeff Atwood's user avatar
  • 63.7k
95 votes
8 answers
45k views

Proper naming convention for a .NET Delegate type? [closed]

By convention classes are often named like nouns, methods like verbs and interfaces like adjectives. What is the common naming convention for a delegate? Or what's a good way to differentiate its ...
John K's user avatar
  • 28.6k
90 votes
1 answer
21k views

What's the difference between 'weak' and 'assign' in delegate property declaration

Whats the difference between this: @property (nonatomic, weak) id <SubClassDelegate> delegate; and this: @property (nonatomic, assign) id <SubClassDelegate> delegate; I want to use ...
Firdous's user avatar
  • 4,642
89 votes
7 answers
98k views

Assigning to 'id<Delegate>' from incompatible type 'ViewController *const_strong'

Throughout my app, I'm getting semantic issue warnings when I set ViewController.delegate = self. I have searched and found similar posts but none were able to solve my problem. ViewController.m: ...
David L's user avatar
  • 4,357
87 votes
4 answers
87k views

What's the method signature for passing an async delegate?

I've recently moved back to C# from being in Objective-C land, and the async/await keywords in C# 5 look cool. But I'm still trying to get a handle on the proper syntax. I want to declare a method ...
AndrewS's user avatar
  • 8,334
85 votes
4 answers
68k views

Performance of calling delegates vs methods

Following this question - Pass Method as Parameter using C# and some of my personal experience I'd like to know a little more about the performance of calling a delegate vs just calling a method in C#....
Paolo's user avatar
  • 22.4k
85 votes
9 answers
13k views

Is there a downside to adding an anonymous empty delegate on event declaration?

I have seen a few mentions of this idiom (including on SO): // Deliberately empty subscriber public event EventHandler AskQuestion = delegate {}; The upside is clear - it avoids the need to check ...
serg10's user avatar
  • 32.4k
85 votes
8 answers
31k views

C# Generics won't allow Delegate Type Constraints

Is it possible to define a class in C# such that class GenericCollection<T> : SomeBaseCollection<T> where T : Delegate I couldn't for the life of me accomplish this last night in .NET 3....
Nicholas Mancuso's user avatar
81 votes
8 answers
19k views

Creating delegates manually vs using Action/Func delegates

Today I was thinking about declaring this: private delegate double ChangeListAction(string param1, int number); but why not use this: private Func<string, int, double> ChangeListAction; or ...
Elisabeth's user avatar
  • 20.9k
79 votes
7 answers
100k views

Events - naming convention and style

I'm learning about Events / Delegates in C#. Could I ask your opinion on the naming/coding style I've chosen (taken from the Head First C# book)? Am teaching a friend about this tomorrow, and am ...
Dave Mateer's user avatar
  • 6,708

1
2 3 4 5
228