C# value 的默认参数必须是编译时常量吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18740421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Default parameter for value must be a compile time constant?
提问by Ankit
This is my method signature. While trying to pass end
as an optional parameter it gives me this error. What should I do to resolve this? Why isn't DateTime.MinValue
a constant?
这是我的方法签名。在尝试end
作为可选参数传递时,它给了我这个错误。我应该怎么做才能解决这个问题?为什么不是DateTime.MinValue
常数?
public static void DatesToPeriodConverter(DateTime start, DateTime end = DateTime.MinValue,
out string date, out string time)
采纳答案by Marc Gravell
DateTime.MinValue
is not a const
, because the language doesn't like const
on DateTime
. One option is to use DateTime?
instead, i.e.
DateTime.MinValue
不是const
,因为语言不喜欢const
on DateTime
。一种选择是DateTime?
改用,即
public static void DatesToPeriodConverter(DateTime start, DateTime? end = null,
out string date, out string time)
{
var effectiveEnd = end ?? DateTime.MinValue;
// ...
}
However, you will still have the issue of having non-default parameters afterdefault parameters - you may need to re-order them to use that as a default.
但是,您仍然会遇到在默认参数之后使用非默认参数的问题- 您可能需要重新排序它们以将其用作默认值。
回答by Adam Houldsworth
Use regular method overloads instead:
改用常规方法重载:
public static void DatesToPeriodConverter(DateTime start, out string date, out string time)
{
DatesToPeriodConverter(start, DateTime.MinValue, out date, out time);
}
public static void DatesToPeriodConverter(DateTime start, DateTime end, out string date, out string time)
{ }
Atlernatively, default(DateTime)
is the same as DateTime.MinValue
and is compile time constant, but I tend to err away from using this style (there's no guarantee in future that default(DateTime)
will equal DateTime.MinValue
):
Atlernatively,default(DateTime)
是一样的DateTime.MinValue
,是编译时间常数,但我倾向于使用这种风格宁可走(有一个在将来不能保证default(DateTime)
将等于DateTime.MinValue
):
public static void DatesToPeriodConverter(DateTime start, DateTime end = default(DateTime), out string date, out string time)
Or as Marc suggests, use DateTime?
which allows a null
default value.
或者像 Marc 建议的那样,使用DateTime?
它允许null
默认值。
回答by Adarsh Kumar
You are correct. Default parameter for value must be a compile time constant. Dynamically calculated value is not accepted by compiler against optional parameter. The reason behind this may be that it is not definite that the dynamic value you are providing would give some valid value.
你是对的。value 的默认参数必须是编译时常量。编译器不接受针对可选参数的动态计算值。这背后的原因可能是不确定您提供的动态值是否会给出一些有效值。
回答by Viacheslav Ivanov
Change a type of the parameter end to a Nullable and use null as a default value:
将参数 end 的类型更改为 Nullable 并使用 null 作为默认值:
public static void DatesToPeriodConverter(DateTime start, DateTime? end = null, out string date, out string time)
or use default(DateTime) as a default value:
或使用 default(DateTime) 作为默认值:
public static void DatesToPeriodConverter(DateTime start, DateTime end = default(DateTime), out string date, out string time)
回答by Eduard Dumitru
Optional parameters must appear at the end of the parameter list. outparameters must also appear at the end of the parameter list. Your optional parameter is not an outparameter.
可选参数必须出现在参数列表的末尾。out参数也必须出现在参数列表的末尾。您的可选参数不是输出参数。
Furthermore, you can't use default values for optional parameters other than literal constants and a few weird corner cases.
此外,除了文字常量和一些奇怪的极端情况之外,您不能为可选参数使用默认值。
All facts point in the following direction:
所有事实都指向以下方向:
- Create a secondary overload method.
- Make the initial method not include the parameter
- Make the secondary one include the parameter
- Call your more general method (the one withthe parameter) from your more specific one and implement the logic only in the more general one
- 创建辅助重载方法。
- 使初始方法不包含参数
- 使次要包含参数
- 从更具体的方法中调用更通用的方法(带有参数的方法),并仅在更通用的方法中实现逻辑
回答by Adam Moszczyński
You can try doing it this way:
您可以尝试这样做:
public static void DatesToPeriodConverter(DateTime start, DateTime? end , out string date, out string time)
{
if(!end.HasValue){
end = DateTime.MinValue;
}
}
回答by HydTechie
we can create CONSTANTS class with default values
我们可以使用默认值创建 CONSTANTS 类
public const int DEFAULTINT = -9999;
public const int DEFAULTINT = -9999;
and use them as CONSTANTS.DEFAULTINT as business defaults..
并将它们用作 CONSTANTS.DEFAULTINT 作为业务默认值..
hope it helps,
希望能帮助到你,