I would declare an empty String variable like this:
string myString = string.Empty;
Is there an equivalent for a 'DateTime' variable ?
Update :
The problem is I use this 'DateTime' as a parameter for a 'StoredProcedure' in SQL. E.g:
DateTime? someDate = null; myCommand.Parameters.AddWithValue("@SurgeryDate", someDate);
When I run this code an exception is catched telling me the 'StoredProcedure' expected a '@SurgeryDate' parameter. But i provided it. Any idea why?
DateTime. MinValue; The above will display the minimum value i.e. Let us see how to display the minimum value and avoid adding null to a date to initialize it as empty.
DateTime CAN be compared to null; It cannot hold null value, thus the comparison will always be false. DateTime is a "Value Type". Basically a "value type" can't set to NULL. But by making them to "Nullable" type, We can set to null.
The Nullable < T > structure is using a value type as a nullable type. By default DateTime is not nullable because it is a Value Type, using the nullable operator introduced in C# 2, you can achieve this. Using a question mark (?) after the type or using the generic style Nullable.
Use model. myDate. HasValue. It will return true if date is not null otherwise false.
Since DateTime
is a value type you cannot assign null
to it, but exactly for these cases (absence of a value) Nullable<T>
was introduced - use a nullable DateTime
instead:
DateTime? myTime = null;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With