Export (0) Print
Expand All

Using the Edit and Continue Feature in C# 2.0

Visual Studio 2005
 

3 Leaf Solutions

October 2004

Summary: This article provides an overview of how to use the new Edit and Continue debugging feature in Visual C# 2005. The feature allows you to make changes to the code while an application is being debugged and apply those code changes without having to close, rebuild, and run the application again. (6 printed pages)

Applies to:
   Visual C# 2005

Contents

Introduction
What Is Edit and Continue
How Does Edit and Continue Help Developers?
Using Edit and Continue
How Does Edit and Continue Work?
Limitations of Edit and Continue
Setting the Next Statement
Conclusion

Introduction

Edit and Continue (hereafter referred to as E&C) is a new debugging feature introduced in Visual C# 2005. It allows you to make changes to the code while an application is being debugged and to apply the code changes without having to end execution, rebuild, and run the application again.

In this article, you'll see what E&C is, and walk through a debugging session using E&C. In the end, this article will examine how you can make best use of the E&C support in C#.

What Is Edit and Continue

E&C is a new feature that lets C# developers fix certain kinds of bugs in their code in a much more productive fashion. Traditionally, when you spot a bug in your application, you stop your application, change the code, and recompile. However, with E&C, you no longer have to stop your application and recompile.

When you make changes to your code with E&C, the running application is modified in memory. The state of the application is preserved, meaning all the variables retain the same value, all the database connections stay open, and so on. You can continue your application from the same point, but with the application using your new code. This means that you can write code and make changes a great deal faster than before. This feature is also useful when doing test driven development. When a test fails, you can set a breakpoint in the offending code, and step through to determine where the error is happening. Depending on the error, you may be able to fix it on the spot and continue running to see if the test passes.

How Does Edit and Continue Help Developers?

Imagine that you are debugging a complex Windows Forms application. You've made some changes to the code and you are stepping through your application to see whether your changes work as expected. Suddenly, you see that you've made a small mistake. A SQL query hasn't been written properly. Cursing your bad luck, you stop the application, make the correction and start all over again.

This is specifically the kind of scenario that E&C addresses. Typically C# developers work on a code-compile-debug cycle. However, this means that you often spend a lot of time recompiling the entire application even if you've changed only one line of code. E&C changes this approach and reduces the time required to fix bugs. For example, in the above scenario, you could just change the SQL query and continue running the application from the same point.

E&C enhances the productivity of developers. You can now focus on writing code and fixing bugs rather than recompiling the application over and over again to return the application to the same state.

Using Edit and Continue

Next, we'll look at some sample usage scenarios. These include some of the common tasks frequently performed with E&C.

Base Project

Start Visual C# 2005 Express Edition and create a new console application. The standard skeleton application with a Main method has been generated for you. There's nothing extra that you have to do to enable the E&C feature. It's on by default.

Changing Variables

Start with a traditional "Hello World" example. Make the changes shown below to the Main method.

static void Main(string[] args)
{
   string s = "Hello World";
   Console.WriteLine(s);
}

Set a breakpoint on Console.WriteLine method call. Select the Debug | Start menu command, and run the application. The application hits the breakpoint and execution is paused.

Now, let's say that you want to break with tradition and write something other than "Hello World" to the console. Normally, you would have to stop the application, make the change, and restart. However, this is not necessary thanks to E&C. With the application still paused, change the string "Hello World" to "Edit and continue rocks!".

    
   string s = "Edit and continue rocks!";

As with previous version of Visual Studio, you can move the execution point to the line you wish to execute, which offers awesome new possibilities when coupled with E&C. In this case, you need to make this string assignment code run again. To do this, move the execution point to the line where you've made the change by dragging the yellow arrowhead in the margin. If you were already deep into a function, another option would be to simply edit the value in the locals, watch, or command window

ms379578.edit_continue-fig1(en-US,VS.80).gif

Figure 1. Editing the code

