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

Why does trying to understand delegates feel like trying to understand the nature of the universe?

I've read two books, tons of examples. They still make next to no sense to me. I could probably write some code that uses delegates, but I have no idea why. Am I the only one with this problem, or am I just an idiot? If anyone can actually explain…
Kin
  • 1,407
  • 20
  • 24
58
votes
1 answer

Difference between @Delegate, @Mixin and Traits in Groovy?

Would someone explain when I would want to use Groovy Traits vs. Mixins (@Mixin) vs. Delegates (@Delegate)? Maybe some trade-offs and design concerns would help. They all seem to allow for reusing multiple "classes" of behavior. Thanks. :-) This SO…
Vahid Pazirandeh
  • 1,552
  • 3
  • 13
  • 29
57
votes
6 answers

About "Declaration is only valid at file scope"

I have a class and extension Swift file. After adding a delegate that I declared in another file to the class, Xcode shows this error Declaration is only valid at file scope at the extension line. I don't know what the problem is. Can anyone help…
Dennis
  • 709
  • 1
  • 6
  • 9
56
votes
7 answers

Is EndInvoke() optional, sort-of optional, or definitely not optional?

I've read conflicting opinions as to whether every BeginInvoke() has to be matched by an EndInvoke(). Are there any leaks or other problems associated with NOT calling EndInvoke()?
endian
  • 4,234
  • 8
  • 34
  • 42
56
votes
8 answers

Cast delegate to Func in C#

I have code: public delegate int SomeDelegate(int p); public static int Inc(int p) { return p + 1; } I can cast Inc to SomeDelegate or Func: SomeDelegate a = Inc; Func b = Inc; but I can't cast Inc to SomeDelegate and…
AndreyAkinshin
  • 18,603
  • 29
  • 96
  • 155
55
votes
4 answers

Is it possible to have a delegate as attribute parameter?

Is it possible to have a delegate as the parameter of an attribute? Like this: public delegate IPropertySet ConnectionPropertiesDelegate(); public static class TestDelegate { public static IPropertySet GetConnection() { return new…
George Silva
  • 3,454
  • 10
  • 39
  • 64
55
votes
2 answers

Checking to see if an optional protocol method has been implemented

Does anyone know the best way to check to see if an optional protocol method has been implemented. I tried this: if ([self.delegate respondsToSelector:@selector(optionalProtocolMethod:)] ) where delegate is: id delegate; However, I get…
Nick Cartwright
  • 8,334
  • 15
  • 45
  • 56
54
votes
1 answer

Why are there memory allocations when calling a func

I have the following program which construct a local Func from two static methods. But strangely, when I profile the program, it allocated close to a million Func objects. Why invoking Func object is also creating Func instances? public static…
Xiaoguo Ge
  • 2,177
  • 20
  • 26
54
votes
2 answers

Cannot convert from 'method group' to 'System.Action' error
I have created the following function: public void DelegatedCall(Action delegatedMethod) And defined the following method public void foo1(String str) { } However, when I try to call DelegateCall with foo1: DelegatedCall(foo1); ...I get…
Jonathan
  • 543
  • 1
  • 4
  • 5
53
votes
5 answers

Can I use params in Action or Func delegates?

When I'm trying to use params in an Action delegate... private Action WriteToLogCallBack; I received this design time error: Invalid token 'params' in class, struct, or interface member declaration Any help!
Homam
  • 23,263
  • 32
  • 111
  • 187
52
votes
4 answers

How to use custom delegates in Objective-C

I need to know about the usage of delegate methods in Objective-C. Can anyone point me to the correct source?
Sreelal
  • 785
  • 3
  • 13
  • 15
51
votes
3 answers

Getting a delegate from methodinfo

I have a drop down list that is populated by inspecting a class's methods and including those that match a specific signature. The problem is in taking the selected item from the list and getting the delegate to call that method in the class. The…
Ty.
  • 3,888
  • 3
  • 24
  • 31
51
votes
12 answers

How do i exit a List.ForEach loop when using an anonymous delegate?

In a normal loop you can break out of a loop using break. Can the same be done using an anonymous delegate? Example inputString and result are both declared outside the delegate. blackList.ForEach(new Action( delegate(string item) …
SecretDeveloper
  • 3,140
  • 2
  • 31
  • 36
51
votes
8 answers

How to create an asynchronous method

I have simple method in my C# app, it picks file from FTP server and parses it and stores the data in DB. I want it to be asynchronous, so that user perform other operations on App, once parsing is done he has to get message stating "Parsing is…
Prashant Cholachagudda
  • 13,012
  • 23
  • 97
  • 162
50
votes
6 answers

Standard delegates in C#

There are some Delegates predefined in C# I know these: EventHandler // Default event callbacks EventHandler // Default event callbacks with custom parameter (inheriting from EventArgs) Action // Function without return value and without…
Tarion
  • 16,283
  • 13
  • 71
  • 107