VS.NET suggestion – anonymous delegate auto-complete

Preface:


Let’s say that you have Button control named myBtn and you want to handle its Click event:


myBtn.Click += new EventHandler(myBtn_Click);


static void myBtn_Click(object sender, EventArgs e) {
   // do something
}



Now, raise your hand if you ever attached the event handler to the event manually, meaning you wrote “new EventHandler(myBtn_Click);” or even specified the method signature “void myBtn_Click(object sender, EventArgs e)”.
I don’t know about you, but the way I work is by simply writing “myBtn.Click +=” and this magic yellow thingi appears:


autoCompleteEvent.gif


TAB-ing again will generate the method stub and I’m good to go.


Doing this yellow magic on delegates parameters:


We all(?) worked with List<T>. This nice class has some nice methods like ForEach, FindAll, Find etc which get delegate(Action<T>\Predicate<T>) as parameter. for example: 


List<int> numbers = new List<int>(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });


// Print the numbers via ForEach method
numbers.ForEach(
delegate(int i) {
   
Console.Write(i);
});


Would it be great if you can write something like:


numbers.ForEach(


And the yellow thingi will popup and by clicking TAB this code will be generated:


numbers.ForEach(delegate(int i) {
   //TODO
});


This means that this IDE feature should check the type of the delegate – the number and type (even generic type!) of parameters and the return type. This will prevent us from looking at MSDN or “Go To Definition” while working with delegates which are sent as parameters.


Time for you to make a difference:


what say you ? can this save you some time ? If so – leave a comment, I’ll do my best to implement it or to forward this request to MS folks (as ladybug).

 

Oren Ellenbogen

 

5 thoughts on “VS.NET suggestion – anonymous delegate auto-complete

  1. This trick look awesome.

    1) What are all the options that this one will handle? is it only ForEach and FindAll?
    2) will it give a combo for choosing between different delegates?
    3) Isn’t the ReSharper giving such abilities?

    Anyway, if you need help in implementing one of the features… it will be my pleasure to help you.

  2. Yoni – 10x for your "vote" :)

    Shani –
    1). for any kind of delegate (even one that you wrote). the IDE will have to be smart enough to probe the delegate(s) types.
    2). I thought about this one – a combo should be opened if the method has overloads. this can be totally cool !
    3). To be honest, I haven’t checked. Is there a kind reader that can check this one ?

    If I’ll implement it you’ll be the first to know. I’m talking with one of the guys from MS which are responsible for those kind of things so I’ll keep you all update.

  3. ReSharper 2.0 does indeed have "Create anonymous delegate for…" and "Create a new method for…" options in its autocomplete box.

    I use it all the time to set up UI event handlers that just call some other method to do the real work:

    buttonSomeButton.Click += [autocomplete here] ->
    buttonSomeButton.Click += delegate(object sender, EventArgs e){
    [Put your code here]
    };

    And then if you don’t use the parameters, ReSharper highlights them in grey, and gives you an option to remove them.

Comments are closed.