Compiler error “Default parameter specifiers are not permitted”

前端 未结 2 523
独厮守ぢ
独厮守ぢ 2020-12-11 02:48

Below is my code.

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


        
2条回答
  •  春和景丽
    2020-12-11 02:58

    The problem is that you cannot have optional parameters in C# version less than 4.
    You can find more information on this here.

    You can solve it like this:

    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;
      }
    }
    

提交回复
热议问题