是不允许的默认参数说明符"编译器错误" [英] Compiler error "Default parameter specifiers are not permitted"
本文介绍了是不允许的默认参数说明符"编译器错误"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面是我的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;
}
}
这篇关于是不允许的默认参数说明符&QUOT;编译器错误&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文