We are trying to override the DateTime.MinValue in our application, but by doing it we noticed that our Web services are timing-out, following is a sample code. Not sure what is wrong/what we are missing.
public MainWindow()
{
//Introducing this.. Causes timeout of the webservice call...
typeof(DateTime).GetField("MinValue").SetValue(typeof(DateTime),new DateTime(1900, 1, 1));
var yesitworks= DateTime.MinValue;
InitializeComponent();
ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
//Below call will timeout...
var value =client.GetData(10);
}
PS: This might not be the best solution for what we are trying resolve but now its more of curiosity as to why it is not working? how is it related.
DateTime.MinValueis a static readonly field. That means that library authors will not expect it to change, and may write code that depends on it having the expected value.Hence, you should not change the value of
DateTime.MinValue.For example, a library may use it as the default value for a variable:
In this example, if
myDatesonly contained dates earlier than your new value forDateTime.MinValue, then this code would setmostRecentDatetoDateTime.MinValuerather than the latest date inmyDates.While this rather contrived example may not be good programming practise (for example, you could use Nullable instead), it is valid code, whose behaviour would be changed if you changed the value of
DateTime.MinValue.The point is that libraries you are using could also be dependant on the value on
DateTime.MinValue, so changing it could break them. You are llucky in so far as you found out that this introduced a bug early. If you are unlucky, you would not see a problem until your software had gone live and some corner case was hit.