-1
public void ObjTest(StringBuilder sb, List<string> list, int i = 0,  [Optional] string bs)
{
    ......
 }

The above code throwing compilation error " Optional parameters must appear after all required parameters ". Does optional parameter and optional attribute not supported together in same method parameter, but it allows params arry after optional paramer ?

Anoob K A
  • 59
  • 8
  • I don't understand your question (or why you've repeated it verbatim in the title and the body, using a heading in the body). Are you asking *why* something is allowed/forbidden, or what *is* allowed? Please clarify. – Jon Skeet Aug 02 '14 at 13:42
  • C# never formally supported the [Optional] attribute. Its meaning was changed in version 4, used to make [COM interop easier](http://stackoverflow.com/a/24695592/17034). This is not something you ought to consider using in your own code, given the oddball default values it can generate. – Hans Passant Aug 02 '14 at 13:54
  • @ Hans Passant- Thanks for the information related to COM interop details. However I am able to use its like a optional parameter at framework 3.5. – Anoob K A Aug 02 '14 at 16:34

1 Answers1

1

You can use them in conjunction but the optional parameter (the language construct) must be the last parameter in the parameter list.

public void X(StringBuilder sb, List<string> list, [Optional] string bs, int i = 0)
{
}
User 12345678
  • 7,714
  • 2
  • 28
  • 46