Stop creating custom delegate types


Note to OSS and framework developers:

Please stop creating custom delegate types.  Use the Action and Func delegates instead.

The problem is that delegate types with the same signature are not convertible to each other.  For example, none of these assignments will work:

public delegate bool CustomMatchingFunction(string value);

public void Custom_delegates_are_bad()
{
    Predicate<string> match1 = value => value == "Blarg";
    Func<string, bool> match2 = value => value == "Blarg";
    CustomMatchingFunction match3 = value => value == "Blarg";

    match1 = match2;
    match1 = (Predicate<string>)match2;
    match1 = match3;
}

Although all of the delegate types shown have the same signature, this does not mean that they're the same type.  They're neither implicitly nor explicitly convertible from each other.  One of the preliminary LINQ framework design guidelines states:

Do use the new LINQ types "Func<>" and "Expression<>" instead of custom delegates and predicates, when defining new APIs.

There are Action delegates for void methods and Func delegates for methods that return values.  If something needs a delegate, use either the generic or specific versions of these delegate types.  Instead of "Predicate<T>", use "Func<T, bool>".  Instead of creating a custom void delegate, use "Action".

By creating custom delegate types, you're forcing people using your API to either force your custom type into their code, or force them to use a bunch of "wrapper methods" that wrap, convert and call your custom delegate type.

If you declare even a single delegate type in your code (and you're using .NET 3.5), stop and make sure there isn't already an Action or Func delegate that works for you.


Posted Mar 26 2008, 10:03 AM by bogardj
Filed under:

Comments

Peter Ritchie wrote re: Stop creating custom delegate types
on 03-26-2008 10:40 AM

...after you upgrade to Visual Studio 2008...

Joe Ocampo wrote re: Stop creating custom delegate types
on 03-26-2008 5:34 PM

Nice. I am surprised R#4 doesn't catch this, in its hints.

Jarod wrote re: Stop creating custom delegate types
on 03-26-2008 6:32 PM

It is a personal goal to never type the word 'delegate' in an IDE again :)

Func<>/Action<T> is kind of addicting though, they are so easy I find myself wanting to use them were I would have not considered a delegate in the past.

bogardj wrote re: Stop creating custom delegate types
on 03-26-2008 7:33 PM

@Peter

Yeah yeah...let's just pretend that everyone upgrades to .NET 3.5 immediately.

I wanted to get some comments back from the FDG folks to see what they might recommend for .NET 2.0 situations...but these custom delegates can be a real pain...

Ray Houston wrote A Simple Closure To Handle Try/Catch Around Transactions
on 03-27-2008 8:35 PM

If you&#39;re like me, you&#39;re lazy and hate putting try/catch around your transaction handling in

Reflective Perspective - Chris Alcock » The Morning Brew #61 wrote Reflective Perspective - Chris Alcock &raquo; The Morning Brew #61
on 03-28-2008 3:23 AM

Pingback from  Reflective Perspective - Chris Alcock  &raquo; The Morning Brew #61

Jon Skeet wrote re: Stop creating custom delegate types
on 03-28-2008 3:31 AM

It's not quite as bad as you're making it out to be. Yes, there's no direct conversion - but you don't need to write any extra methods to convert from one to another. The following assignments both work:

match1 = new Predicate<string>(match2);

match1 = new Predicate<string>(match3);

No wrapper methods required.

That said, I do agree that it's best not to create your own delegate types by and large.

Jon

DotNetKicks.com wrote Stop creating custom delegate types!
on 03-28-2008 9:38 AM

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Bart Czernicki wrote re: Stop creating custom delegate types
on 03-28-2008 3:34 PM

Uhh...no.  2 reasons:

- backwards compatibility:

I could list you a few examples why u still would want custom delegate types, but say ur maintaining an API that has to work with 2.0 code...why exactly would u start using Func/Action??

Same goes for automatic properties, collection initializers, implicit anon types...all the "syntactic sugar" for C# 3.0

- standards:

The achitect should define what the best practices are and code standards are.  If you ur standards are to write code a specific way and then u have developers auto update their code to what they see on a blog, ur going to have bigger problems

Justin Etheredge wrote re: Stop creating custom delegate types
on 03-28-2008 3:55 PM

Here here, that is an excellent suggestion for those working on a completely 3.5 codebase.

bogardj wrote re: Stop creating custom delegate types
on 03-28-2008 8:23 PM

@Jon

Hooray!  I knew someone would find a way for it to work easily.  Thanks!

bogardj wrote re: Stop creating custom delegate types
on 03-28-2008 8:24 PM

@Jon

Hooray!  I knew someone would find a way for it to work easily.  Thanks!

bogardj wrote re: Stop creating custom delegate types
on 03-28-2008 8:26 PM

