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

前端 未结 8 2131
星月不相逢
星月不相逢 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:32

    My suggestion:

    void A( long spanInMs = 2000 )
    {
        var ts = TimeSpan.FromMilliseconds(spanInMs);
    
        //...
    }
    

    BTW TimeSpan.FromSeconds(2.0) does not equal new TimeSpan(2000) - the constructor takes ticks.

提交回复
热议问题