Press F5 and run the application. If you watch the status bar, you'll see messages as the code changes are compiled and applied. In the console window, you now see Edit and continue rocks! was printed rather than Hello World. Unlike previous versions of C#, you didn't have to stop and rebuild the application to make this change.

Changing Code

E&C doesn't stop with the ability to change string variables. Let's look at a scenario where E&C is used to fix a common bug. Working with the same project, change the code so that it matches the following snippet:

static void Main(string[] args)
{
   string s = "Hello World";
   
   for(int i=0; i <= s.Length ; i++)
   {
      Console.Write( s.Substring(i, 1) );
   }
}

Set a breakpoint on the Console.Write method call and run the application using the Debug | Start menu command.

This rather contrived example does the same thing the first program, but it has a subtle error-by-one bug in the for loop. You can fix this bug without stopping and rebuilding. With the application still paused, change the for loop declaration to the following and run the application.

   for (int i=0; i < s.Length; i++)

You'll see Code changes were applied successfully in the status area and the expected output (Hello World in this case) in the console window once the changes are applied by performing a debugger action, such as stepping, resuming execution, or setting the next statement. The code changes that you made were applied to the executable in memory.

ms379578.edit_continue-fig2(en-US,VS.80).gif

Figure 2. Status message on applying code changes

How Does Edit and Continue Work?

Though E&C is simple to use, the CLR and the compiler have to work together to do some heavy lifting in the background. When a file is edited in the IDE during a debugging session, the compiler first checks whether the change is allowed or not. If it is not possible to perform that change, you'll see a dialog box like the one in Figure 3.

ms379578.edit_continue-fig3(en-US,VS.80).gif

Figure 3. Message box shown when you make a change that is not allowed

Clicking the Edit Code button takes you back to the debugging session to allow you to fix the errors that won't allow the debugging session to continue. You might choose this option if the errors that didn't allow E&C to continue were build errors. . Clicking the Stop Debugging button ends the current debugging session and reverts back to design mode. You would choose this option if the errors detected are changes that you need to make. The compiler supplies delta-IL and delta-metadata, which is used to replace parts of the existing IL and metadata in memory. Delta-IL and delta-metadata simply refers to the gap used to insert the edited code. If the edited methods have been JIT-ed into native x86 code, they are replaced with new x86 code.

Limitations of Edit and Continue

The rule of thumb for making E&C changes is that you can change virtually anything within a method body, so larger scope changes affecting public or method level code are not supported. Some examples of such unsupported changes are:

  • Changing the name of a class.
  • Adding or removing method parameters.
  • Adding public fields to a class.
  • Adding or removing methods.

In such cases, you'll notice a squiggle under the offending change. If you try to continue while squiggles exist in the program, you'll receive the message shown in Figure 4.

ms379578.edit_continue-fig4(en-US,VS.80).gif

Figure 4. Error on making a change not supported by E&C

Setting the Next Statement

In the examples in this article, you were already aware of the bugs in the code. However, in the real world, bugs are often found after they have already been stepped over. Like we did in the examples earlier, you could then go and make the necessary fixes with the application still running.

In such cases, it is important to remember that E&C is not a time machine. If you change any variable, initialization code, or any other code that could have an effect on the currently executing method, you can use the Set Next Statement command from the context menu to move the execution pointer to a prior point in your code. However, the Set Next Statement never undoes program execution. It only moves the point of execution. Keep in mind, you could use the locals, watch, or command window to quickly bring an application to a desired state when multiple attempts at debugging are required.

Conclusion

E&C is included in Visual C# 2005 because it enhances developer productivity, and this article illustrates the kinds of scenarios where E&C can be beneficial.

One of the best aspects of E&C is that almost anyone can benefit from it, regardless of the skill level or knowledge of the feature. We can all look forward to smoother and less interrupted development, resulting in a more productive day in the life of a developer.

Show:
© 2014 Microsoft