@Bart

Many of the C# 3.0 features work just fine with .NET 2.0.  C# 3.0 still compiles to the CLR 2.0.  It's just not as nice :)

Yes, this only applies to .NET 3.5.  It's still annoying creating these custom types, especially when you have APIs that expose properties and methods that are designed to work with Action/Func and something has to translate.

Bart Czernicki wrote re: Stop creating custom delegate types
on 03-28-2008 9:00 PM

@bogardj

"many" is the keyword here.  

I completely agree that Func/Action provide u enough overrides to not have to create a delegate.  Even if, you have a method that takes 8 parameters u should be passing in an object instead probably and using a Func/Action

However, with having a mixed dev environment 2005/2008 (which we are transitioning from) it would cause MAJOR coding horrors if we had developers go on blogs and simply apply what a few people agree is a good practice.

bogardj wrote re: Stop creating custom delegate types
on 03-31-2008 7:17 AM

@Bart

If you have developers applying wholesale advice from what they read on blogs, I think your problem is in the personnel/training department.

This isn't just my advice, it comes from Microsoft's Framework Design Guidelines folks as well.  It applies only to .NET 3.5 environments.

Frank Quednau wrote re: Stop creating custom delegate types
on 04-10-2008 3:53 AM

Recently I encountered one issue with generic delegates: They cannot be marshalled to static external functions imported from some dll. In this case you will have to define your own non-generic delegate even though you may have a fitting one.

ways to fold money wrote re: Stop creating custom delegate types
on 01-13-2009 6:07 AM

