C#中参数的默认值

发布于 2024-10-02 04:25:55 字数 558 浏览 1 评论 0原文

我有以下 Schedule 类并使用 .Net Framework 4.0
该类有两个构造函数,都具有 interval 参数,默认值为 120 秒。有没有办法在私有变量中设置间隔的默认值,然后将该变量分配给间隔参数。我尝试这样做,但出现编译时错误,提示 'interval' 的默认参数值必须是编译时常量

public class Schedule
{
    public Delegate Callback { get; set; }
    public object[] Params { get; set; }
    public int Interval { get; set; }

    public Schedule(Delegate callback, int interval = 120)
    {

    }

    public Schedule(Delegate callback, object[] parameters, int interval = 120)
    {

    }
}

I have the following class of Schedule and using .Net Framework 4.0.
this class has two constructors, both having the parameter of interval with a default value of 120 seconds. is there any way to set the default value of interval in a private variable and then assign that variable to interval parameter. I tried doing it, but I get a compile time error saying Default parameter value for 'interval' must be a compile-time constant

public class Schedule
{
    public Delegate Callback { get; set; }
    public object[] Params { get; set; }
    public int Interval { get; set; }

    public Schedule(Delegate callback, int interval = 120)
    {

    }

    public Schedule(Delegate callback, object[] parameters, int interval = 120)
    {

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉南桥 2024-10-09 04:25:55

不,因为可选参数的工作方式是调用代码的编译器将默认值融入其中。因此,像这样的调用:

new Schedule(MyCallback)

在编译时转换为:

new Schedule(MyCallback, 120)

所以这就是为什么它必须是常量。现在,您仍然可以将其设置为常量 - 如果您愿意,甚至可以将其设置为私有常量 - 但它不能是普通变量。所以这没问题:

public class Schedule
{
    private const int DefaultInterval = 120;

    public Schedule(Delegate callback, int interval = DefaultInterval)
    {

    }

    public Schedule(Delegate callback, object[] parameters,
                    int interval = DefaultInterval)
    {
         ...
    }
}

如果您想要一个在执行时可能变化的值,您可以使用可空类型作为参数,空值是默认值,在执行时替换为real默认值时间。例如,下面的方法允许您指定时间戳,但默认为“now”:

public void Foo(DateTime? timestamp = null)
{
    DateTime realTimestamp = timestamp ?? DateTime.UtcNow;
    ...
}

No, because the way that optional parameters work is that the compiler of the calling code has the default value baked into it. So a call like this:

new Schedule(MyCallback)

is converted at compile-time into:

new Schedule(MyCallback, 120)

So that's why it has to be a constant. Now, you can still make that a constant - even a private one, if you want - but it can't be a normal variable. So this would be okay:

public class Schedule
{
    private const int DefaultInterval = 120;

    public Schedule(Delegate callback, int interval = DefaultInterval)
    {

    }

    public Schedule(Delegate callback, object[] parameters,
                    int interval = DefaultInterval)
    {
         ...
    }
}

If you want a value which may vary at execution time, you could use a nullable type as the parameter, with the null value being the default, replaced by the real default at execution time. For example, here's a method which allows you to specify a timestamp, but defaults to "now":

public void Foo(DateTime? timestamp = null)
{
    DateTime realTimestamp = timestamp ?? DateTime.UtcNow;
    ...
}
桃气十足 2024-10-09 04:25:55

无法将动态值作为默认参数。它应该在编译时就知道。事实上,它与属性参数是可用作默认参数的同一组值,因为默认参数值作为 DefaultParameterValueAttribute 属性。

你可以使用一个常量:

private const int DefaultInterval = 120;
public Schedule(Delegate callback, int interval =DefaultInterval)
{

}

public Schedule(Delegate callback, object[] parameters, int interval = DefaultInterval)
{

}

There's no way to have a dynamic value as default parameter. It should be known at compile time. In fact the it is the same set of values that can be used as default parameter as for attribute arguments because default parameters values are emitted as the DefaultParameterValueAttribute attribute.

You could use a constant:

private const int DefaultInterval = 120;
public Schedule(Delegate callback, int interval =DefaultInterval)
{

}

public Schedule(Delegate callback, object[] parameters, int interval = DefaultInterval)
{

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文