Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding option parameter to a library in c# without creating a breaking change

Tags:

c#

I have a nuget package, in it I have a method which has optional args (v1.0)

public void MyMethod(int a = 0){}

I later created a new version of my package with another optional arg (thinking it wasnt a breaking change) (v1.1)

public void MyMethod(int a = 0, int b = 1){}

This package is consumed by another nuget package which references v1.0 I have both v1.1 and the other nuget package included in my project. This means that at a binding level Im using v1.1 and the package using v1.0 is redirecting to the 1.1 dll.

This causes a missing method exception because of the following: https://stackoverflow.com/a/9884700/1070291

I want to fix my library so that either signature will function, my first thought was this:

public void MyMethod(int a = 0, int b = 1){}
public void MyMethod(int a = 0){ MyMethod(a,1); }

However this causes an ambiguous method when its used elsewhere.

I'm wondering if there is some way to fill in the old method for backward compatibility without creating something ambiguous going forward.

I almost want to mark the old signature with something to instruct the compiler to include this in the assembly but not link any new methods to it.

like image 585
Not loved Avatar asked May 22 '17 03:05

Not loved


People also ask

How do you add optional parameters?

Here for the [Optional] attribute is used to specify the optional parameter. Also, it should be noted that optional parameters should always be specified at the end of the parameters. For ex − OptionalMethodWithDefaultValue(int value1 = 5, int value2) will throw exception.

Can you have optional parameter in C?

Optional arguments are generally not allowed in C (but they exist in C++ and in Ocaml, etc...). The only exception is variadic functions (like printf ).

How do you use optional parameters?

You can use optional parameters in Methods, Constructors, Indexers, and Delegates. Each and every optional parameter contains a default value which is the part of its definition. If we do not pass any parameter to the optional arguments, then it takes its default value.


1 Answers

Provide a method with explicitly no arguments. For example:

public void MyMethod(int a = 0, int b = 1) { }
public void MyMethod(int a = 0) { MyMethod(a, 1); }
public void MyMethod() { MyMethod(0, 1); }

This removes the ambiguity of having no arguments passed to MyMethod.

Extending for more arguments means you can write something like:

public void MyMethod(int a, int b, int c) { }
public void MyMethod(int a, int b) { MyMethod(a, b, 2); }
public void MyMethod(int a) { MyMethod(a, 1); }
public void MyMethod() { MyMethod(0); }

Note that we can completely remove optional arguments by explicitly implementing each overload. We do lose some intellisense help with default arguments, but this can be somewhat replaced with code comments.

like image 108
Rob Avatar answered Sep 28 '22 05:09

Rob