comment2, <a href="www.geocities.com/.../money-mart-ottawa.html"">www.geocities.com/.../money-mart-ottawa.html">money">www.geocities.com/.../money-mart-ottawa.html">money mart ottawa</a>, [url="www.geocities.com/.../money-mart-ottawa.html"">www.geocities.com/.../money-mart-ottawa.html"]money mart ottawa[/url], www.geocities.com/.../money-mart-ottawa.html money mart ottawa,  >:(, <a href="www.geocities.com/.../travelers-money-exchange.html"">www.geocities.com/.../travelers-money-exchange.html">travelers">www.geocities.com/.../travelers-money-exchange.html">travelers money exchange</a>, [url="www.geocities.com/.../travelers-money-exchange.html"">www.geocities.com/.../travelers-money-exchange.html"]travelers money exchange[/url], www.geocities.com/.../travelers-money-exchange.html travelers money exchange,  09125, <a href="www.geocities.com/.../past-money-worth-calculator.html"">www.geocities.com/.../past-money-worth-calculator.html">past">www.geocities.com/.../past-money-worth-calculator.html">past money worth calculator</a>, [url="www.geocities.com/.../past-money-worth-calculator.html"">www.geocities.com/.../past-money-worth-calculator.html"]past money worth calculator[/url], www.geocities.com/.../past-money-worth-calculator.html past money worth calculator,  wevgwk, <a href="www.geocities.com/.../where-to-buy-a-money-gram.html"">www.geocities.com/.../where-to-buy-a-money-gram.html">where">www.geocities.com/.../where-to-buy-a-money-gram.html">where to buy a money gram</a>, [url="www.geocities.com/.../where-to-buy-a-money-gram.html"">www.geocities.com/.../where-to-buy-a-money-gram.html"]where to buy a money gram[/url], www.geocities.com/.../where-to-buy-a-money-gram.html where to buy a money gram,  ddmu, <a href="www.geocities.com/.../money-instructor-check-register.html"">www.geocities.com/.../money-instructor-check-register.html">money">www.geocities.com/.../money-instructor-check-register.html">money instructor-check register</a>, [url="www.geocities.com/.../money-instructor-check-register.html"">www.geocities.com/.../money-instructor-check-register.html"]money instructor-check register[/url], www.geocities.com/.../money-instructor-check-register.html money instructor-check register,  293, <a href="www.geocities.com/.../real-money-trees.html"">www.geocities.com/.../real-money-trees.html">real">www.geocities.com/.../real-money-trees.html">real money trees</a>, [url="www.geocities.com/.../real-money-trees.html"">www.geocities.com/.../real-money-trees.html"]real money trees[/url], www.geocities.com/.../real-money-trees.html real money trees,  373776, <a href="www.geocities.com/.../is-hummel-worth-money.html"">www.geocities.com/.../is-hummel-worth-money.html">is">www.geocities.com/.../is-hummel-worth-money.html">is hummel worth money</a>, [url="www.geocities.com/.../is-hummel-worth-money.html"">www.geocities.com/.../is-hummel-worth-money.html"]is hummel worth money[/url], www.geocities.com/.../is-hummel-worth-money.html is hummel worth money,  5994, <a href="www.geocities.com/.../take-others-money-legally.html"">www.geocities.com/.../take-others-money-legally.html">take">www.geocities.com/.../take-others-money-legally.html">take others money legally</a>, [url="www.geocities.com/.../take-others-money-legally.html"">www.geocities.com/.../take-others-money-legally.html"]take others money legally[/url], www.geocities.com/.../take-others-money-legally.html take others money legally,  yixinx, <a href="www.geocities.com/.../phila-area-bank-money-market-rates.html"">www.geocities.com/.../phila-area-bank-money-market-rates.html">phila">www.geocities.com/.../phila-area-bank-money-market-rates.html">phila area bank money market rates</a>, [url="www.geocities.com/.../phila-area-bank-money-market-rates.html"">www.geocities.com/.../phila-area-bank-money-market-rates.html"]phila area bank money market rates[/url], www.geocities.com/.../phila-area-bank-money-market-rates.html phila area bank money market rates,  yuclkr, <a href="www.geocities.com/.../college-money-for-school-boxing-romangreico.html"">www.geocities.com/.../college-money-for-school-boxing-romangreico.html">college">www.geocities.com/.../college-money-for-school-boxing-romangreico.html">college money for school boxing romangreico</a>, [url="www.geocities.com/.../college-money-for-school-boxing-romangreico.html"">www.geocities.com/.../college-money-for-school-boxing-romangreico.html"]college money for school boxing romangreico[/url], www.geocities.com/.../college-money-for-school-boxing-romangreico.html college money for school boxing romangreico,  bibnn, <a href="www.geocities.com/.../money-for-bills-in-west-virginia.html"">www.geocities.com/.../money-for-bills-in-west-virginia.html">money">www.geocities.com/.../money-for-bills-in-west-virginia.html">money for bills in west virginia</a>, [url="www.geocities.com/.../money-for-bills-in-west-virginia.html"">www.geocities.com/.../money-for-bills-in-west-virginia.html"]money for bills in west virginia[/url], www.geocities.com/.../money-for-bills-in-west-virginia.html money for bills in west virginia,  =-]], <a href="www.geocities.com/.../make-money-by-filling-out-surveys.html"">www.geocities.com/.../make-money-by-filling-out-surveys.html">make">www.geocities.com/.../make-money-by-filling-out-surveys.html">make money by filling out surveys</a>, [url="www.geocities.com/.../make-money-by-filling-out-surveys.html"">www.geocities.com/.../make-money-by-filling-out-surveys.html"]make money by filling out surveys[/url], www.geocities.com/.../make-money-by-filling-out-surveys.html make money by filling out surveys,  7624, <a href="www.geocities.com/.../worth-of-paper-money.html"">www.geocities.com/.../worth-of-paper-money.html">worth">www.geocities.com/.../worth-of-paper-money.html">worth of paper money</a>, [url="www.geocities.com/.../worth-of-paper-money.html"">www.geocities.com/.../worth-of-paper-money.html"]worth of paper money[/url], www.geocities.com/.../worth-of-paper-money.html worth of paper money,  etbuv, <a href="www.geocities.com/.../the-masters-money.html"">www.geocities.com/.../the-masters-money.html">the">www.geocities.com/.../the-masters-money.html">the masters money</a>, [url="www.geocities.com/.../the-masters-money.html"">www.geocities.com/.../the-masters-money.html"]the masters money[/url], www.geocities.com/.../the-masters-money.html the masters money,  572351, <a href="www.geocities.com/.../exscite-money-home.html"">www.geocities.com/.../exscite-money-home.html">exscite">www.geocities.com/.../exscite-money-home.html">exscite money home</a>, [url="www.geocities.com/.../exscite-money-home.html"">www.geocities.com/.../exscite-money-home.html"]exscite money home[/url], www.geocities.com/.../exscite-money-home.html exscite money home,  792, <a href="www.geocities.com/.../sims-deluxe-money-cheat-codes.html"">www.geocities.com/.../sims-deluxe-money-cheat-codes.html">sims">www.geocities.com/.../sims-deluxe-money-cheat-codes.html">sims deluxe money cheat codes</a>, [url="www.geocities.com/.../sims-deluxe-money-cheat-codes.html"">www.geocities.com/.../sims-deluxe-money-cheat-codes.html"]sims deluxe money cheat codes[/url], www.geocities.com/.../sims-deluxe-money-cheat-codes.html sims deluxe money cheat codes,  bxo,

name wrote re: Stop creating custom delegate types
on 01-18-2009 2:57 AM

comment1,

Add a Comment

(required)  
(optional)
(required)  
Remember Me?

Enter the numbers above:
Copyright Los Techies 2008, 2009. All rights reserved.
Powered by Community Server (Commercial Edition), by Telligent Systems