不允许使用默认参数说明符

Moh*_*mad 9 .net c# .net-3.5 c#-3.0

我有以下代码给出错误

不允许使用默认参数说明符

怎么解决这个问题?

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);
Run Code Online (Sandbox Code Playgroud)

Lar*_*ech 18

根据您的错误消息,您无法在v3.5中执行此操作.

解决方法是多个构造函数:

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;
}
Run Code Online (Sandbox Code Playgroud)


小智 9

我刚刚遇到这个错误,我的项目也是针对4.0而不是3.5或更低.

我将其切换为3.5,然后再切换回4.0,然后错误消失了.希望这些步骤对您或其他人有用.

  • 除此之外,几年后,切换还向Web.config的<compilation>节点添加了targetFramework =“ 4.0”,也可以手动添加它以解决此问题。 (2认同)

p.c*_*ell 7

应用程序/类库未设置为以.NET 4 Framework为目标.在项目的设置页面中进行调整.

在此输入图像描述


Rik*_*Rik 5

可选参数是 C# 4 的一项功能,在早期版本中不存在。由于您使用的是 .NET 3.5,因此不能使用可选参数。

要么切换到 .NET 4.0,要么改用重载方法。