value 的默认参数必须是编译时常量吗? [英] Default parameter for value must be a compile time constant?
本文介绍了value 的默认参数必须是编译时常量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的方法签名.在尝试将 end
作为可选参数传递时,它给了我这个错误.我应该怎么做才能解决这个问题?为什么 DateTime.MinValue
不是常量?
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?
public static void DatesToPeriodConverter(DateTime start, DateTime end = DateTime.MinValue,
out string date, out string time)
推荐答案
DateTime.MinValue
不是 const
,因为语言不喜欢 const
在 DateTime
上.一种选择是使用 DateTime?
代替,即
DateTime.MinValue
is not a const
, because the language doesn't like const
on DateTime
. One option is to use DateTime?
instead, i.e.
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 after default parameters - you may need to re-order them to use that as a default.
这篇关于value 的默认参数必须是编译时常量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文