值的默认参数必须是编译时常量?

时间:2022-08-30 10:05:30

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)

7 个解决方案

#1


59  

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,因为该语言不喜欢DateTime上的const。一种选择是使用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 after default parameters - you may need to re-order them to use that as a default.

但是,在默认参数之后仍然存在非默认参数的问题 - 您可能需要重新排序它们以将其用作默认参数。

#2


12  

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):

atlernative,默认(DateTime)与DateTime.MinValue相同,并且是编译时常量,但我倾向于使用此样式(以后无法保证默认(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默认值。

#3


4  

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;
    }
}

#4


3  

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:

或使用默认值(DateTime)作为默认值:

public static void DatesToPeriodConverter(DateTime start, DateTime end = default(DateTime), out string date, out string time)

#5


2  

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的默认参数必须是编译时常量。编译器不接受可选参数的动态计算值。这背后的原因可能是,您提供的动态值不能确定是否会给出一些有效值。

#6


1  

Optional parameters must appear at the end of the parameter list. out parameters must also appear at the end of the parameter list. Your optional parameter is not an out parameter.

可选参数必须出现在参数列表的末尾。 out参数也必须出现在参数列表的末尾。您的可选参数不是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 with the parameter) from your more specific one and implement the logic only in the more general one
  • 从更具体的方法中调用更通用的方法(带参数的方法),并仅在更一般的方法中实现逻辑

#7


-1  

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,

希望能帮助到你,

#1


59  

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,因为该语言不喜欢DateTime上的const。一种选择是使用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 after default parameters - you may need to re-order them to use that as a default.

但是,在默认参数之后仍然存在非默认参数的问题 - 您可能需要重新排序它们以将其用作默认参数。

#2


12  

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):

atlernative,默认(DateTime)与DateTime.MinValue相同,并且是编译时常量,但我倾向于使用此样式(以后无法保证默认(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默认值。

#3


4  

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;
    }
}

#4


3  

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:

或使用默认值(DateTime)作为默认值:

public static void DatesToPeriodConverter(DateTime start, DateTime end = default(DateTime), out string date, out string time)

#5


2  

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的默认参数必须是编译时常量。编译器不接受可选参数的动态计算值。这背后的原因可能是,您提供的动态值不能确定是否会给出一些有效值。

#6


1  

Optional parameters must appear at the end of the parameter list. out parameters must also appear at the end of the parameter list. Your optional parameter is not an out parameter.

可选参数必须出现在参数列表的末尾。 out参数也必须出现在参数列表的末尾。您的可选参数不是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 with the parameter) from your more specific one and implement the logic only in the more general one
  • 从更具体的方法中调用更通用的方法(带参数的方法),并仅在更一般的方法中实现逻辑

#7


-1  

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,

希望能帮助到你,