C# 编译器错误";不允许使用默认参数说明符";

C# 编译器错误";不允许使用默认参数说明符";,c#,compiler-errors,.net-3.5,default-parameters,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

下面是我的代码

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;
    }
}
公共类PItem
{
公共字符串内容;
公共整数计数;
公共int费用;
公共整数金额;
公共字符串描述;
//默认值
公共PItem(字符串“内容=”,整数计数=0,整数费用=0,字符串“描述=”,整数金额=0)
{
内容=_内容;
计数=_计数<0?0:_计数;
费用=_费用;
描述=_描述;
金额=\金额<0?0:\金额;
}
}
这是在教室里。当我尝试运行程序时,会出现以下错误:

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


如何解决此错误?

问题是在小于4的C版本中不能有可选参数。
你可以找到更多关于这方面的信息

您可以这样解决它:

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;
  }
}
公共类PItem
{
公共字符串内容;
公共整数计数;
公共int费用;
公共整数金额;
公共字符串描述;
//默认值
公共PItem():this(“,0,0,”,0){}
公共PItem(字符串_内容):这个(_内容,0,0,“,0){}
公共PItem(String _content,int _count):这个(_content,_count,0,“,0){}
公共PItem(String _content,int _count,int _fee):这个(_content,_count,_fee,“,0){}
公共PItem(String _content,int _count,int _fee,String _description):这个(_content,int count,int fee,_description,0){
公共PItem(字符串内容、整数计数、整数费用、字符串描述、整数金额)
{
内容=_内容;
计数=_计数<0?0:_计数;
费用=_费用;
描述=_描述;
金额=\金额<0?0:\金额;
}
}

如果您的项目似乎设置为.NET 4.0,则将其更改为例如3.5,然后再次更改为4.0。当我想在我的新软件中包含一个类库项目时,我从我的旧解决方案中包含了一个类库项目,然后将它添加到一个新的解决方案中。两种解决方案都是.NET4,但我得到了“默认参数说明符是不允许的”错误。我只是照我解释的做了。

你怎么知道你在使用C#4.0?这个错误是在哪里发生的?在编写时?是的,阿卡什。。在编译时,我遇到了这个错误。对于C#4.0,我很抱歉。。。我如何解决我的问题problme@hesamsalehnamadi既然你已经解决了你的问题,你可能会考虑通过编辑或删除你的问题来回馈一点,因为它是误导性的(你不使用C 4)。- 1:这是错误的信息。文档()明确指出“方法、构造函数、索引器或委托的定义可以指定其参数是必需的或是可选的。”@Jon:wording changed。。。更好?确实更好。虽然不幸的是,整个问题是一场惨败。谢谢!同样的解决方案对我起了作用。。。但是一个非常奇怪的错误!这也是我的解决方案。该项目是.NET4.5,但在我从Github中删除它之后,在第一次构建时给了我这个错误。