1

I am totally new to C# and have to now support an entire testsuite written in C#. I am using Visual Studio 2013 on Windows 2008 Server.

My issue is the following:

I had a class method with signature

public CommandInfo SetDns(String ipAdr1)

I changed it to add an optional argument

public CommandInfo SetDns(String ipAdr1, String ipAdr2="")

I just rebuilt this DLL and thought that all the other DLL's which refer to this will see no difference

But it seems the older DLL's now fail with error

System.MissingMethodException: Method not found: 'My.TestBeds.CommandInfo My.TestBeds.DDR.Net.SetDns(System.String)'.

My queries:

  1. Is this expected ? I'll have to rebuild all the DLL's as well ?

The trouble I have is that I have to check-in the DLL's too due to some constraint. So I wanted to rebuild and check-in the relevant ones only :(

  1. If I have to rebuild all the other dll's as well, is there a way to know which DLL's have this one as reference and need rebuilt ?
dymanoid
  • 14,771
  • 4
  • 36
  • 64
mittal
  • 915
  • 10
  • 29
  • 1
    Possible duplicate of [Does adding optional parameters change method signatures and would it trigger method missing exception?](https://stackoverflow.com/questions/30317625/does-adding-optional-parameters-change-method-signatures-and-would-it-trigger-me) – dymanoid May 08 '18 at 15:41
  • Rebuild the project with the `SetDns()`, and just change the `.csproj` file to point to the newly build DLL in the other projects that reference it. Then build the solution again, I think Visual Studio only builds the projects that need to be updated. – Jimenemex May 08 '18 at 15:42
  • The trouble is, 'what all refers it' is not known. It's only when something breaks at run-time, I get to know – mittal May 08 '18 at 16:11

1 Answers1

3

Yes, adding a parameter is a breaking change, even if it is optional. The IL contains the exact signature that the compiler resolved, not just the name. The typical way to implement this change without impacting compatibility would be to add an overload - something like:

public CommandInfo SetDns(string ipAdr1) { return SetDns(ipAdr1, ""); }
public CommandInfo SetDns(string ipAdr1, string ipAdr2) {...}

Otherwise: yes, this is a breaking change and you would need to rebuild all consumers.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • What is 'IL' ? Is there an easy way to get all consumers ? – mittal May 08 '18 at 15:43
  • 1
    @mittal IL is "intermediate language"; usually, .NET languages such as C# compile to IL, and then the JIT compiles to machine code on the machine you actually run it on. If you're familiar with Java: IL would be analogous to Java bytecode. – Marc Gravell May 08 '18 at 15:44
  • Ah !! Ok. So basically I should override the method instead of replacing it. Maybe I can leave the older version of method there and just add the new one too ? – mittal May 08 '18 at 15:47
  • @mittal as for finding all consumers: I'd rather not introduce breaking changes in the first place, but: the most reliable approach would be to just rebuild everything and see what breaks :) It helps if you have automated builds, of course... – Marc Gravell May 08 '18 at 15:47
  • 1
    @mittal yup; that's incredibly common - but by forwarding it as I've shown above, you only have one version of the actual code to maintain – Marc Gravell May 08 '18 at 15:47
  • Yeah, got it !! Regarding finding the consumers comment, the build doesn't break anything. Error is reported at run-time only so there is no way to know what will break :) – mittal May 08 '18 at 15:50