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
  • 305,182
  • 66
  • 621
  • 471
Dennis Puzak
  • 3,726
  • 3
  • 23
  • 22

1 Answers1

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)
{
    ...
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 4
    In my experience, there are many cases where you have to explicitly set optional parameters to null (http://stackoverflow.com/a/22397723/1454888). This is counter-intuitive for me, but it works. Thanks. – Augusto Barreto Jan 14 '16 at 18:45
  • 19
    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. – Nebula Apr 01 '16 at 11:30
  • 1
    I had to insert [FromUri] attribute before the List 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. – pawellipowczan Mar 09 '20 at 14:59