62

I'm really struggling with this one. I need a generic list parameter for my Get method, but it needs to be optional. I just did this:

public dynamic Get(List <long> ManufacturerIDs = null)

Unfortunately on runtime i get the error:

Optional parameter 'ManufacturerIDs' is not supported by 'FormatterParameterBinding'.

How to get a generic list as an optional parameter here?

JohnnyHK
313k70 gold badges631 silver badges477 bronze badges
asked Mar 26, 2013 at 15:12

1 Answer 1

100

What's the point of using an optional parameter? List<T> is a reference type and if the client doesn't supply a value it will simply be null:

public HttpResponseMessage Get(List<long> manufacturerIDs)
{
 ...
}
answered Mar 26, 2013 at 15:30

3 Comments

In my experience, there are many cases where you have to explicitly set optional parameters to null (stackoverflow.com/a/22397723/1454888). This is counter-intuitive for me, but it works. Thanks.
I am using swashbuckle for swagger documentation generation. In that context it does matter how the optional parameters are defined, observe: [FromUri] object[] foo = null --> "Provide multiple values in new lines." [FromUri] object[] foo --> "Provide multiple values in new lines (at least one required)." object[] foo --> "(required)" So in that context it really depends on what you expect from your end user.
I had to insert [FromUri] attribute before the List<string> optional parameter. There is a difference when you use = null as @Nebula noticed. When you don't add = null then you have to specify at least one element.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.