由网友(诗酒趁年少)分享简介:下面是我的code public class PItem{public String content;public int count;public int fee;public int amount;public string description;// default valuespublic PItem(S...
下面是我的code
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 on a class. when i try to run program gives this error:
默认参数指定不允许
我该如何解决这个问题?
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.
您可以解决这个问题是这样的:
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;
}
}
相关推荐
最新文章