编译器错误“不允许使用默认参数说明符" [英] Compiler error "Default parameter specifiers are not permitted"

查看:31
本文介绍了编译器错误“不允许使用默认参数说明符"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的代码.

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

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

This is inside in a class. When I try to run a program it gives this error:

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

Default parameter specifiers are not permitted

我该如何解决这个错误?

How can I solve this error?

推荐答案

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

The problem is that you cannot have optional parameters in C# version less than 4.
You can find more information on this 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;
  }
}

这篇关于编译器错误“不允许使用默认参数说明符"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