默认参数值必须是编译时间常数? [英] Default parameter for value must be a compile time constant?

查看:1549
本文介绍了默认参数值必须是编译时间常数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的方法签名。虽然试图通过结束作为可选的参数它给了我这个错误。我应该怎么做才能解决这个问题?为什么不是 DateTime.MinValue 恒定的?



 公共静态无效DatesToPeriodConverter(DateTime的开始,结束日期时间= DateTime.MinValue,出字符串日期,出字符串时间)


解决方案

DateTime.MinValue 不是常量,因为语言不喜欢常量的DateTime 。一种选择是使用的DateTime?来代替,即

 公共静态无效DatesToPeriodConverter(开始日期时间,日期?到底= NULL,
OUT字符串日期,出字符串时间)
{
VAR effectiveEnd =结束? 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 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.

这篇关于默认参数值必须是编译时间常数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