I have a .NET application that uses an assembly (.dll) that defines some method:
public void DoSomething()
{
// Do work
}
Suppose this method signature changes to include a string return type:
public string DoSomething()
{
// Do work
return "something";
}
Why does the code that uses this method fails on a System.MissingMethodException ?
It seems to me, that at all call sites to this method, no use was made of the return value (since it did not exist before).
Why does this change break the code then?
The other answers which state that you've changed the signature of a method, and therefore must recompile the callers, are correct. I thought I might add some additional information about this bit of your question:
It seems to me, that at all call sites to this method, no use was made of the return value (since it did not exist before).
That is exactly right. Now, consider this question: how do you write code that makes no use of data? You seem to be labouring under the entirely false assumption that not using a value requires no code, but not using a value certainly does require code!
Suppose you have methods:
static int M1(int y) { return y + 1; }
static void M2(int z) { ... }
and you have a call
int x;
x = M1(123);
What happens at the IL level? the following:
Suppose you now do:
M1(345);
What happens? Same thing:
But there is no instruction which stores the value on the stack anywhere, so we have to issue a pop instruction:
Now suppose you call
M2(456);
What happens?
Now do you see why changing a method from void returning to value returning is a breaking change? Every caller now has to pop the unused value off the stack. Doing nothing with data still requires cleaning it up off the stack. You are misaligning the stack if you do not pop that value off; the CLR requires the stack to be empty at the beginning of every statement to ensure that this sort of misalignment does not happen.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With