下面是我的代码。
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/