Friday, February 24, 2012 6:52 PM
Using Visual Studio 2010 SP1, and with a method definition like the following -
public static void test(string test1, [Optional, DefaultParameterValue("")] string test2)
When I try to call the method like so, omitting the optional parameter --
I get the error "no overload for method 'test' takes 1 arguments". However, the project compiles and runs fine. Passing in the optional parameter works as expected, and not passing in the parameter works as expected also with the default value getting set.
Why does VS insist on telling me that this is an error when it works perfectly?
- Moved by CoolDadTxMVP, Moderator Friday, February 24, 2012 9:29 PM Language related (From:Visual C# IDE)
Friday, February 24, 2012 9:29 PMModerator
That is not how you define optional parameters in C#. The correct syntax is this:
public static void test ( string test1, string test2 = "" )
The attributes you are using are for interop cases (specifically for COM interfaces is where you see this). As documented in MSDN these attribute do not allow you to specify optional parameters in languages that do not support them. Furthermore in languages that do support optional parameters you have to use the syntax of the language (for C# see above). Thus your C# method is really just a regular C# method that requires 2 string parameters. For languages that support optional parameters the compiler can retrieve the default argument and call the method but ultimately your method will always be called with 2 parameters. Note that if you referenced the method in another assembly it might work since ultimately optional parameters boil down to attributes and metadata but I've never tried that.
Nevertheless if you switch to the correct optional argument syntax your code will work correctly in C# and any other language that supports optional arguments.
Michael Taylor - 2/24/2012
Saturday, February 25, 2012 3:13 AMI have only used a declaration style as follows for your method:public static void test(string test1, string test2 = "")
}I have never seen your declaration style. Are you sure it is valid?
- Marked As Answer by bjquinn Friday, March 02, 2012 5:01 PM
Saturday, February 25, 2012 3:37 AM
Monday, February 27, 2012 2:25 PM
The compiler, for whatever reason, allows the declaration of optional parameters using the attributes (against the C# specification, I think).
The Visual Studio parser correctly doesn't recognize it as a valid optional parameters declaration.
I think the compiler accepts it so it have the same behavior for methods declared in the current project and for methods declared outside it.
Visual Studio is correct in rejecting that method call and the compiler tolerates it as a simplification, I think.
- Edited by Louis.frMicrosoft Community Contributor Monday, February 27, 2012 2:30 PM
Monday, February 27, 2012 2:36 PM
Friday, March 02, 2012 5:03 PM
Thanks, that did it. I don't know where I got that other optional parameter syntax from.
The only issue I had with the way you suggested is that it forces me to have all optional parameters at the end (i.e. I can't have required parameter, required parameter, optional parameter, required parameter). That was easy enough to resolve.