4

I'm not sure if it is a .Net or C# or even Visual Studio 2010/2008 thing, but I noticed that whenever I pause my application to change something in the code and the changes are made within a delegate, I can't resume the application but have to restart it.

So my questions are:

1) Is this depend on the programming language? Or the framework? Or the Editor?

2) Is it only when I change delegates? Or other things?

3) Why is this so?

4 Answers4

5

There are certain changes you can make in code which are significant enough that the debugger can't resume without a full restart. Typically you can safely change lines of code within a method, but if you change the class itself (adding a parameter to the method signature, for example), that will require a full recompile.

Anonymous delegates and lambda functions are represented in .NET pretty much as an anonymous class with some variable references. Since there is no name for the debugger to "reconnect" this particular lambda expression to when you resume, changing a delegate function basically turns it into a new class. You'll notice similar behavior with anonymous types.

StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
4

Edit and Continue doesn't support all possible code changes. It is well documented in the MSDN library, I'll repro the list here:

  • Changes to the current statement or any other active statement.
  • Changes to global symbols, including the following:
    • Adding new types.
    • Adding methods to a type.
    • Changing the signature of a type.
    • Adding fields, events, or properties to a type.
  • Editing an anonymous method or any method that contains an anonymous method.
  • Adding a new anonymous method.
  • Adding, removing, or changing attributes.
  • Adding, removing, or changing using directives.
  • Removing or changing local variables. Adding local variables is allowed.
  • Adding a foreach, using, or lock around the active statement.
  • Modifying a method that contains a yield return or yield break statement.
  • Changing a constructor with a field that is initialized by an anonymous method.

You are running afoul of the "Changing the signature of a type" limitation.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
2

Answering your questions:

1) Is this depend on the programming language? Or the framework? Or the Editor?

I don't know the exact name in the .NET terminology but in Java/Eclipse the modification of code while it is running is called Hot Code Replacement. It all depends on whether the runtime environment is able to accept the kind of changes while keeping the rest of the program valid.

2) Is it only when I change delegates? Or other things?

If the changes are in the behaviour I'd say it's more likely to be accepted. Structural changes like adding methods, renaming classes or modifying other maybe more sophisticated constructs that are translated to classes or otherwise rather static structures will cause problems at most times.

3) Why is this so?

Well, the other answers should make this clear. Some changes cannot be done during runtime. Static type safety is another reason that may be circumvented otherwise.

Martin Klinke
  • 7,294
  • 5
  • 42
  • 64
0

1: You can edit your html markup in the ASPX pages, but messing with the compiled code will require you to halt your application and recompile.

2: No, if you change any compiled code or the config files you will be required to stop debugging and recompile.

3: Like I implied in 1-2, the c# and vb code gets compiled before debugging starts, so in order to view changes to that code, you will have to recompile.

NoAlias
  • 9,218
  • 2
  • 27
  • 46
  • This is not true, you can change code while debugging under certain circumstances. See StriplingWarrior's answer. – Matt Greer Mar 03 '11 at 22:26
  • @Matt Greer: I think the Visual Studio IDE can run in two modes: one makes is so that you can't edit any .cs file without stopping the debugger. The other makes it so that you can edit files, but certain changes will require an application restart before you can resume debugging. ASP.NET projects tend to run in the latter mode. So we're both right. – StriplingWarrior Mar 03 '11 at 23:14