edit & continue in C# 2.0 ... - A world apart from the everday ...

A world apart from the everday ...

Assert.IsTrue(Entries.Count == 0);

edit & continue in C# 2.0 ...

Oooh man i gotta say this is one of the best new features added to C#. it was asked for by the community, it was rumored almost to death during early betas, but thankfully Microsoft once again listened to what it's customers wanted and decided to throw us a little lifeline here with this one.

Yes, yes i know the "purists" out there call it evil and refuse to see the point of it but hear me out ...

Imagine that you are trying to debug a hectic application. whilst you are stepping through your code to see whether everything works as it should you see that you've made a small mistake; a database query is missing a comma between fields. Cursing your torrid luck, you stop the application, make the correction and start all over again.... welcome to the life of a C# 1.0 developer ...

What a pain in the butt! yes i know you should've just done it properly the first time ... but how many of us do that ?!?

Now step in C# 2.0 with edit & continue; now you can simply fix your sql query on the fly and continue onwards without taking the PT (physical torture) of stopping the debugger, editing the code, recompiling your entire project, then painstakingly stepping through everything again to get back to the point where you were 10 minutes ago.

So time for a demo ...

Create a simple Console application in C# 2.0, and do something simple as shown below ...

  static void Main(string[] args)
  {
     string s = "this is sooooo boring!";
     Console.WriteLine(s);
  }

Set a breakpoint on the Console.WriteLine line and run your app. when the code hits your breakpoint, change the value of s using your autos window,

Now press F11 and go look what happened;

simply wicked!

yes i know this is a trivial example but with Edit and Continue you can do so much more ...

you can change code on the fly;

static void Main(string[] args)
{
   string s = "Hello World";
   
   for(int i=0; i 

Set a breakpoint on the Console.Write method call and start your debugger ...

this silly code sample has a subtle error-by-one bug in the for loop (did anybody spot it???). Now thanks to Edit and Continue you can fix this bug without stopping and rebuilding. Whilst you're stopped on your breakpoint, change the for loop to the following and continue your application.

   for (int i=0; i 

continue now hit F5 and watch pure “magic” at work ...

It is simply brilliant!

I do not want to try and imagine what the poor compiler and the CLR have to do in the background to get this working, but they did so well done C# team! I know this kind of thing will save me many many hours of frustration and make me a much more productive developer.

Now if I could just somehow stop doing these silly things from the word go then all would be better!

Sure there are some things that you cannot do with Edit and Continue like;

  • Add new methods
  • Adding or removing parameters from a method
  • Changing the name of a class or method
  • etc.

But with what it gives us, already I'll be far more productive.

 

 

oh and before I get hammered on from the VB.NET development *cough* community, yes I know you've had this since VB3 or something, but I don't care; we've finally got in C# and that is all that matters ... well to me anyways :-D

Posted: Jul 27 2005, 11:36 AM by Ryan CrawCour | with 5 comment(s)
Filed under:

Comments

Brady Kelly said:

OK, C# 1 is not ideal and I do look forward to Edit & Continue, but I don't painfully step through all my code every time I recompile. I frequently set 'checkpoint' breakpoints as I step, so if I have to stop I can resume stepping not far behind the point I stop at.
# July 27, 2005 12:18 PM

Senkwe said:

Bah, I still don't like it. (From a TDD point of view that is)
# July 27, 2005 12:42 PM

Ryan said:

Senkwe;

i hear your point; but even in a Test Drive Development environment as a developer you still need to debug your code that you are busy writing.

TDD does not for one moment encourage compile==workingcode. so as a dev you still need to compil, debug, step through and make modifications along the way.

once you're happy your code at least appears to run then unleash it on those carefully written tests, that were obviously done prior to coding even beginning.
# July 27, 2005 1:03 PM

CJ said:

I was quite surprised the first time I tried edit-and-continue in Beta 2 - my app was running, I tried to make a change to the code, and a popup informed that I couldn't edit my code while the app was running! A major step backwards ... luckily, the next time I tried it worked fine, as it has since.

Oh well, I guess it's Beta for a reason :-)
# July 27, 2005 2:55 PM

Ryan said:

Like i said there are some times where you cannot change your code; this instance was probably one of those times.

# Add new methods
# Adding or removing parameters from a method
# Changing the name of a class or method
# July 27, 2005 2:59 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: