9

I have the following code that gives the error

Default parameter specifiers are not permitted

How can this be fixed?

bool listSubscribe(string apikey, string id, string email_address, string [] merge_vars, string email_type="html", bool double_optin=false, bool replace_interests=true, bool send_welcome=false); bool listUnsubscribe(string apikey, string id, string email_address, bool delete_menber=false, bool send_goodbye=true, bool send_notify=true); 
5
  • 7
    Are you targetting .NET 4.0? Visual Studio 2010? Commented Oct 19, 2011 at 13:59
  • 2
    which version of Visual Studio and which .NET framework are you using? are these methods inside a Web form or what kind of class? Commented Oct 19, 2011 at 14:00
  • This might be worth reading: blogs.msdn.com/b/ericlippert/archive/2011/05/12/… Commented Oct 19, 2011 at 14:09
  • 1
    I am using 3.5 .NET Framework Commented Oct 19, 2011 at 14:53
  • you need set language version in project properties, see stackoverflow.com/a/8325095/451495 Commented Apr 23, 2012 at 8:38

4 Answers 4

20

As per your error message, you can't do that in v3.5.

The work around is multiple constructors:

bool listUnsubscribe(string apikey, string id, string email_address) { return listUnsubscribe(apikey, id, email_address, false, true, true); } bool listUnsubscribe(string apikey, string id, string email_address, bool delete_menber, bool send_goodbye, bool send_notify) { return whatever; } 
Sign up to request clarification or add additional context in comments.

2 Comments

Hi LarsTech I am doing this work in the interface so therefore it didn't allow to define the definition of the function, Could you recomeend any other solution for it.
@user1003290 No, you won't get that to work in an interface. The interface would have to have all of the multiple constructors, and then in the class that consumes the interface, you would have to set the defaults. It can't be done from the interface alone.
9

I just now encountered this error and my project is also targeting 4.0 and not 3.5 or below.

I toggled it to 3.5 and then back to 4.0 and then the error went away. Hopefully these steps will work for you, or someone else.

1 Comment

To add to this, after several years, the toggling added a targetFramework="4.0" to the <compilation> node of the Web.config which could be manually added to resolve this as well.
7

The application/class library is not set to target .NET 4 Framework. Adjust in the project's settings page.

enter image description here

1 Comment

OK p.campbell but whether there is not any alternative of it in .net framework 3.5
5

Optional parameters are a feature of C# 4, not present in earlier versions. Since you're using .NET 3.5, you can't use optional parameters.

Either switch to .NET 4.0, or use overloaded methods instead.

1 Comment

-1, optional parameters can be used with .net 3.5, it need to set language version to default see stackoverflow.com/a/8325095/451495

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.