C# 4.0: Can I use a TimeSpan as an optional parameter with a default value?

前端 未结 8 2107
星月不相逢
星月不相逢 2020-11-28 10:55

Both of these generate an error saying they must be a compile-time constant:

void Foo(TimeSpan span = TimeSpan.FromSeconds(2.0))
void Foo(TimeSpan span = new         


        
8条回答
  •  南方客
    南方客 (楼主)
    2020-11-28 11:35

    TimeSpan is a special case for the DefaultValueAttribute and is specified using any string that can be parsed via the TimeSpan.Parse method.

    [DefaultValue("0:10:0")]
    public TimeSpan Duration { get; set; }
    

提交回复
热议问题