| public void DoStuff(TimeSpan interval = TimeSpan.FromSeconds(5)) |
The compiler error is: "Error 1 Default parameter value for 'interval' must be a compile-time constant".
I have tried making a const field in the class but still no luck.
| public void DoStuff(TimeSpan interval = TimeSpan.FromSeconds(5)) |
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Zoran HorvatRank #792 · 2.9K reputationPosted Jul 25, 2011, 3:40 AM EDT
Do not use TimeSpan as argument but integer which receives seconds, then declare the method like this:
public void DoStuff(int seconds=5)
{
TimeSpan interval = TimeSpan.FromSeconds(seconds);
...
}
Second solution:
Do it old style C# - define two methods, one receiving interval, another without arguments:
public void DoStuff(TimeSpan interval)
{
...
}
public void DoStuff()
{
DoStuff(TmeSpan.FromSeconds(5));
}
Zoran
Benjamin KemnerRank #7,492 · 240 reputationPosted Jul 25, 2011, 3:51 AM EDT
public void DoStuff(int initvalue=5){
TimeSpan interval = TimeSpan.FromSeconds(initvalue);
}
Or the "old-school" way:
public void DoStuff(){
DoStuff(TimeSpan.FromSeconds(5));
}
public void DoStuff(TimeSpan interval){
}
Zoran HorvatRank #792 · 2.9K reputationPosted Jul 25, 2011, 3:52 AM EDT
Guest UserRank #1,422 · 1.2K reputationPosted Jul 25, 2011, 4:38 AM EDT
VulpesRank #55 · 29.2K reputationPosted Jul 25, 2011, 4:51 AM EDT
VulpesRank #55 · 29.2K reputationPosted Jul 25, 2011, 5:24 AM EDT
This works because default(TimeSpan) sets all bits to zero and so is equivalent to TimeSpan.Zero. However, the former is regarded as a constant but the latter is not.
time and space31 reputationPosted Jul 28, 2011, 7:34 PM EDT
time and space31 reputationPosted Apr 2, 2012, 5:11 AM EDT
VulpesRank #55 · 29.2K reputationPosted Apr 2, 2012, 5:25 AM EDT
I think that it uses a long integer (8 bytes) to represent TimeSpan values internally, so TimeSpan.Zero will be repesented by a long with a value of 0.
TimeSpan.Zero is a static readonly field which creates a single TimeSpan instance. The same instance is used each time the field is accessed.
Vector2 is also a struct though for some reason Vector2.Zero is a static readonly property rather than a field.
time and space31 reputationPosted Apr 2, 2012, 5:33 AM EDT
VulpesRank #55 · 29.2K reputationPosted Apr 2, 2012, 7:08 AM EDT
You'd just be copying the values to another location and it would be no shorter to write.
time and space31 reputationPosted Apr 2, 2012, 7:17 AM EDT
VulpesRank #55 · 29.2K reputationPosted Apr 2, 2012, 7:34 AM EDT
So, the memory location for TimeSpan.Zero actually contains an 8 byte value - it doesn't contain a reference (i.e. a pointer) to where an instance is stored on the heap which would be the case if TimeSpan were a class rather than a struct.
By always calling TimeSpan.Zero, you're re-using the value stored at its memory location. This value will persist throughout the application's lifetime and will never be garbage collected.