They are Anonymous Methods, not Anonymous Delegates.


It is not just a talking point because we want to be difficult. It helps us reason about what exactly is going on.  To be clear, there is *no such thing as an anonymous delegate. They don’t exist (not yet).  They are "Anonymous Methods" – period.  It matters in how we think of them and how we talk about them.  Lets take a look at the anonymous method statement "delegate() {…}".  This is actually two different operations and when we think of it this way, we will never be confused again.  The first thing the compiler does is create the anonymous method under the covers using the inferred delegate signature as the method signature.  It is not correct to say the method is "unnamed" because it does have a name and the compiler assigns it. It is just hidden from normal view.  The next thing it does is create a delegate object of the required type to wrap the method. This is called delegate inference and can be the source of this confusion. For this to work, the compiler must be able to figure out (i.e. infer) what delegate type it will create. It has to be a known concrete type.  Let write some code to see why.

private void MyMethod()

Does not compile:

1) Delegate d = delegate() { };                       // Cannot convert anonymous method to type ‘System.Delegate’ because it is not a delegate type
2) Delegate d2 = MyMethod;                         // Cannot convert method group ‘MyMethod’ to non-delegate type ‘System.Delegate’
3) Delegate d3 = (WaitCallback)MyMethod;   // No overload for ‘MyMethod’ matches delegate ‘System.Threading.WaitCallback’

Line 1 does not compile because the compiler can not infer any delegate type. It can plainly see the signature we desire, but there is no concrete delegate type the compiler can see.  It could create an anonymous type of type delegate for us, but it does not work like that.  Line 2 does not compile for a similar reason. Even though the compiler knows the method signature, we are not giving it a delegate type and it is not just going to pick one that would happen to work (not what side effects that could have).  Line 3 does not work because we purposely mismatched the method signature with a delegate having a different signature (as WaitCallback takes and object).


4) Delegate d4 = (MethodInvoker)MyMethod;  // Works because we cast to a delegate type of the same signature.
5) Delegate d5 = (Action)delegate { };              // Works for same reason as d4.
6) Action d6 = MyMethod;                                // Delegate inference at work here. New Action delegate is created and assigned.

In contrast, these work. Line 1 works because we tell the compiler what delegate type to use and they match, so it works.  Line 5 works for the same reason. Note we used the special form of "delegate" without the parens. The compiler infers the method signature from the cast and creates the anonymous method with the same signature as the inferred delegate type. Line 6 works because the MyMethod() and Action use same signature.

I hope this helps.

Also see:

This entry was posted in Uncategorized. Bookmark the permalink.

