c# - 编译器错误 "Default parameter specifiers are not permitted"

标签 c# compiler-errors .net-3.5 default-parameters

下面是我的代码。

public class PItem
{
    public String content;
    public int count;
    public int fee;
    public int amount;
    public string description;

    // Default values
    public PItem(String _content = "", int _count = 0, int _fee = 0, string _description = "", int _amount = 0)
    {
        content = _content;
        count = _count < 0 ? 0 : _count;
        fee = _fee;
        description = _description;
        amount = _amount < 0 ? 0 : _amount;
    }
}

这是在一个类里面。当我尝试运行程序时出现此错误:

Default parameter specifiers are not permitted

我该如何解决这个错误?

最佳答案

问题是在低于 4 的 C# 版本中不能有可选参数。
您可以找到有关此 here 的更多信息。

你可以这样解决:

public class PItem
{
  public String content;
  public int count;
  public int fee;
  public int amount;
  public String description;
  // default values
  public PItem(): this("", 0, 0, "", 0) {}
  public PItem(String _content): this (_content, 0, 0, "", 0) {}
  public PItem(String _content, int _count): this(_content, _count, 0, "", 0) {}
  public PItem(String _content, int _count, int _fee): this(_content, _count, _fee, "", 0) {}
  public PItem(String _content, int _count, int _fee, string _description): this(_content, _count, _fee, _description, 0) {}
  public PItem(String _content, int _count, int _fee, string _description, int _amount)
  {
      content = _content;
      count = _count < 0 ? 0 : _count;
      fee = _fee;
      description = _description;
      amount = _amount < 0 ? 0 : _amount;
  }
}

关于c# - 编译器错误 "Default parameter specifiers are not permitted",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6398382/

相关文章:

c++ - std::bind causes illegal indirection error

c# - 确定 Powerpoint 是否处于演示模式

gcc - 编译器错误 "missing binary operator before token"是什么意思?

ios - 在 Cordova iOS 包中找不到 `OBJROOT="$OBJROOT"` 来尝试修复构建错误 : xcodebuild: Command failed with exit code 65

c# - 推荐程序结构

c# - 从本地 Web 服务器或网络共享获取文件 - 哪个更好

.net-3.5 - 支持 .Net 3.5 的 StructureMap 最新版本是什么?

c# - 远程服务器返回错误 : (407) Proxy Authentication Required

c# - Windows Phone map 不断崩溃

c# - Anders Hejlsberg 的 C# 4.0 REPL