Can someone explain what’s wrong with the following method signature written using C# 4.0?
public void Test(string arg1 = string.Empty, DateTime arg2 = DateTime.MinValue){}
I understand the difference between “” and string.Empty in terms of compile time checking but surely the way that optional parameters have been implemented in C# 4.0 is pretty inadequate if you can’t declare a reasonable value type null style comparisson?
Because
DateTime.MinValueandDateTime.MaxValuearen’t compile time-constants — they’rereadonlyfields that are initialized at run time byDateTime‘s static constructor.See the difference between
constfields (which are compile-time constants) andreadonlyfields (which aren’t): What is the difference between const and readonly?