8 Responses to They are Anonymous Methods, not Anonymous Delegates.

  1. Unknown says: Acer aspire 3610 Battery Acer aspire 3680 Battery Acer aspire 5050 Battery Acer aspire 5080 Battery Acer aspire 5100 series Battery Acer aspire 5500 Battery Acer aspire 5550 Battery Acer aspire 5580 Battery Acer aspire 9300 series Battery Acer aspire 9400 series Battery Acer lcbtp03003 Battery Acer squ-401 Battery Acer travelmate 290 Battery Acer travelmate 291 Battery Acer travelmate 29x Battery Acer um08a71 Battery Acer um08a72 Battery Acer um08a73 Battery Acer um08b72 Battery Acer white aspire one zg5 Battery Apple a1022 Battery Apple a1045 Battery Apple a1057 Battery Apple a1078 Battery Apple a1079 Battery Apple a1148 Battery Apple a1175 Battery Apple a1189 Battery Apple a7426 Battery Apple g4 12-inch Battery Apple m7426 Battery Apple powerbook g4 12 inch Battery Asus a3000 Battery Asus a4000 Battery Asus a42-a3 Battery Asus a42-w1 Battery Asus m6 Battery Asus m6000 Battery Asus m6n Battery Asus w1 Battery Asus w1000 Battery Asus z9100 Battery Compaq n600 Battery Compaq n600c Battery Compaq n610 Battery Compaq n610c Battery

  2. Unknown says: Hp dv2200 Battery Hp dv4000 Battery Hp dv6000 Battery Ibm thinkpad t40 Battery Toshiba satellite a105 Battery Toshiba satellite m45 Battery Toshiba pa3285u-3bas Battery Toshiba pa3395u-1brs Battery Toshiba pa3399u-1bas Battery Toshiba pa3399u-1brs Battery Toshiba pa3399u-2bas Battery Toshiba pa3399u-2brs Battery laptop battery laptop batteries Toshiba pa3465u-1brs Battery Toshiba pa3534u-1brs Battery Toshiba pa3594u-1brs Battery Uniwill 255-3s4400-g1l1 Battery Uniwill 258-4s4400-s1p1 Battery HP pavilion dv9000 Battery HP pavilion dv9700 Battery HP pavilion dv9500 Battery HP pavilion dv9600 Battery Dell inspiron b130 Battery Dell inspiron mini 9 Battery Hp nc8230 Battery Hp nc8430 Battery Hp nw8000 Battery Hp nw8200 Battery Hp nc6200 Battery Hp nc6100 Battery Hp nc6120 Battery Hp nc6400 Battery Hp pavilion dv2000 Battery Hp pavilion dv6000 Battery Hp pavilion dv8000 Battery Hp pavilion dv9000 Battery Hp pavilion dv9500 Battery Hp pavilion dv9600 Battery Hp pavilion dv9700 Battery Acer aspire 5520 Adapter Acer aspire 5920 Adapter Dell kd476 Battery Dell kr-onx511 Battery Dell latitude d620 Battery Dell latitude d630 Battery

  3. Unknown says: compaq 19V 4.74A 90w 5.5mm*2.5mm adapter PA-1650-05D2 AD-90195D dell 19.5V 3.34A 65w 7.4mm*5.0mm with pin pa-12 adapter PA-1650-05D2 PA-1900-02D dell 19.5V 4.62A 90w 7.4mm*5.0mm with pin pa-10 adapter dell 19.5V 6.7A 130w 7.4mm*5.0mm with pin pa-13 adapter laptop PA-1151-06D dell 19.5V 7.7A 150w 7.4mm*5.0mm with pin pa-15 adapter PA-1600-06D1 PA-1600-06D2 dell 19V 3.16A 60w 5.5mm*2.5mm adapter ADP-70E ADP-70EB dell 20V 3.5A 70w horseshoe style special dell pa-6 adapter ADP-65DB delta 19V 3.42A 65w 5.5mm*2.5mm adapter ADP-90FB delta 19V 4.74A 90w 5.5mm*2.5mm adapter A4190 fujitsu 19V 3.16A 60w 5.5mm*2.5mm adapter laptop C2110 fujitsu 19V 4.74A 90w 5.5mm*2.5mm adapter laptop CA01007-0920 FMV-AC314 fujitsu 19V 4.22A 80w 5.5mm*2.5mm adapter laptop PPP009X PW-AC003 hp 19V 4.74A 90w 5.5mm*2.5mm adapter laptop DC359A DL606A hp 18.5V 3.5A 65w 4.8mm*1.7mm adapter laptop hp 19V 4.74A 90w 4.8mm*1.7mm adapter laptop hp 18.5V 4.9A 90w 5.5mm*2.5mm adapter laptop hp 18.5V 4.9A 90w flat dc adapter laptop PA-1121-02H PA-1121-12H hp 18.5V 6.5A 120w 5.5mm*2.5mm adapter laptop hp 18.5V 6.5A 120w flat dc adapter laptop hp 19V 7.1A 135w flat dc adapter laptop hp 19V 9.5A 180w flat dc adapter laptop AC-C10 CQPS1200 hp 19V 3.16A 60w 5.5mm*2.5mm adapter laptop 02K6553 02K655 ibm 16V 3.5A 56w 5.5mm*2.5mm adapter laptop 92P1153 92P1154 ibm 20V 3.25A 65w 7.9mm*5.5mm DC with pin inside adapter laptop PA-1900-171 ibm 20V 4.5A 90w 7.9mm*5.5mm grey DC with pin inside adapter laptop compaq 19V 4.74A 90w 5.5mm*2.6mm adapter

  4. Unknown says: dell inspiron 9200 battery dell inspiron 9300 battery dell inspiron 9400 battery dell e1705 battery dell inspiron 1520 battery dell inspiron 1720 battery dell vostro 1500 battery dell vostro 1700 battery dell 0xr693 battery dell 312-0625 battery dell 312-0633 battery dell gw240 battery dell hp297 battery dell rn873 battery dell xr693 battery dell inspiron 1525 battery dell inspiron 1526 battery dell m1210 battery dell nf343 battery dell 1210 battery dell cg036 battery dell d044h battery dell w953g battery dell 312-0831 battery dell inspiron 910 battery dell inspiron mini 9 battery dell xd187 battery dell inspiron 1300 battery dell inspiron b120 battery dell inspiron b130 battery gateway 12msbg battery gateway 4s2p battery gateway 8msb battery gateway 8msbg battery gateway s62044l battery gateway m320 battery gateway m325 battery gateway 4000 battery gateway s62066l battery gateway m680 battery gateway m360 battery gateway m460 battery gateway s62066l battery gateway squ-412 battery gateway squ-413 battery

  5. Unknown says: dell inspiron 630m battery dell inspiron 640m battery dell xps m140 battery dell 312-0566 battery dell 312-0567 battery dell 451-10473 battery dell 451-10474 battery dell pu556 battery dell pu563 battery dell tt485 battery dell wr050 battery dell xps 1330 battery dell xps m1330 battery dell inspiron 6400 battery dell latitude 131l battery dell vostro 1000 battery dell 1501 battery dell e1505 battery dell kd476 battery dell gd761 battery dell 312-0273 battery dell 312-0417 battery dell 451-10180 battery dell c2174 battery dell f1244 battery dell g1947 battery dell h5559 battery dell hj424 battery dell inspiron 9100 battery dell dell inspiron xps battery dell inspiron 2000 battery dell inspiron 2100 battery dell inspiron 2800 battery dell latitude ls 400 battery dell latitude c400 battery dell 4e369 battery dell d620 battery dell d630 battery dell latitude d620 battery dell latitude d820 battery dell latitude d830 battery dell latitude d531 battery dell precision m65 battery dell d820 battery dell inspiron 6000 battery

  6. Unknown says: asus w1000g battery asus w1000 silver battery asus w1 silver battery asus a42-w1 silver battery asus a42-w3 battery asus w3a battery asus w3n battery asus w3v battery asus w3000a battery asus w3000 battery asus w3000v battery asus a42-a3 battery asus a3000 battery asus z9100 battery asus a3 battery asus z91 battery asus a31-s5 battery asus a32-s5 battery asus s52n battery asus s5000 battery asus s5200n battery asus s5n battery asus m5n battery asus s5n battery asus s5000n battery asus s5000a battery asus a32-s5 white battery asus s5000 white battery asus m5000 white battery asus a42-a2 battery asus a2000 battery asus a2500h battery asus a4000 battery asus a42-a4 battery asus a4 battery asus 90-naa1b1000 battery asus s2691061 battery asus a42-v6 battery asus m3000 silver battery asus m3n silver battery asus a42-m6 battery asus m6n battery asus m67 battery asus w1001g battery

  7. Unknown says:

    Hi,Do you have used LCDs, second hand LCDs, used flat screens and used LCD monitors? Please go Stars).We are constantly buying re-usable LCD panels and working for LCD recycling.The re-usable panels go through strictly designed process of categorizing, checking, testing, repairing and refurbishing before they are re-used to make remanufactured LCD displays and TV sets.Due to our recent breakthrough in testing and repairing technology of LCD, we can improve the value for your LCD panels.[cibfccbbgijcgdh]

  8. Unknown says:

Comments are closed.