tags:

views:

16

answers:

2

Which is a better approach to run operations

  1. Use Delegate
  2. Use Action
  3. Use Predicate
  4. Use Func

Which once is the best in terms of performance, memory and code maintainability.

A: 

As with most things programming, it depends. Are you trying to run async operations? Multithreaded operations? Are you dealing with events? Your name 'WPF User' suggests that you're using .NET.

RQDQ
I would like to run a piece of code over a delegate. The options I have is Action, Delegate, Predicate and Func.This operation may run asynchronously. Which is the best option to chose?I'm into .net using C# prgramming.
WPF User
+1  A: 

Probably not what you want to hear but,

It all depends.

Using Predicate<> is a good idea in the specific applications that it is fit for (but it is also the same as Func<T, bool>).

If you can use Func<> (or its return-less cousin Action<>) then go for it. It's always better to re-use what is already there rather than re-invent the wheel.

If all else fails, fall back on delegate. There's nothing wrong with it and it still works great.

I don't think you're going to find that any one of those consistently performs any better in terms of speed or memory consumption since their performance is going to be dictated by what code you're running inside them.

Just pick what works for your needs and move on. If there's a performance issue at some point down the road...worry about it then. Code first, optimize later.

Justin Niessner