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);
Anthony Pegram
  • 123,721
  • 27
  • 225
  • 246

4 Answers4

19

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;
}
LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • 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. – Mohammad Azeem Ahmad Oct 20 '11 at 06:26
  • @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. – LarsTech Oct 20 '11 at 12:31
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.

wjfamilia
  • 91
  • 1
  • 1
  • 2
    To add to this, after several years, the toggling added a targetFramework="4.0" to the node of the Web.config which could be manually added to resolve this as well. – Daved Oct 27 '15 at 16:33
7

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

enter image description here

p.campbell
  • 98,673
  • 67
  • 256
  • 322
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rik
  • 28,507
  • 14
  • 48
  • 67
  • 1
    -1, optional parameters can be used with .net 3.5, it need to set language version to default see http://stackoverflow.com/a/8325095/451495 – gdbdable Apr 23 '12 at 08:37