Questions tagged [delegates]

47 questions
38
votes
10 answers

Why do heavily object-oriented languages avoid having functions as a primitive type?

As has been covered to the point of parody, heavily object-oriented languages, such as C# or Java, tend to lack the feature of having functions as a primitive type. You can argue about whether or not their functions are first class, but the pattern…
28
votes
7 answers

Why would I ever use delegates if I'm not doing events?

I am trying to learn how to do events in C#, and according to the MSDN Events tutorial, Events are declared using delegates. If you have not yet studied the Delegates Tutorial, you should do so before continuing so now I'm trying to understand…
shieldfoss
  • 663
  • 1
  • 6
  • 13
18
votes
5 answers

Using Func instead of interfaces for IoC

Context: I am using C# I designed a class, and in order to isolate it, and make unit testing easier, I am passing in all its dependencies; it does no object instantiation internally. However, instead of referencing interfaces to get the data it…
TheCatWhisperer
  • 5,231
  • 1
  • 22
  • 41
17
votes
10 answers

Real world use of C# Delegates

I think that I conceptually understand C# delegates, however, I am struggling to find a real world example where they would be useful. Can you provide some answers detailing how C# delegates were used in real applications and what problems they…
AlexC
  • 1,337
  • 1
  • 11
  • 18
13
votes
3 answers

Why aren't properties implicitly convertible to delegates

We all know that properties in C# get compiled to actual plain old methods. But unlike method(-group)s, they can't be given as arguments to other methods that expect a delegate like a Func or Action (getter and setter). Is there anything…
sara
  • 2,549
  • 15
  • 23
11
votes
2 answers

What is Delegation and why is it important in iOS programming?

At the moment I am teaching myself iOS programming, and one concept I find really hard to wrap my head around is delegation. What is it? Why and how is it used? What is the advantage? The technical writing from the book I'm reading makes it hard to…
TestinginProd
  • 262
  • 1
  • 2
  • 10
10
votes
5 answers

What are the advantages of the delegate pattern over the observer pattern?

In the delegate pattern, only one object can directly listen to another object's events. In the observer pattern, any number of objects can listen to a particular object's events. When designing a class that needs to notify other object(s) of…
JoJo
  • 1,475
  • 2
  • 14
  • 21
9
votes
4 answers

C# Delegates are immutable - but why does that matter?

This is a followup question to this other question. Background Working from the MSDN Delegates Tutorial (C#), I see the following: Note that once a delegate is created, the method it is associated with never changes — delegate objects are…
shieldfoss
  • 663
  • 1
  • 6
  • 13
7
votes
4 answers

Can you overuse delegates and events

I've just started getting to grips with event driven programming and I'm finding delegates and events to be very useful. Since starting to see the potential I've began using them all the time to solve problems such as updating displays between…
rainhamtown
  • 403
  • 1
  • 3
  • 8
7
votes
2 answers

MVC and delegation

I am a beginning iOS programmer and use the Model-View-Controller model as a design pattern: my model doesn't know anything about my view (in order to make it compatible with any view), my view doesn't know anything about my model so they interact…
Tim Vermeulen
  • 173
  • 1
  • 5
6
votes
3 answers

Differences between "first-class function" mechanisms

Some languages (Javascript, Python) have the notion that a function is an object: //Javascript var fn = console.log; This means that functions can be treated like any other object (first-class functions), e.g. passed in as an argument to another…
Zev Spitz
  • 693
  • 5
  • 18
6
votes
2 answers

Delegates and events

What is the difference between public delegate void SecondChangedHandler(Object clock, TimeInfoEventArgs ti); public event SecondChangedHandler OnSecondChanged; and public event EventHandler OnSecondChanged; When should you use…
user47646
  • 63
  • 2
5
votes
6 answers

Do delegates defy OOP

I'm trying to understand OOP so I can write better OOP code and one thing which keeps coming up is this concept of a delegate (using .NET). I could have an object, which is totally self contained (encapsulated); it knows nothing of the outside…
Dave
  • 642
  • 1
  • 8
  • 17
5
votes
1 answer

Need to process 2 million 100k messages per second and route them to a particular event, delegate or multicast delegate

I need to process 2 million messages per second (perhaps in a scale out configuration) and route each message to a N delegates or multicast delegates. Question How should I structure my application in C# so that I can achieve the best performance…
makerofthings7
  • 6,038
  • 4
  • 39
  • 77
4
votes
2 answers

Is there an Ideal way of storing namespace-related elements?

In the title, with namespace-related elements, I refer to Enums, Delegates and other elements that do not belong to a single class, but to the whole namespace, or application. I know that I can cram these into the namespace just about anywhere, for…
oneManArmin
  • 181
  • 6
1
2 3 4