A Little C# 2.0 Gem

Published 30 May 07 01:06 PM

You may already know this (maybe I did at one point, but if so I surely forgot it), but I learned it today and was pleasantly surprised (I'm easily impressed).  I would like to thank ReSharper for this discovery <g>!

Anyways, here it is.  If you're using anonymous methods on a defined delegate type and your method body is not using any of the parameters for the delegate signature, you can simply omit the entire parameters section.  Here's an example.

   1: Timer t = new Timer(2000);
   2: t.AutoReset = false;
   3: t.Enabled = true;
   4: t.Elapsed += new ElapsedEventHandler(delegate {
   5:         bugFlowSvc.CloseBug(instance.InstanceId, bug);
   6:     });

I'm so easily amused!

Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Duncan Godwin said on May 30, 2007 5:17 PM:

After t.Elapsed += hit Ctrl+Shift+Space and you get options to generate an anonymous delegate or a new method.  Even less code :)

# hdierking said on May 30, 2007 5:20 PM:

Totally cool!  Thanks for the tip!

Seriously though - where would we be without R#?!?!

# Richard said on May 31, 2007 10:53 AM:

You can even take it one step further, and just write:

t.Elapsed += delegate { bugFlowSvc.CloseBug(instance.InstanceId, bug); };

The compiler already knows that it's an ElapsedEventHandler. It doesn't need you to remind it. :)

# Mithun said on June 19, 2008 3:26 AM:

Can we pass a method in "ElapsedEventHandler()" as I have done below..

t.Elapsed += new ElapsedEventHandler(CallFormLoad);

The passed method "CallFormLoad" is as under...

public static void CallFormLoad(object sender, ElapsedEventArgs e)

{

}

Please guid me on this

# Mithun said on June 19, 2008 3:43 AM:

What I meant was why I'm not able to debug the method passed to the delegate

Folowing is the code skeleton...

class Service1

{

  public void OnStart()

 {

    t.Elapsed += new  ElapsedEventHandler(CallFormLoad);

 }

 public static void CallFormLoad(object sender,   ElapsedEventArgs e)

 {

 }

}

# hdierking said on June 19, 2008 10:51 AM:

Hi Mithun - your event code looks fine and is probably not the issue with regards to your debugger.  I would suspect that it has more to do with how you're using System.Timer.  I just tested the following on my machine with the debugger and all breakpoints hit just fine.

namespace ConsoleApplication1

{

   internal class Program

   {

       private static void Main(string[] args) {

           var svc = new Service1();

           svc.OnStart();

           Console.WriteLine("block the thread so that the timer will expire");

           Console.ReadLine();

       }

   }

   internal class Service1

   {

       public void OnStart() {

           var t = new Timer(2000);

           t.AutoReset = false;

           t.Elapsed += CallFormLoad;

           t.Start();

       }

       public static void CallFormLoad(object sender, ElapsedEventArgs e) {

           Console.WriteLine("CallFormLoad");

       }

   }

}

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required

About hdierking

I am currently the Editor-in-Chief for MSDN Magazine. I joined Microsoft in 2006 as a product planner with the certification team at Microsoft Learning. Prior to that, I spent my career as a developer and later as an architect. My main technology passions include pretty much anything on language theory, agile development, and service-oriented architecture.
Page view tracker